mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 00:27:21 +02:00
fix(v2): redirect plugin: use siteConfig.trailingSlash (#4988)
This commit is contained in:
parent
80b6d9728e
commit
b54ec72389
15 changed files with 443 additions and 73 deletions
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* 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 {getFilePathForRoutePath} from '../getFilePathForRoutePath';
|
||||
import {posixPath} from '../posixPath';
|
||||
|
||||
describe('getFilePathForRoutePath trailingSlash=undefined', () => {
|
||||
test('works for /', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/', undefined))).toEqual(
|
||||
'/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath', undefined))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath/', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath/', undefined))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath/xyz.html', () => {
|
||||
expect(
|
||||
posixPath(getFilePathForRoutePath('/somePath/xyz.html', undefined)),
|
||||
).toEqual('/somePath/xyz.html');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFilePathForRoutePath trailingSlash=true', () => {
|
||||
test('works for /', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/', true))).toEqual(
|
||||
'/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath', true))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath/', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath/', true))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath/xyz.html', () => {
|
||||
expect(
|
||||
posixPath(getFilePathForRoutePath('/somePath/xyz.html', true)),
|
||||
).toEqual('/somePath/xyz.html');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFilePathForRoutePath trailingSlash=false', () => {
|
||||
test('works for /', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/', false))).toEqual(
|
||||
'/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath', false))).toEqual(
|
||||
'/somePath.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath/', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath/', false))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
|
||||
test('works for /somePath/xyz.html', () => {
|
||||
expect(
|
||||
posixPath(getFilePathForRoutePath('/somePath/xyz.html', false)),
|
||||
).toEqual('/somePath/xyz.html');
|
||||
});
|
||||
});
|
|
@ -21,7 +21,6 @@ import {
|
|||
removeTrailingSlash,
|
||||
removeSuffix,
|
||||
removePrefix,
|
||||
getFilePathForRoutePath,
|
||||
addLeadingSlash,
|
||||
getElementsAround,
|
||||
mergeTranslations,
|
||||
|
@ -401,22 +400,6 @@ describe('removePrefix', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('getFilePathForRoutePath', () => {
|
||||
test('works for /', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/'))).toEqual('/index.html');
|
||||
});
|
||||
test('works for /somePath', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath'))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
test('works for /somePath/', () => {
|
||||
expect(posixPath(getFilePathForRoutePath('/somePath/'))).toEqual(
|
||||
'/somePath/index.html',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getElementsAround', () => {
|
||||
test('can return elements around', () => {
|
||||
expect(getElementsAround(['a', 'b', 'c', 'd'], 0)).toEqual({
|
||||
|
|
43
packages/docusaurus-utils/src/getFilePathForRoutePath.ts
Normal file
43
packages/docusaurus-utils/src/getFilePathForRoutePath.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* 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 path from 'path';
|
||||
|
||||
/*
|
||||
export function getFilePathForRoutePath(routePath: string): string {
|
||||
const fileName = path.basename(routePath);
|
||||
const filePath = path.dirname(routePath);
|
||||
return path.join(filePath, `${fileName}/index.html`);
|
||||
}
|
||||
*/
|
||||
|
||||
// Almost exact copy of the behavior we implemented in our Docusaurus fork of the webpack static gen plugin
|
||||
// See https://github.com/slorber/static-site-generator-webpack-plugin/blob/master/index.js#L167
|
||||
export function getFilePathForRoutePath(
|
||||
routePath: string,
|
||||
trailingSlash: boolean | undefined,
|
||||
): string {
|
||||
// const outputFileName = routePath.replace(/^(\/|\\)/, ''); // Remove leading slashes for webpack-dev-server
|
||||
|
||||
// Paths ending with .html are left untouched
|
||||
if (/\.(html?)$/i.test(routePath)) {
|
||||
return routePath;
|
||||
}
|
||||
|
||||
// Legacy retro-compatible behavior
|
||||
if (typeof trailingSlash === 'undefined') {
|
||||
return path.join(routePath, 'index.html');
|
||||
}
|
||||
|
||||
// New behavior: we can say if we prefer file/folder output
|
||||
// Useful resource: https://github.com/slorber/trailing-slash-guide
|
||||
if (routePath === '' || routePath.endsWith('/') || trailingSlash) {
|
||||
return path.join(routePath, 'index.html');
|
||||
} else {
|
||||
return `${routePath}.html`;
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@ import {docuHash} from './docuHash';
|
|||
|
||||
export const posixPath = posixPathImport;
|
||||
|
||||
export * from './getFilePathForRoutePath';
|
||||
export * from './codeTranslationsUtils';
|
||||
export * from './markdownParser';
|
||||
export * from './markdownLinks';
|
||||
|
@ -325,12 +326,6 @@ export function removePrefix(str: string, prefix: string): string {
|
|||
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
|
||||
}
|
||||
|
||||
export function getFilePathForRoutePath(routePath: string): string {
|
||||
const fileName = path.basename(routePath);
|
||||
const filePath = path.dirname(routePath);
|
||||
return path.join(filePath, `${fileName}/index.html`);
|
||||
}
|
||||
|
||||
export function getElementsAround<T extends unknown>(
|
||||
array: T[],
|
||||
aroundIndex: number,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue