diff --git a/.eslintignore b/.eslintignore index bda9362a870f..a8ca421a6ce2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -16,3 +16,4 @@ packages/docusaurus-init/lib/ packages/docusaurus-plugin-content-blog/lib/ packages/docusaurus-plugin-content-docs/lib/ packages/docusaurus-plugin-content-pages/lib/ +packages/docusaurus-plugin-sitemap/lib/ diff --git a/.gitignore b/.gitignore index 929727c2751f..e230c5dcf9a5 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ packages/docusaurus-init/lib/ packages/docusaurus-plugin-content-blog/lib/ packages/docusaurus-plugin-content-docs/lib/ packages/docusaurus-plugin-content-pages/lib/ +packages/docusaurus-plugin-sitemap/lib/ diff --git a/.prettierignore b/.prettierignore index 7499928e82e3..a74d4b9bb16f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -8,3 +8,4 @@ packages/docusaurus-init/lib/ packages/docusaurus-plugin-content-blog/lib/ packages/docusaurus-plugin-content-docs/lib/ packages/docusaurus-plugin-content-pages/lib/ +packages/docusaurus-plugin-sitemap/lib/ \ No newline at end of file diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index 98889efbae93..b6cec6facd92 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -2,6 +2,8 @@ ## Unreleased +- Convert sitemap plugin to TypeScript + ## 2.0.0-alpha.31 - Footer is now sticky/ pinned to the bottom of the viewport in desktop browsers. diff --git a/packages/docusaurus-plugin-sitemap/package.json b/packages/docusaurus-plugin-sitemap/package.json index af6761849d76..ee12645ea186 100644 --- a/packages/docusaurus-plugin-sitemap/package.json +++ b/packages/docusaurus-plugin-sitemap/package.json @@ -2,12 +2,16 @@ "name": "@docusaurus/plugin-sitemap", "version": "2.0.0-alpha.31", "description": "Simple sitemap generation plugin for Docusaurus", - "main": "src/index.js", + "main": "lib/index.js", + "scripts": { + "tsc": "tsc" + }, "publishConfig": { "access": "public" }, "license": "MIT", "dependencies": { + "@docusaurus/types": "^2.0.0-alpha.30", "sitemap": "^3.2.2" }, "peerDependencies": { diff --git a/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.js b/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts similarity index 70% rename from packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.js rename to packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts index f5cf466f0940..1f849716fc6b 100644 --- a/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.js +++ b/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts @@ -6,23 +6,30 @@ */ import createSitemap from '../createSitemap'; +import {DocusaurusConfig} from '@docusaurus/types'; +import DEFAULT_OPTIONS from '../index'; describe('createSitemap', () => { test('simple site', () => { - const sitemap = createSitemap({ - siteConfig: { + const sitemap = createSitemap( + { url: 'https://example.com', + } as DocusaurusConfig, + ['/', '/test'], + { + cacheTime: 600, + changefreq: 'daily', + priority: 0.7, }, - routesPaths: ['/', '/test'], - }); - expect(sitemap).toContain( + ); + expect(sitemap.toString()).toContain( ``, ); }); test('empty site', () => { expect(() => { - createSitemap({}); + createSitemap({} as any, [], {} as any); }).toThrowErrorMatchingInlineSnapshot( `"Url in docusaurus.config.js cannot be empty/undefined"`, ); diff --git a/packages/docusaurus-plugin-sitemap/src/createSitemap.js b/packages/docusaurus-plugin-sitemap/src/createSitemap.js deleted file mode 100644 index f9cc6318007e..000000000000 --- a/packages/docusaurus-plugin-sitemap/src/createSitemap.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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 sitemap = require('sitemap'); - -module.exports = function createSitemap({ - siteConfig = {}, - routesPaths, - options = {}, -}) { - 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: options.changefreq, - priority: options.priority, - })); - - return sitemap - .createSitemap({ - hostname, - cacheTime: options.cacheTime, - urls, - }) - .toString(); -}; diff --git a/packages/docusaurus-plugin-sitemap/src/createSitemap.ts b/packages/docusaurus-plugin-sitemap/src/createSitemap.ts new file mode 100644 index 000000000000..50aa2be44e7c --- /dev/null +++ b/packages/docusaurus-plugin-sitemap/src/createSitemap.ts @@ -0,0 +1,36 @@ +/** + * 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 sitemap, {SitemapItemOptions} from 'sitemap'; +import {PluginOptions} from './types'; +import {DocusaurusConfig} from '@docusaurus/types'; + +export default function createSitemap( + siteConfig: DocusaurusConfig, + routesPaths: string[], + options: PluginOptions, +) { + 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: options.changefreq, + priority: options.priority, + } as SitemapItemOptions), + ); + + return sitemap.createSitemap({ + hostname, + cacheTime: options.cacheTime, + urls, + }); +} diff --git a/packages/docusaurus-plugin-sitemap/src/index.js b/packages/docusaurus-plugin-sitemap/src/index.ts similarity index 61% rename from packages/docusaurus-plugin-sitemap/src/index.js rename to packages/docusaurus-plugin-sitemap/src/index.ts index eb43d8338480..0394d40eb08a 100644 --- a/packages/docusaurus-plugin-sitemap/src/index.js +++ b/packages/docusaurus-plugin-sitemap/src/index.ts @@ -5,30 +5,34 @@ * LICENSE file in the root directory of this source tree. */ -const fs = require('fs'); -const path = require('path'); +import fs from 'fs'; +import path from 'path'; +import {PluginOptions} from './types'; +import createSitemap from './createSitemap'; +import {LoadContext, Props} from '@docusaurus/types'; -const createSitemap = require('./createSitemap'); - -const DEFAULT_OPTIONS = { +const DEFAULT_OPTIONS: PluginOptions = { cacheTime: 600 * 1000, // 600 sec - cache purge period changefreq: 'weekly', priority: 0.5, }; -module.exports = function(context, opts) { +export default function pluginSitemap( + _context: LoadContext, + opts: Partial, +) { const options = {...DEFAULT_OPTIONS, ...opts}; return { name: 'docusaurus-plugin-sitemap', - async postBuild({siteConfig = {}, routesPaths = [], outDir}) { + async postBuild({siteConfig, routesPaths, outDir}: Props) { // Generate sitemap - const generatedSitemap = createSitemap({ + const generatedSitemap = createSitemap( siteConfig, routesPaths, options, - }).toString(); + ).toString(); // Write sitemap file const sitemapPath = path.join(outDir, 'sitemap.xml'); @@ -39,4 +43,4 @@ module.exports = function(context, opts) { }); }, }; -}; +} diff --git a/packages/docusaurus-plugin-sitemap/src/types.ts b/packages/docusaurus-plugin-sitemap/src/types.ts new file mode 100644 index 000000000000..418f15aa0f0a --- /dev/null +++ b/packages/docusaurus-plugin-sitemap/src/types.ts @@ -0,0 +1,12 @@ +/** + * 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. + */ + +export interface PluginOptions { + cacheTime: number; + changefreq: string; + priority: number; +} diff --git a/packages/docusaurus-plugin-sitemap/tsconfig.json b/packages/docusaurus-plugin-sitemap/tsconfig.json new file mode 100644 index 000000000000..f5902ba1089b --- /dev/null +++ b/packages/docusaurus-plugin-sitemap/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + } +}