mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-29 06:29:03 +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
38
lib/load/routes.js
Normal file
38
lib/load/routes.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
const path = require('path');
|
||||
const {fileToComponentName} = require('./utils');
|
||||
|
||||
async function genRoutesConfig({docsData, docsDir}) {
|
||||
function genDocsRoute({path: docsPath, source}) {
|
||||
const componentName = fileToComponentName(source);
|
||||
return `
|
||||
{
|
||||
path: ${JSON.stringify(docsPath)},
|
||||
component: () => <Docs><${componentName} /></Docs>
|
||||
}`;
|
||||
}
|
||||
|
||||
function genDocsImport({source}) {
|
||||
const filePath = path.resolve(docsDir, source);
|
||||
const componentName = fileToComponentName(source);
|
||||
return `import ${componentName} from ${JSON.stringify(filePath)}`;
|
||||
}
|
||||
|
||||
const notFoundRoute = `,
|
||||
{
|
||||
path: '*',
|
||||
component: NotFound
|
||||
}`;
|
||||
|
||||
return (
|
||||
`import React from 'react';\n` +
|
||||
`import Docs from '@theme/Docs';\n` +
|
||||
`import NotFound from '@theme/NotFound';\n` +
|
||||
`${docsData.map(genDocsImport).join('\n')}\n` +
|
||||
`const routes = [${docsData
|
||||
.map(genDocsRoute)
|
||||
.join(',')}${notFoundRoute}\n];\n` +
|
||||
`export default routes;\n`
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = genRoutesConfig;
|
Loading…
Add table
Add a link
Reference in a new issue