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:
Sébastien Lorber 2021-06-14 20:04:39 +02:00 committed by GitHub
parent 4e5f0febb9
commit aeb8e9da51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 127 additions and 35 deletions

View file

@ -0,0 +1,3 @@
# `@docusaurus/utils`
Common (Node/Browser) utility functions for Docusaurus packages.

View 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"
}
}

View file

@ -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');
});
});

View 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);
}

View 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';

View file

@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib",
"noEmitHelpers": false
}
}