mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-30 09:27:04 +02:00
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:
parent
2bbfbf88d6
commit
c23f981f67
11 changed files with 94 additions and 50 deletions
46
packages/docusaurus-plugin-sitemap/src/index.ts
Normal file
46
packages/docusaurus-plugin-sitemap/src/index.ts
Normal 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}`);
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue