mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 03:12:35 +02:00
fix(v2): sitemap plugin should handle siteConfig.trailingSlash automatically (#4950)
* create new @docusaurus/utils-common and move applyTrailingSlash there * sitemap plugin should handle siteConfig.trailingSlash automatically * typo
This commit is contained in:
parent
4e5f0febb9
commit
aeb8e9da51
18 changed files with 127 additions and 35 deletions
|
@ -20,6 +20,7 @@
|
|||
"@docusaurus/core": "2.0.0-beta.0",
|
||||
"@docusaurus/types": "2.0.0-beta.0",
|
||||
"@docusaurus/utils": "2.0.0-beta.0",
|
||||
"@docusaurus/utils-common": "2.0.0-beta.0",
|
||||
"@docusaurus/utils-validation": "2.0.0-beta.0",
|
||||
"fs-extra": "^10.0.0",
|
||||
"sitemap": "^7.0.0",
|
||||
|
|
|
@ -30,8 +30,13 @@ describe('normalizeSitemapPluginOptions', () => {
|
|||
priority: 0.9,
|
||||
trailingSlash: false,
|
||||
};
|
||||
const {value} = await PluginOptionSchema.validate(userOptions);
|
||||
const {value, warning} = await PluginOptionSchema.validate(userOptions);
|
||||
expect(value).toEqual(userOptions);
|
||||
|
||||
expect(warning?.details?.length).toEqual(1);
|
||||
expect(warning?.details[0].message).toMatchInlineSnapshot(
|
||||
`"Option \\"trailingSlash\\" of the sitemap plugin is deprecated: Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should reject out-of-range priority inputs', () => {
|
||||
|
|
|
@ -9,6 +9,7 @@ import {SitemapStream, streamToPromise} from 'sitemap';
|
|||
import {PluginOptions} from './types';
|
||||
import {DocusaurusConfig} from '@docusaurus/types';
|
||||
import {addTrailingSlash} from '@docusaurus/utils';
|
||||
import {applyTrailingSlash} from '@docusaurus/utils-common';
|
||||
|
||||
export default async function createSitemap(
|
||||
siteConfig: DocusaurusConfig,
|
||||
|
@ -25,11 +26,21 @@ export default async function createSitemap(
|
|||
hostname,
|
||||
});
|
||||
|
||||
function applySitemapTrailingSlash(routePath: string): string {
|
||||
// kept for retrocompatibility
|
||||
// TODO remove deprecated trailingSlash option before 2022
|
||||
if (options.trailingSlash) {
|
||||
return addTrailingSlash(routePath);
|
||||
} else {
|
||||
return applyTrailingSlash(routePath, trailingSlash);
|
||||
}
|
||||
}
|
||||
|
||||
routesPaths
|
||||
.filter((route) => !route.endsWith('404.html'))
|
||||
.map((routePath) =>
|
||||
sitemapStream.write({
|
||||
url: trailingSlash ? addTrailingSlash(routePath) : routePath,
|
||||
url: applySitemapTrailingSlash(routePath),
|
||||
changefreq,
|
||||
priority,
|
||||
}),
|
||||
|
|
|
@ -25,5 +25,11 @@ export const PluginOptionSchema = Joi.object({
|
|||
.valid(...Object.values(EnumChangefreq))
|
||||
.default(DEFAULT_OPTIONS.changefreq),
|
||||
priority: Joi.number().min(0).max(1).default(DEFAULT_OPTIONS.priority),
|
||||
trailingSlash: Joi.bool().default(false),
|
||||
trailingSlash: Joi.bool().default(false).warning('deprecate.error', {
|
||||
msg:
|
||||
'Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it.',
|
||||
}),
|
||||
}).messages({
|
||||
'deprecate.error':
|
||||
'Option {#label} of the sitemap plugin is deprecated: {#msg}',
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue