mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 05:57:05 +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
3
packages/docusaurus-utils-common/README.md
Normal file
3
packages/docusaurus-utils-common/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# `@docusaurus/utils`
|
||||
|
||||
Common (Node/Browser) utility functions for Docusaurus packages.
|
27
packages/docusaurus-utils-common/package.json
Normal file
27
packages/docusaurus-utils-common/package.json
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"name": "@docusaurus/utils-common",
|
||||
"version": "2.0.0-beta.0",
|
||||
"description": "Common (Node/Browser) utility functions for Docusaurus packages.",
|
||||
"main": "./lib/index.js",
|
||||
"types": "./lib/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"watch": "tsc --watch"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/facebook/docusaurus.git",
|
||||
"directory": "packages/docusaurus-utils-common"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@docusaurus/types": "2.0.0-beta.0",
|
||||
"tslib": "^2.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.13.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import applyTrailingSlash from '../applyTrailingSlash';
|
||||
|
||||
describe('applyTrailingSlash', () => {
|
||||
test('should apply to empty', () => {
|
||||
expect(applyTrailingSlash('', true)).toEqual('/');
|
||||
expect(applyTrailingSlash('', false)).toEqual('');
|
||||
expect(applyTrailingSlash('', undefined)).toEqual('');
|
||||
});
|
||||
|
||||
test('should apply to /', () => {
|
||||
expect(applyTrailingSlash('/', true)).toEqual('/');
|
||||
expect(applyTrailingSlash('/', false)).toEqual('');
|
||||
expect(applyTrailingSlash('/', undefined)).toEqual('/');
|
||||
});
|
||||
|
||||
test('should not apply to #anchor links ', () => {
|
||||
expect(applyTrailingSlash('#', true)).toEqual('#');
|
||||
expect(applyTrailingSlash('#', false)).toEqual('#');
|
||||
expect(applyTrailingSlash('#', undefined)).toEqual('#');
|
||||
expect(applyTrailingSlash('#anchor', true)).toEqual('#anchor');
|
||||
expect(applyTrailingSlash('#anchor', false)).toEqual('#anchor');
|
||||
expect(applyTrailingSlash('#anchor', undefined)).toEqual('#anchor');
|
||||
});
|
||||
|
||||
test('should apply to simple paths', () => {
|
||||
expect(applyTrailingSlash('abc', true)).toEqual('abc/');
|
||||
expect(applyTrailingSlash('abc', false)).toEqual('abc');
|
||||
expect(applyTrailingSlash('abc', undefined)).toEqual('abc');
|
||||
expect(applyTrailingSlash('abc/', true)).toEqual('abc/');
|
||||
expect(applyTrailingSlash('abc/', false)).toEqual('abc');
|
||||
expect(applyTrailingSlash('abc/', undefined)).toEqual('abc/');
|
||||
expect(applyTrailingSlash('/abc', true)).toEqual('/abc/');
|
||||
expect(applyTrailingSlash('/abc', false)).toEqual('/abc');
|
||||
expect(applyTrailingSlash('/abc', undefined)).toEqual('/abc');
|
||||
expect(applyTrailingSlash('/abc/', true)).toEqual('/abc/');
|
||||
expect(applyTrailingSlash('/abc/', false)).toEqual('/abc');
|
||||
expect(applyTrailingSlash('/abc/', undefined)).toEqual('/abc/');
|
||||
});
|
||||
|
||||
test('should apply to path with #anchor', () => {
|
||||
expect(applyTrailingSlash('/abc#anchor', true)).toEqual('/abc/#anchor');
|
||||
expect(applyTrailingSlash('/abc#anchor', false)).toEqual('/abc#anchor');
|
||||
expect(applyTrailingSlash('/abc#anchor', undefined)).toEqual('/abc#anchor');
|
||||
expect(applyTrailingSlash('/abc/#anchor', true)).toEqual('/abc/#anchor');
|
||||
expect(applyTrailingSlash('/abc/#anchor', false)).toEqual('/abc#anchor');
|
||||
expect(applyTrailingSlash('/abc/#anchor', undefined)).toEqual(
|
||||
'/abc/#anchor',
|
||||
);
|
||||
});
|
||||
|
||||
test('should apply to path with ?search', () => {
|
||||
expect(applyTrailingSlash('/abc?search', true)).toEqual('/abc/?search');
|
||||
expect(applyTrailingSlash('/abc?search', false)).toEqual('/abc?search');
|
||||
expect(applyTrailingSlash('/abc?search', undefined)).toEqual('/abc?search');
|
||||
expect(applyTrailingSlash('/abc/?search', true)).toEqual('/abc/?search');
|
||||
expect(applyTrailingSlash('/abc/?search', false)).toEqual('/abc?search');
|
||||
expect(applyTrailingSlash('/abc/?search', undefined)).toEqual(
|
||||
'/abc/?search',
|
||||
);
|
||||
});
|
||||
|
||||
test('should apply to path with ?search#anchor', () => {
|
||||
expect(applyTrailingSlash('/abc?search#anchor', true)).toEqual(
|
||||
'/abc/?search#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/abc?search#anchor', false)).toEqual(
|
||||
'/abc?search#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/abc?search#anchor', undefined)).toEqual(
|
||||
'/abc?search#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/abc/?search#anchor', true)).toEqual(
|
||||
'/abc/?search#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/abc/?search#anchor', false)).toEqual(
|
||||
'/abc?search#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/abc/?search#anchor', undefined)).toEqual(
|
||||
'/abc/?search#anchor',
|
||||
);
|
||||
});
|
||||
|
||||
test('should apply to fully qualified urls', () => {
|
||||
expect(
|
||||
applyTrailingSlash('https://xyz.com/abc?search#anchor', true),
|
||||
).toEqual('https://xyz.com/abc/?search#anchor');
|
||||
expect(
|
||||
applyTrailingSlash('https://xyz.com/abc?search#anchor', false),
|
||||
).toEqual('https://xyz.com/abc?search#anchor');
|
||||
expect(
|
||||
applyTrailingSlash('https://xyz.com/abc?search#anchor', undefined),
|
||||
).toEqual('https://xyz.com/abc?search#anchor');
|
||||
expect(
|
||||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', true),
|
||||
).toEqual('https://xyz.com/abc/?search#anchor');
|
||||
expect(
|
||||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', false),
|
||||
).toEqual('https://xyz.com/abc?search#anchor');
|
||||
expect(
|
||||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', undefined),
|
||||
).toEqual('https://xyz.com/abc/?search#anchor');
|
||||
});
|
||||
});
|
36
packages/docusaurus-utils-common/src/applyTrailingSlash.tsx
Normal file
36
packages/docusaurus-utils-common/src/applyTrailingSlash.tsx
Normal file
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
export default function applyTrailingSlash(
|
||||
path: string,
|
||||
trailingSlash: boolean | undefined,
|
||||
): string {
|
||||
// Never apply trailing slash to an anchor link
|
||||
if (path.startsWith('#')) {
|
||||
return path;
|
||||
}
|
||||
|
||||
// TODO deduplicate: also present in @docusaurus/utils
|
||||
function addTrailingSlash(str: string): string {
|
||||
return str.endsWith('/') ? str : `${str}/`;
|
||||
}
|
||||
function removeTrailingSlash(str: string): string {
|
||||
return str.endsWith('/') ? str.slice(0, -1) : str;
|
||||
}
|
||||
|
||||
// undefined = legacy retrocompatible behavior
|
||||
if (typeof trailingSlash === 'undefined') {
|
||||
return path;
|
||||
}
|
||||
|
||||
// The trailing slash should be handled before the ?search#hash !
|
||||
const [pathname] = path.split(/[#?]/);
|
||||
const newPathname = trailingSlash
|
||||
? addTrailingSlash(pathname)
|
||||
: removeTrailingSlash(pathname);
|
||||
return path.replace(pathname, newPathname);
|
||||
}
|
8
packages/docusaurus-utils-common/src/index.ts
Normal file
8
packages/docusaurus-utils-common/src/index.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
export {default as applyTrailingSlash} from './applyTrailingSlash';
|
10
packages/docusaurus-utils-common/tsconfig.json
Normal file
10
packages/docusaurus-utils-common/tsconfig.json
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"incremental": true,
|
||||
"tsBuildInfoFile": "./lib/.tsbuildinfo",
|
||||
"rootDir": "src",
|
||||
"outDir": "lib",
|
||||
"noEmitHelpers": false
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue