mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 19:16:58 +02:00
refactor(v2): convert plugins into a class
This commit is contained in:
parent
006f7050cd
commit
92c7e1f44b
2 changed files with 83 additions and 76 deletions
|
@ -84,14 +84,21 @@ module.exports = async function load(siteDir) {
|
||||||
// Process plugins.
|
// Process plugins.
|
||||||
if (siteConfig.plugins) {
|
if (siteConfig.plugins) {
|
||||||
const context = {env, siteDir, siteConfig};
|
const context = {env, siteDir, siteConfig};
|
||||||
|
// Currently runs all plugins in parallel and not order-dependent. We could change
|
||||||
|
// this in future if there's a need.
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
siteConfig.plugins.map(async ({name, options: opts}) => {
|
siteConfig.plugins.map(async ({name, options: opts}) => {
|
||||||
// TODO: Resolve using node_modules as well.
|
// TODO: Resolve using node_modules as well.
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
const plugin = require(path.resolve(__dirname, '../../plugins', name));
|
const Plugin = require(path.resolve(__dirname, '../../plugins', name));
|
||||||
const pluginContent = await plugin.onLoadContent(opts, context);
|
const plugin = new Plugin(opts, context);
|
||||||
const {options, contents} = pluginContent;
|
const {options} = plugin;
|
||||||
contentsStore[options.contentKey] = pluginContent;
|
const contents = await plugin.load();
|
||||||
|
const pluginContents = {
|
||||||
|
options,
|
||||||
|
contents,
|
||||||
|
};
|
||||||
|
contentsStore[options.contentKey] = pluginContents;
|
||||||
await generate(
|
await generate(
|
||||||
generatedFilesDir,
|
generatedFilesDir,
|
||||||
options.cachePath,
|
options.cachePath,
|
||||||
|
|
|
@ -28,79 +28,79 @@ const DEFAULT_OPTIONS = {
|
||||||
cachePath: 'blogMetadata.js',
|
cachePath: 'blogMetadata.js',
|
||||||
};
|
};
|
||||||
|
|
||||||
async function onLoadContent(opts, context) {
|
class DocusaurusContentBlogPlugin {
|
||||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
constructor(opts, context) {
|
||||||
|
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||||
const {env, siteConfig, siteDir} = context;
|
this.context = context;
|
||||||
const {pageCount, path: filePath, routeBasePath} = options;
|
|
||||||
const blogDir = path.resolve(siteDir, filePath);
|
|
||||||
const {baseUrl} = siteConfig;
|
|
||||||
|
|
||||||
const blogFiles = await globby(options.include, {
|
|
||||||
cwd: blogDir,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Prepare metadata container.
|
|
||||||
const blogMetadata = [];
|
|
||||||
|
|
||||||
// Language for each blog page.
|
|
||||||
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
|
|
||||||
|
|
||||||
await Promise.all(
|
|
||||||
blogFiles.map(async relativeSource => {
|
|
||||||
const source = path.join(blogDir, relativeSource);
|
|
||||||
|
|
||||||
const blogFileName = path.basename(relativeSource);
|
|
||||||
// Extract, YYYY, MM, DD from the file name.
|
|
||||||
const filePathDateArr = blogFileName.split('-');
|
|
||||||
const date = new Date(
|
|
||||||
`${filePathDateArr[0]}-${filePathDateArr[1]}-${
|
|
||||||
filePathDateArr[2]
|
|
||||||
}T06:00:00.000Z`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const fileString = await fs.readFile(source, 'utf-8');
|
|
||||||
const {metadata: rawMetadata} = parse(fileString);
|
|
||||||
const metadata = {
|
|
||||||
permalink: normalizeUrl([
|
|
||||||
baseUrl,
|
|
||||||
routeBasePath,
|
|
||||||
fileToUrl(blogFileName),
|
|
||||||
]),
|
|
||||||
source,
|
|
||||||
...rawMetadata,
|
|
||||||
date,
|
|
||||||
language: defaultLangTag,
|
|
||||||
};
|
|
||||||
blogMetadata.push(metadata);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
blogMetadata.sort((a, b) => a.date - b.date);
|
|
||||||
|
|
||||||
// Blog page handling. Example: `/blog`, `/blog/page1`, `/blog/page2`
|
|
||||||
const numOfBlog = blogMetadata.length;
|
|
||||||
const numberOfPage = Math.ceil(numOfBlog / pageCount);
|
|
||||||
const basePageUrl = path.join(baseUrl, routeBasePath);
|
|
||||||
|
|
||||||
// eslint-disable-next-line
|
|
||||||
for (let page = 0; page < numberOfPage; page++) {
|
|
||||||
blogMetadata.push({
|
|
||||||
permalink: normalizeUrl([
|
|
||||||
basePageUrl,
|
|
||||||
`${page > 0 ? `page${page + 1}` : ''}`,
|
|
||||||
]),
|
|
||||||
language: defaultLangTag,
|
|
||||||
isBlogPage: true,
|
|
||||||
posts: blogMetadata.slice(page * pageCount, (page + 1) * pageCount),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
async load() {
|
||||||
contents: blogMetadata,
|
const {pageCount, path: filePath, include, routeBasePath} = this.options;
|
||||||
options,
|
const {env, siteConfig, siteDir} = this.context;
|
||||||
};
|
const blogDir = path.resolve(siteDir, filePath);
|
||||||
|
const {baseUrl} = siteConfig;
|
||||||
|
|
||||||
|
const blogFiles = await globby(include, {
|
||||||
|
cwd: blogDir,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Prepare metadata container.
|
||||||
|
const blogMetadata = [];
|
||||||
|
|
||||||
|
// Language for each blog page.
|
||||||
|
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
blogFiles.map(async relativeSource => {
|
||||||
|
const source = path.join(blogDir, relativeSource);
|
||||||
|
|
||||||
|
const blogFileName = path.basename(relativeSource);
|
||||||
|
// Extract, YYYY, MM, DD from the file name.
|
||||||
|
const filePathDateArr = blogFileName.split('-');
|
||||||
|
const date = new Date(
|
||||||
|
`${filePathDateArr[0]}-${filePathDateArr[1]}-${
|
||||||
|
filePathDateArr[2]
|
||||||
|
}T06:00:00.000Z`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const fileString = await fs.readFile(source, 'utf-8');
|
||||||
|
const {metadata: rawMetadata} = parse(fileString);
|
||||||
|
const metadata = {
|
||||||
|
permalink: normalizeUrl([
|
||||||
|
baseUrl,
|
||||||
|
routeBasePath,
|
||||||
|
fileToUrl(blogFileName),
|
||||||
|
]),
|
||||||
|
source,
|
||||||
|
...rawMetadata,
|
||||||
|
date,
|
||||||
|
language: defaultLangTag,
|
||||||
|
};
|
||||||
|
blogMetadata.push(metadata);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
blogMetadata.sort((a, b) => a.date - b.date);
|
||||||
|
|
||||||
|
// Blog page handling. Example: `/blog`, `/blog/page1`, `/blog/page2`
|
||||||
|
const numOfBlog = blogMetadata.length;
|
||||||
|
const numberOfPage = Math.ceil(numOfBlog / pageCount);
|
||||||
|
const basePageUrl = path.join(baseUrl, routeBasePath);
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
for (let page = 0; page < numberOfPage; page++) {
|
||||||
|
blogMetadata.push({
|
||||||
|
permalink: normalizeUrl([
|
||||||
|
basePageUrl,
|
||||||
|
`${page > 0 ? `page${page + 1}` : ''}`,
|
||||||
|
]),
|
||||||
|
language: defaultLangTag,
|
||||||
|
isBlogPage: true,
|
||||||
|
posts: blogMetadata.slice(page * pageCount, (page + 1) * pageCount),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return blogMetadata;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = DocusaurusContentBlogPlugin;
|
||||||
onLoadContent,
|
|
||||||
};
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue