docusaurus/packages/docusaurus-plugin-sitemap/index.js
Endilie Yacop Sucipto 72242dddf4
feat(v2): postBuild() - plugin lifecycle (#1302)
* feat(v2): build() - plugin lifecycle

* rename to postBuild
2019-03-25 14:30:42 +07:00

62 lines
1.5 KiB
JavaScript

/**
* 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;