mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-27 03:08:06 +00:00
Initial check-in of new React docs and website
Co-authored-by: Dan Abramov <dan.abramov@me.com> Co-authored-by: Sylwia Vargas <sylwia.vargas@gmail.com> Co-authored-by: Dan Lebowitz <dan.lebo@me.com> Co-authored-by: Razvan Gradinar <grazvan@fb.com> Co-authored-by: Jared Palmer <jared@palmer.net> Co-authored-by: Dane Grant <danecando@gmail.com> Co-authored-by: Dustin Goodman <dustin.s.goodman@gmail.com> Co-authored-by: Rick Hanlon <rickhanlonii@gmail.com> Co-authored-by: Maggie Appleton <maggie.fm.appleton@gmail.com> Co-authored-by: Alex Moldovan <alex.n.moldovan@gmail.com> Co-authored-by: Ives van Hoorne <ives.v.h@gmail.com> Co-authored-by: Brian Vaughn <bvaughn@fb.com>
This commit is contained in:
committed by
Dan Abramov
parent
b62e4475d6
commit
5f404d978c
69
scripts/generateBlogIndex.js
Normal file
69
scripts/generateBlogIndex.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const fm = require('gray-matter');
|
||||
const globby = require('globby');
|
||||
const parseISO = require('date-fns/parseISO');
|
||||
const readingTime = require('reading-time');
|
||||
const {markdownToHtml} = require('../plugins/markdownToHtml');
|
||||
|
||||
/**
|
||||
* This looks at the ./src/pages/blog directory and creates a route manifest that can be used
|
||||
* in the sidebar and footers, and (in theory) category and author pages.
|
||||
*
|
||||
* For now, the blog manifest is a big array in reverse chronological order.
|
||||
*/
|
||||
Promise.resolve()
|
||||
.then(async () => {
|
||||
const routes = [];
|
||||
const blogPosts = await globby('src/pages/blog/**/*.md');
|
||||
|
||||
for (let postpath of blogPosts) {
|
||||
const [year, month, day, title] = postpath
|
||||
.replace('src/pages/blog/', '')
|
||||
.split('/');
|
||||
|
||||
const rawStr = await fs.readFile(postpath, 'utf8');
|
||||
const {data, excerpt, content} = fm(rawStr, {
|
||||
excerpt: function firstLine(file, options) {
|
||||
file.excerpt = file.content.split('\n').slice(0, 2).join(' ');
|
||||
},
|
||||
});
|
||||
const rendered = await markdownToHtml(excerpt.trimLeft().trim());
|
||||
|
||||
routes.unshift({
|
||||
path: postpath.replace('src/pages', ''),
|
||||
date: [year, month, day].join('-'),
|
||||
title: data.title,
|
||||
author: data.author,
|
||||
excerpt: rendered,
|
||||
readingTime: readingTime(content).text,
|
||||
});
|
||||
}
|
||||
|
||||
const sorted = routes.sort((post1, post2) =>
|
||||
parseISO(post1.date) > parseISO(post2.date) ? -1 : 1
|
||||
);
|
||||
const blogManifest = {
|
||||
routes: sorted,
|
||||
};
|
||||
const blogRecentSidebar = {
|
||||
routes: [
|
||||
{
|
||||
title: 'Recent Posts',
|
||||
path: '/blog',
|
||||
heading: true,
|
||||
routes: sorted.slice(0, 25),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
await fs.writeFile(
|
||||
path.resolve('./src/blogIndex.json'),
|
||||
JSON.stringify(blogManifest, null, 2)
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.resolve('./src/blogIndexRecent.json'),
|
||||
JSON.stringify(blogRecentSidebar, null, 2)
|
||||
);
|
||||
})
|
||||
.catch(console.error);
|
||||
Reference in New Issue
Block a user