mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-20 20:46:58 +02:00
refactor(v2): convert blog page to be added to routes by plugin lifecycle
This commit is contained in:
parent
74d26d4f3d
commit
7475051526
4 changed files with 37 additions and 64 deletions
|
@ -11,16 +11,14 @@ async function genRoutesConfig({
|
||||||
siteConfig = {},
|
siteConfig = {},
|
||||||
docsMetadatas = {},
|
docsMetadatas = {},
|
||||||
pagesMetadatas = [],
|
pagesMetadatas = [],
|
||||||
contentsStore = {},
|
|
||||||
pluginRouteConfigs = [],
|
pluginRouteConfigs = [],
|
||||||
}) {
|
}) {
|
||||||
const modules = [
|
const imports = [
|
||||||
`import React from 'react';`,
|
`import React from 'react';`,
|
||||||
`import Loadable from 'react-loadable';`,
|
`import Loadable from 'react-loadable';`,
|
||||||
`import Loading from '@theme/Loading';`,
|
`import Loading from '@theme/Loading';`,
|
||||||
`import Doc from '@theme/Doc';`,
|
`import Doc from '@theme/Doc';`,
|
||||||
`import DocBody from '@theme/DocBody';`,
|
`import DocBody from '@theme/DocBody';`,
|
||||||
`import BlogPage from '@theme/BlogPage';`,
|
|
||||||
`import Pages from '@theme/Pages';`,
|
`import Pages from '@theme/Pages';`,
|
||||||
`import NotFound from '@theme/NotFound';`,
|
`import NotFound from '@theme/NotFound';`,
|
||||||
];
|
];
|
||||||
|
@ -34,7 +32,7 @@ async function genRoutesConfig({
|
||||||
path: '${permalink}',
|
path: '${permalink}',
|
||||||
exact: true,
|
exact: true,
|
||||||
component: Loadable({
|
component: Loadable({
|
||||||
loader: () => import(/* webpackPrefetch: true */ '${source}'),
|
loader: () => import('${source}'),
|
||||||
loading: Loading,
|
loading: Loading,
|
||||||
render(loaded, props) {
|
render(loaded, props) {
|
||||||
let Content = loaded.default;
|
let Content = loaded.default;
|
||||||
|
@ -66,7 +64,7 @@ async function genRoutesConfig({
|
||||||
path: '${permalink}',
|
path: '${permalink}',
|
||||||
exact: true,
|
exact: true,
|
||||||
component: Loadable({
|
component: Loadable({
|
||||||
loader: () => import(/* webpackPrefetch: true */ '${source}'),
|
loader: () => import('${source}'),
|
||||||
loading: Loading,
|
loading: Loading,
|
||||||
render(loaded, props) {
|
render(loaded, props) {
|
||||||
let Content = loaded.default;
|
let Content = loaded.default;
|
||||||
|
@ -80,40 +78,6 @@ async function genRoutesConfig({
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blog.
|
|
||||||
function genBlogPageRoute(metadata) {
|
|
||||||
const {permalink} = metadata;
|
|
||||||
const {posts} = metadata;
|
|
||||||
return `
|
|
||||||
{
|
|
||||||
path: '${permalink}',
|
|
||||||
exact: true,
|
|
||||||
component: Loadable.Map({
|
|
||||||
loader: {
|
|
||||||
${posts
|
|
||||||
.map(
|
|
||||||
(post, index) =>
|
|
||||||
`post${index}: () => import(/* webpackPrefetch: true */ '${
|
|
||||||
post.source
|
|
||||||
}')`,
|
|
||||||
)
|
|
||||||
.join(',\n\t\t\t\t')}
|
|
||||||
},
|
|
||||||
loading: Loading,
|
|
||||||
render(loaded, props) {
|
|
||||||
${posts
|
|
||||||
.map((p, i) => `const Post${i} = loaded.post${i}.default;`)
|
|
||||||
.join('\n\t\t\t\t')}
|
|
||||||
return (
|
|
||||||
<BlogPage {...props} metadata={${JSON.stringify(metadata)}} >
|
|
||||||
${posts.map((p, i) => `<Post${i} />`).join(' ')}
|
|
||||||
</BlogPage>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const notFoundRoute = `
|
const notFoundRoute = `
|
||||||
{
|
{
|
||||||
path: '*',
|
path: '*',
|
||||||
|
@ -121,47 +85,46 @@ async function genRoutesConfig({
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const routes = pluginRouteConfigs.map(pluginRouteConfig => {
|
const routes = pluginRouteConfigs.map(pluginRouteConfig => {
|
||||||
const {path, component, metadata, content} = pluginRouteConfig;
|
const {path, component, metadata, modules} = pluginRouteConfig;
|
||||||
return `
|
return `
|
||||||
{
|
{
|
||||||
path: '${path}',
|
path: '${path}',
|
||||||
exact: true,
|
exact: true,
|
||||||
component: Loadable.Map({
|
component: Loadable.Map({
|
||||||
loader: {
|
loader: {
|
||||||
Content: () => import('${content}'),
|
${modules
|
||||||
|
.map(
|
||||||
|
(module, index) => ` Module${index}: () => import('${module}'),`,
|
||||||
|
)
|
||||||
|
.join('\n')}
|
||||||
Component: () => import('${component}'),
|
Component: () => import('${component}'),
|
||||||
},
|
},
|
||||||
loading: Loading,
|
loading: Loading,
|
||||||
render(loaded, props) {
|
render(loaded, props) {
|
||||||
const Content = loaded.Content.default;
|
|
||||||
const Component = loaded.Component.default;
|
const Component = loaded.Component.default;
|
||||||
|
const modules = [
|
||||||
|
${modules
|
||||||
|
.map((module, index) => ` loaded.Module${index}.default,`)
|
||||||
|
.join('\n')}
|
||||||
|
];
|
||||||
return (
|
return (
|
||||||
<Component {...props} metadata={${JSON.stringify(metadata)}}>
|
<Component {...props} metadata={${JSON.stringify(
|
||||||
<Content />
|
metadata,
|
||||||
</Component>
|
)}} modules={modules}/>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}`;
|
}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
return `
|
return `
|
||||||
${modules.join('\n')}
|
${imports.join('\n')}
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
// Docs.${pagesMetadatas.map(genPagesRoute).join(',')},
|
// Docs.${pagesMetadatas.map(genPagesRoute).join(',')},
|
||||||
|
|
||||||
// Pages.${docsRoutes},
|
// Pages.${docsRoutes},
|
||||||
|
|
||||||
// Blog.${
|
|
||||||
contentsStore.blog
|
|
||||||
? contentsStore.blog.contents
|
|
||||||
.filter(metadata => metadata.isBlogPage)
|
|
||||||
.map(genBlogPageRoute)
|
|
||||||
.join(',')
|
|
||||||
: ''
|
|
||||||
},
|
|
||||||
|
|
||||||
// Plugins.${routes.join(',')},
|
// Plugins.${routes.join(',')},
|
||||||
|
|
||||||
// Not Found.${notFoundRoute},
|
// Not Found.${notFoundRoute},
|
||||||
|
|
|
@ -9,7 +9,6 @@ import React, {useContext} from 'react';
|
||||||
import Link from '@docusaurus/Link';
|
import Link from '@docusaurus/Link';
|
||||||
import Head from '@docusaurus/Head';
|
import Head from '@docusaurus/Head';
|
||||||
import Layout from '@theme/Layout'; // eslint-disable-line
|
import Layout from '@theme/Layout'; // eslint-disable-line
|
||||||
import BlogPost from '@theme/BlogPost'; // eslint-disable-line
|
|
||||||
|
|
||||||
import DocusaurusContext from '@docusaurus/context';
|
import DocusaurusContext from '@docusaurus/context';
|
||||||
|
|
||||||
|
@ -17,6 +16,7 @@ function BlogPage(props) {
|
||||||
const context = useContext(DocusaurusContext);
|
const context = useContext(DocusaurusContext);
|
||||||
const {blogMetadata, language, siteConfig = {}} = context;
|
const {blogMetadata, language, siteConfig = {}} = context;
|
||||||
const {baseUrl, favicon} = siteConfig;
|
const {baseUrl, favicon} = siteConfig;
|
||||||
|
const {modules: BlogPosts} = props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
|
@ -34,7 +34,9 @@ function BlogPage(props) {
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
{props.children}
|
{BlogPosts.map(BlogPost => (
|
||||||
|
<BlogPost />
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
|
|
|
@ -88,6 +88,9 @@ class BlogPost extends React.Component {
|
||||||
const {metadata = {}, siteConfig = {}} = this.context;
|
const {metadata = {}, siteConfig = {}} = this.context;
|
||||||
const {baseUrl, favicon} = siteConfig;
|
const {baseUrl, favicon} = siteConfig;
|
||||||
const {language, title} = metadata;
|
const {language, title} = metadata;
|
||||||
|
const {modules} = this.props;
|
||||||
|
const BlogPostContents = modules[0];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<Head defaultTitle={siteConfig.title}>
|
<Head defaultTitle={siteConfig.title}>
|
||||||
|
@ -96,7 +99,7 @@ class BlogPost extends React.Component {
|
||||||
{language && <html lang={language} />}
|
{language && <html lang={language} />}
|
||||||
</Head>
|
</Head>
|
||||||
{this.renderPostHeader()}
|
{this.renderPostHeader()}
|
||||||
{this.props.children}
|
<BlogPostContents />
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,12 +105,17 @@ class DocusaurusContentBlogPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
async generateRoutes({contents, actions}) {
|
async generateRoutes({contents, actions}) {
|
||||||
const {blogPostComponent} = this.options;
|
const {blogPageComponent, blogPostComponent} = this.options;
|
||||||
const {addRoute} = actions;
|
const {addRoute} = actions;
|
||||||
contents.forEach(metadata => {
|
contents.forEach(metadata => {
|
||||||
const {permalink, source} = metadata;
|
const {isBlogPage, permalink} = metadata;
|
||||||
if (metadata.isBlogPage) {
|
if (isBlogPage) {
|
||||||
// TODO: Handle blog page.
|
addRoute({
|
||||||
|
path: permalink,
|
||||||
|
component: blogPageComponent,
|
||||||
|
metadata,
|
||||||
|
modules: metadata.posts.map(post => post.source),
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +123,7 @@ class DocusaurusContentBlogPlugin {
|
||||||
path: permalink,
|
path: permalink,
|
||||||
component: blogPostComponent,
|
component: blogPostComponent,
|
||||||
metadata,
|
metadata,
|
||||||
content: source,
|
modules: [metadata.source],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue