feat(v2): postBuild() - plugin lifecycle (#1302)

* feat(v2): build() - plugin lifecycle

* rename to postBuild
This commit is contained in:
Endilie Yacop Sucipto 2019-03-25 14:30:42 +07:00 committed by GitHub
parent c5d8e2d641
commit 72242dddf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 145 additions and 94 deletions

View file

@ -0,0 +1,35 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import loadSetup from '../../docusaurus/test/loadSetup';
import DocusaurusPluginSitemap from '../index';
describe('docusaurus-plugin-sitemap', () => {
describe('createSitemap', () => {
test.each(['simple', 'versioned', 'translated', 'transversioned'])(
'%s website',
async type => {
const props = await loadSetup(type);
const plugin = new DocusaurusPluginSitemap(null, props);
const sitemap = await plugin.createSitemap(props);
expect(sitemap).toContain(
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">`,
);
},
);
test('empty site', async () => {
const props = await loadSetup('empty');
const plugin = new DocusaurusPluginSitemap(null, props);
expect(
plugin.createSitemap(props),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Url in docusaurus.config.js cannot be empty/undefined"`,
);
});
});
});

View file

@ -0,0 +1,62 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs');
const sitemap = require('sitemap');
const path = require('path');
const DEFAULT_OPTIONS = {
cacheTime: 600 * 1000, // 600 sec - cache purge period
changefreq: 'weekly',
priority: 0.5,
};
class DocusaurusPluginSitemap {
constructor(opts, context) {
this.options = {...DEFAULT_OPTIONS, ...opts};
this.context = context;
}
getName() {
return 'docusaurus-plugin-sitemap';
}
async createSitemap({siteConfig = {}, routesPaths}) {
const {url: hostname} = siteConfig;
if (!hostname) {
throw new Error(`Url in docusaurus.config.js cannot be empty/undefined`);
}
const urls = routesPaths.map(routesPath => ({
url: routesPath,
changefreq: this.changefreq,
priority: this.priority,
}));
return sitemap
.createSitemap({
hostname,
cacheTime: this.cacheTime,
urls,
})
.toString();
}
async postBuild({siteConfig = {}, routesPaths = [], outDir}) {
// Generate sitemap
const generatedSitemap = await this.createSitemap({
siteConfig,
routesPaths,
});
// Write sitemap file
const sitemapPath = path.join(outDir, 'sitemap.xml');
return fs.writeFile(sitemapPath, generatedSitemap);
}
}
module.exports = DocusaurusPluginSitemap;

View file

@ -0,0 +1,10 @@
{
"name": "@docusaurus/plugin-sitemap",
"version": "1.0.0",
"description": "Simple sitemap generation plugin for Docusaurus",
"main": "index.js",
"license": "MIT",
"dependencies": {
"sitemap": "^2.1.0"
}
}