From 92c7e1f44bef8b1e620540bc0054efbab23faae6 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Fri, 1 Mar 2019 22:44:34 -0800 Subject: [PATCH] refactor(v2): convert plugins into a class --- v2/lib/load/index.js | 15 ++- v2/plugins/docusaurus-content-blog.js | 144 +++++++++++++------------- 2 files changed, 83 insertions(+), 76 deletions(-) diff --git a/v2/lib/load/index.js b/v2/lib/load/index.js index 77b6c5f8c2..621b2b4b5f 100644 --- a/v2/lib/load/index.js +++ b/v2/lib/load/index.js @@ -84,14 +84,21 @@ module.exports = async function load(siteDir) { // Process plugins. if (siteConfig.plugins) { 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( siteConfig.plugins.map(async ({name, options: opts}) => { // TODO: Resolve using node_modules as well. // eslint-disable-next-line - const plugin = require(path.resolve(__dirname, '../../plugins', name)); - const pluginContent = await plugin.onLoadContent(opts, context); - const {options, contents} = pluginContent; - contentsStore[options.contentKey] = pluginContent; + const Plugin = require(path.resolve(__dirname, '../../plugins', name)); + const plugin = new Plugin(opts, context); + const {options} = plugin; + const contents = await plugin.load(); + const pluginContents = { + options, + contents, + }; + contentsStore[options.contentKey] = pluginContents; await generate( generatedFilesDir, options.cachePath, diff --git a/v2/plugins/docusaurus-content-blog.js b/v2/plugins/docusaurus-content-blog.js index f34ec290b8..4ee41d2a7b 100644 --- a/v2/plugins/docusaurus-content-blog.js +++ b/v2/plugins/docusaurus-content-blog.js @@ -28,79 +28,79 @@ const DEFAULT_OPTIONS = { cachePath: 'blogMetadata.js', }; -async function onLoadContent(opts, context) { - const options = {...DEFAULT_OPTIONS, ...opts}; - - const {env, siteConfig, siteDir} = 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), - }); +class DocusaurusContentBlogPlugin { + constructor(opts, context) { + this.options = {...DEFAULT_OPTIONS, ...opts}; + this.context = context; } - return { - contents: blogMetadata, - options, - }; + async load() { + const {pageCount, path: filePath, include, routeBasePath} = this.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 = { - onLoadContent, -}; +module.exports = DocusaurusContentBlogPlugin;