refactor(v2): Convert sitemap plugin to TypeScript (#1894)

* Convert sitemap plugin to TypeScript

Test - enabled the sitemap plugin in the v2 website and verified that
the sitemap is created after running `docusaurus build`.

* Addressing review comments
This commit is contained in:
Pawel Kadluczka 2019-10-27 00:44:53 -07:00 committed by Endi
parent 2bbfbf88d6
commit c23f981f67
11 changed files with 94 additions and 50 deletions

View file

@ -0,0 +1,46 @@
/**
* 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 fs from 'fs';
import path from 'path';
import {PluginOptions} from './types';
import createSitemap from './createSitemap';
import {LoadContext, Props} from '@docusaurus/types';
const DEFAULT_OPTIONS: PluginOptions = {
cacheTime: 600 * 1000, // 600 sec - cache purge period
changefreq: 'weekly',
priority: 0.5,
};
export default function pluginSitemap(
_context: LoadContext,
opts: Partial<PluginOptions>,
) {
const options = {...DEFAULT_OPTIONS, ...opts};
return {
name: 'docusaurus-plugin-sitemap',
async postBuild({siteConfig, routesPaths, outDir}: Props) {
// Generate sitemap
const generatedSitemap = createSitemap(
siteConfig,
routesPaths,
options,
).toString();
// Write sitemap file
const sitemapPath = path.join(outDir, 'sitemap.xml');
fs.writeFile(sitemapPath, generatedSitemap, err => {
if (err) {
throw new Error(`Sitemap error: ${err}`);
}
});
},
};
}