mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
feat(v2): createData plugin API (#1400)
* feat(v2): createModule plugin api * remove unused stuff * address review, createModule -> createData * link.link -> link.url * remove youtube page * update yarn.lock
This commit is contained in:
parent
195e934858
commit
b3cf9c62d5
18 changed files with 164 additions and 349 deletions
|
@ -8,7 +8,7 @@
|
|||
const globby = require('globby');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const {parse, idx, normalizeUrl} = require('@docusaurus/utils');
|
||||
const {parse, idx, normalizeUrl, docuHash} = require('@docusaurus/utils');
|
||||
|
||||
// TODO: Use a better slugify function that doesn't rely on a specific file extension.
|
||||
function fileToUrl(fileName) {
|
||||
|
@ -20,8 +20,6 @@ function fileToUrl(fileName) {
|
|||
}
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
metadataKey: 'blogMetadata',
|
||||
metadataFileName: 'blogMetadata.json',
|
||||
path: 'blog', // Path to data on filesystem, relative to site dir.
|
||||
routeBasePath: 'blog', // URL Route.
|
||||
include: ['*.md', '*.mdx'], // Extensions to include.
|
||||
|
@ -121,40 +119,46 @@ class DocusaurusPluginContentBlog {
|
|||
|
||||
async contentLoaded({content, actions}) {
|
||||
const {blogPageComponent, blogPostComponent} = this.options;
|
||||
const {addRoute} = actions;
|
||||
content.forEach(metadataItem => {
|
||||
const {isBlogPage, permalink} = metadataItem;
|
||||
if (isBlogPage) {
|
||||
const {addRoute, createData} = actions;
|
||||
await Promise.all(
|
||||
content.map(async metadataItem => {
|
||||
const {isBlogPage, permalink} = metadataItem;
|
||||
const metadataPath = await createData(
|
||||
`${docuHash(permalink)}.json`,
|
||||
JSON.stringify(metadataItem, null, 2),
|
||||
);
|
||||
if (isBlogPage) {
|
||||
addRoute({
|
||||
path: permalink,
|
||||
component: blogPageComponent,
|
||||
exact: true,
|
||||
modules: {
|
||||
entries: metadataItem.posts.map(post => ({
|
||||
// To tell routes.js this is an import and not a nested object to recurse.
|
||||
__import: true,
|
||||
path: post.source,
|
||||
query: {
|
||||
truncated: true,
|
||||
},
|
||||
})),
|
||||
metadata: metadataPath,
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
addRoute({
|
||||
path: permalink,
|
||||
component: blogPageComponent,
|
||||
component: blogPostComponent,
|
||||
exact: true,
|
||||
metadata: metadataItem,
|
||||
modules: {
|
||||
entries: metadataItem.posts.map(post => ({
|
||||
// To tell routes.js this is an import and not a nested object to recurse.
|
||||
__import: true,
|
||||
path: post.source,
|
||||
query: {
|
||||
truncated: true,
|
||||
},
|
||||
})),
|
||||
content: metadataItem.source,
|
||||
metadata: metadataPath,
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
addRoute({
|
||||
path: permalink,
|
||||
component: blogPostComponent,
|
||||
exact: true,
|
||||
metadata: metadataItem,
|
||||
modules: {
|
||||
content: metadataItem.source,
|
||||
},
|
||||
});
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
configureWebpack(config, isServer, {getBabelLoader, getCacheLoader}) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue