mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 20:32:42 +02:00
feat: react router config generation for docs
This commit is contained in:
parent
f9bc0ff7b8
commit
9e7729ad74
5 changed files with 124 additions and 103 deletions
|
@ -2,16 +2,7 @@ const fs = require('fs-extra');
|
|||
const path = require('path');
|
||||
const fm = require('front-matter');
|
||||
const globby = require('globby');
|
||||
|
||||
const indexRE = /(^|.*\/)index\.md$/i;
|
||||
const mdRE = /\.md$/;
|
||||
|
||||
function fileToPath(file) {
|
||||
if (indexRE.test(file)) {
|
||||
return file.replace(indexRE, '/$1');
|
||||
}
|
||||
return `/${file.replace(mdRE, '').replace(/\\/g, '/')}`;
|
||||
}
|
||||
const {encodePath, fileToPath} = require('./utils');
|
||||
|
||||
function parse(fileString) {
|
||||
if (!fm.test(fileString)) {
|
||||
|
@ -22,27 +13,25 @@ function parse(fileString) {
|
|||
return {metadata, content};
|
||||
}
|
||||
|
||||
// still TODO. still copy paste from blog logic
|
||||
async function loadDocs(siteDir) {
|
||||
const blogFiles = await globby(['**/*.md'], {
|
||||
cwd: siteDir
|
||||
async function loadDocs(docsDir) {
|
||||
const docsFiles = await globby(['**/*.md'], {
|
||||
cwd: docsDir
|
||||
});
|
||||
|
||||
const blogDatas = await Promise.all(
|
||||
blogFiles.map(async file => {
|
||||
const filepath = path.resolve(siteDir, file);
|
||||
const docsData = await Promise.all(
|
||||
docsFiles.map(async source => {
|
||||
const filepath = path.resolve(docsDir, source);
|
||||
const fileString = await fs.readFile(filepath, 'utf-8');
|
||||
const {metadata, content} = parse(fileString);
|
||||
const {metadata} = parse(fileString);
|
||||
|
||||
return {
|
||||
path: fileToPath(file),
|
||||
content,
|
||||
path: encodePath(fileToPath(source)),
|
||||
source,
|
||||
...metadata
|
||||
};
|
||||
})
|
||||
);
|
||||
blogDatas.sort((a, b) => b.date - a.date);
|
||||
return blogDatas;
|
||||
return docsData;
|
||||
}
|
||||
|
||||
module.exports = loadDocs;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue