From 70d05775f144a609ef2af39971c62cb14b9853ac Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 02:03:23 +0800 Subject: [PATCH 1/8] feat(v2): use relative routes path in postBuild hook --- .../src/collectRedirects.ts | 14 ++++++-------- .../src/index.ts | 8 +++++++- .../src/types.ts | 6 ++---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts index 73cf5f09833e..b83dea92b6da 100644 --- a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts @@ -50,9 +50,7 @@ function validateCollectedRedirects( ); } - const allowedToPaths = pluginContext.routesPaths.map((path) => - path.replace(pluginContext.baseUrl, '/'), - ); + const allowedToPaths = pluginContext.relativeRoutesPaths; const toPaths = redirects.map((redirect) => redirect.to); const illegalToPaths = difference(toPaths, allowedToPaths); if (illegalToPaths.length > 0) { @@ -91,7 +89,7 @@ It is not possible to redirect the same pathname to multiple destinations: // We don't want to override an already existing route with a redirect file! const redirectsOverridingExistingPath = redirects.filter((redirect) => - pluginContext.routesPaths.includes(redirect.from), + pluginContext.relativeRoutesPaths.includes(redirect.from), ); if (redirectsOverridingExistingPath.length > 0) { console.error( @@ -103,7 +101,7 @@ It is not possible to redirect the same pathname to multiple destinations: ); } redirects = redirects.filter( - (redirect) => !pluginContext.routesPaths.includes(redirect.from), + (redirect) => !pluginContext.relativeRoutesPaths.includes(redirect.from), ); return redirects; @@ -113,18 +111,18 @@ It is not possible to redirect the same pathname to multiple destinations: function doCollectRedirects(pluginContext: PluginContext): RedirectMetadata[] { return [ ...createFromExtensionsRedirects( - pluginContext.routesPaths, + pluginContext.relativeRoutesPaths, pluginContext.options.fromExtensions, pluginContext.baseUrl, ), ...createToExtensionsRedirects( - pluginContext.routesPaths, + pluginContext.relativeRoutesPaths, pluginContext.options.toExtensions, pluginContext.baseUrl, ), ...createRedirectsOptionRedirects(pluginContext.options.redirects), ...createCreateRedirectsOptionRedirects( - pluginContext.routesPaths, + pluginContext.relativeRoutesPaths, pluginContext.options.createRedirects, ), ]; diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index c94b734514e4..a81751e8a59f 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -25,7 +25,9 @@ export default function pluginClientRedirectsPages( name: 'docusaurus-plugin-client-redirects', async postBuild(props: Props) { const pluginContext: PluginContext = { - routesPaths: props.routesPaths, + relativeRoutesPaths: props.routesPaths.map((path) => + removePrefix(path, props.baseUrl), + ), baseUrl: props.baseUrl, outDir: props.outDir, options, @@ -43,3 +45,7 @@ export default function pluginClientRedirectsPages( }, }; } + +function removePrefix(str: string, prefix: string) { + return str.startsWith(prefix) ? str.slice(str.length) : str; +} diff --git a/packages/docusaurus-plugin-client-redirects/src/types.ts b/packages/docusaurus-plugin-client-redirects/src/types.ts index fe8b0296ed03..15c0987e0dd0 100644 --- a/packages/docusaurus-plugin-client-redirects/src/types.ts +++ b/packages/docusaurus-plugin-client-redirects/src/types.ts @@ -28,11 +28,9 @@ export type RedirectOption = { export type UserPluginOptions = Partial; // The minimal infos the plugin needs to work -export type PluginContext = Pick< - Props, - 'routesPaths' | 'outDir' | 'baseUrl' -> & { +export type PluginContext = Pick & { options: PluginOptions; + relativeRoutesPaths: string[]; }; // In-memory representation of redirects we want: easier to test From 7c1644cf05fc6bdbba6955021b82a02fdb91b057 Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 02:52:45 +0800 Subject: [PATCH 2/8] feat(v2): use relativeRoutesPath in other methods and modify tests --- .../src/__tests__/collectRedirects.test.ts | 4 +- .../src/__tests__/extensionRedirects.test.ts | 53 +++++++++---------- .../src/collectRedirects.ts | 2 - .../src/extensionRedirects.ts | 14 ++--- .../src/index.ts | 6 +-- 5 files changed, 35 insertions(+), 44 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts index f43e1d795542..8cf956a077c9 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -12,12 +12,12 @@ import {removeTrailingSlash} from '@docusaurus/utils'; function createTestPluginContext( options?: UserPluginOptions, - routesPaths: string[] = [], + relativeRoutesPaths: string[] = [], ): PluginContext { return { outDir: '/tmp', baseUrl: 'https://docusaurus.io', - routesPaths, + relativeRoutesPaths, options: normalizePluginOptions(options), }; } diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts index ce327688a22a..286df4303c6e 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts @@ -15,33 +15,32 @@ const createExtensionValidationTests = ( extensionRedirectCreatorFn: ( paths: string[], extensions: string[], - baseUrl: string, ) => RedirectMetadata[], ) => { test('should reject empty extensions', () => { expect(() => { - extensionRedirectCreatorFn(['/'], ['.html'], '/'); + extensionRedirectCreatorFn(['/'], ['.html']); }).toThrowErrorMatchingInlineSnapshot( `"Extension=['.html'] contains a . (dot) and is not allowed. If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the 'createRedirects' plugin option."`, ); }); test('should reject extensions with .', () => { expect(() => { - extensionRedirectCreatorFn(['/'], ['.html'], '/'); + extensionRedirectCreatorFn(['/'], ['.html']); }).toThrowErrorMatchingInlineSnapshot( `"Extension=['.html'] contains a . (dot) and is not allowed. If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the 'createRedirects' plugin option."`, ); }); test('should reject extensions with /', () => { expect(() => { - extensionRedirectCreatorFn(['/'], ['ht/ml'], '/'); + extensionRedirectCreatorFn(['/'], ['ht/ml']); }).toThrowErrorMatchingInlineSnapshot( `"Extension=['ht/ml'] contains a / and is not allowed. If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the 'createRedirects' plugin option."`, ); }); test('should reject extensions with illegal url char', () => { expect(() => { - extensionRedirectCreatorFn(['/'], [','], '/'); + extensionRedirectCreatorFn(['/'], [',']); }).toThrowErrorMatchingInlineSnapshot( `"Extension=[','] contains invalid uri characters. If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the 'createRedirects' plugin option."`, ); @@ -53,28 +52,28 @@ describe('createToExtensionsRedirects', () => { test('should create redirects from html/htm extensions', () => { const ext = ['html', 'htm']; - expect(createToExtensionsRedirects([''], ext, '/')).toEqual([]); - expect(createToExtensionsRedirects(['/'], ext, '/')).toEqual([]); - expect(createToExtensionsRedirects(['/abc.html'], ext, '/')).toEqual([ + expect(createToExtensionsRedirects([''], ext)).toEqual([]); + expect(createToExtensionsRedirects(['/'], ext)).toEqual([]); + expect(createToExtensionsRedirects(['/abc.html'], ext)).toEqual([ {from: '/abc', to: '/abc.html'}, ]); - expect(createToExtensionsRedirects(['/abc.htm'], ext, '/')).toEqual([ + expect(createToExtensionsRedirects(['/abc.htm'], ext)).toEqual([ {from: '/abc', to: '/abc.htm'}, ]); - expect(createToExtensionsRedirects(['/abc.xyz'], ext, '/')).toEqual([]); + expect(createToExtensionsRedirects(['/abc.xyz'], ext)).toEqual([]); }); test('should create "to" redirects without baseUrl when baseUrl is used', () => { expect( - createToExtensionsRedirects(['/prefix/file.html'], ['html'], '/prefix/'), + createToExtensionsRedirects(['/prefix/file.html'], ['html']), ).toEqual([{from: '/file', to: '/file.html'}]); }); test('should not create redirection for an empty extension array', () => { const ext: string[] = []; - expect(createToExtensionsRedirects([''], ext, '/')).toEqual([]); - expect(createToExtensionsRedirects(['/'], ext, '/')).toEqual([]); - expect(createToExtensionsRedirects(['/abc.html'], ext, '/')).toEqual([]); + expect(createToExtensionsRedirects([''], ext)).toEqual([]); + expect(createToExtensionsRedirects(['/'], ext)).toEqual([]); + expect(createToExtensionsRedirects(['/abc.html'], ext)).toEqual([]); }); }); @@ -83,28 +82,28 @@ describe('createFromExtensionsRedirects', () => { test('should create redirects to html/htm extensions', () => { const ext = ['html', 'htm']; - expect(createFromExtensionsRedirects([''], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/'], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/abc'], ext, '/')).toEqual([ + expect(createFromExtensionsRedirects([''], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/abc'], ext)).toEqual([ {from: '/abc.html', to: '/abc'}, {from: '/abc.htm', to: '/abc'}, ]); - expect(createFromExtensionsRedirects(['/def.html'], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/def/'], ext, '/')).toEqual([]); + expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]); }); test('should create "from" redirects without baseUrl when baseUrl is used', () => { - expect( - createFromExtensionsRedirects(['/prefix/file'], ['html'], '/prefix/'), - ).toEqual([{from: '/file.html', to: '/file'}]); + expect(createFromExtensionsRedirects(['/prefix/file'], ['html'])).toEqual([ + {from: '/file.html', to: '/file'}, + ]); }); test('should not create redirection for an empty extension array', () => { const ext: string[] = []; - expect(createFromExtensionsRedirects([''], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/'], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/abc'], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/def.html'], ext, '/')).toEqual([]); - expect(createFromExtensionsRedirects(['/def/'], ext, '/')).toEqual([]); + expect(createFromExtensionsRedirects([''], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/abc'], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]); + expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]); }); }); diff --git a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts index b83dea92b6da..1a2541897d90 100644 --- a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts @@ -113,12 +113,10 @@ function doCollectRedirects(pluginContext: PluginContext): RedirectMetadata[] { ...createFromExtensionsRedirects( pluginContext.relativeRoutesPaths, pluginContext.options.fromExtensions, - pluginContext.baseUrl, ), ...createToExtensionsRedirects( pluginContext.relativeRoutesPaths, pluginContext.options.toExtensions, - pluginContext.baseUrl, ), ...createRedirectsOptionRedirects(pluginContext.options.redirects), ...createCreateRedirectsOptionRedirects( diff --git a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts index ee43f329d7a7..9adebf43b437 100644 --- a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts @@ -43,7 +43,6 @@ const addLeadingDot = (extension: string) => `.${extension}`; export function createToExtensionsRedirects( paths: string[], extensions: string[], - baseUrl: string, ): RedirectMetadata[] { extensions.forEach(validateExtension); @@ -54,8 +53,8 @@ export function createToExtensionsRedirects( if (extensionFound) { const routePathWithoutExtension = removeSuffix(path, extensionFound); return [routePathWithoutExtension].map((from) => ({ - from: trimBaseUrl(from, baseUrl), - to: trimBaseUrl(path, baseUrl), + from, + to: path, })); } return []; @@ -68,7 +67,6 @@ export function createToExtensionsRedirects( export function createFromExtensionsRedirects( paths: string[], extensions: string[], - baseUrl: string, ): RedirectMetadata[] { extensions.forEach(validateExtension); @@ -82,14 +80,10 @@ export function createFromExtensionsRedirects( return []; } return extensions.map((ext) => ({ - from: `${trimBaseUrl(path, baseUrl)}.${ext}`, - to: trimBaseUrl(path, baseUrl), + from: `${path}.${ext}`, + to: path, })); }; return flatten(paths.map(createPathRedirects)); } - -function trimBaseUrl(path: string, baseUrl: string) { - return path.startsWith(baseUrl) ? path.replace(baseUrl, '/') : path; -} diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index a81751e8a59f..cd66ac14f543 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -26,7 +26,7 @@ export default function pluginClientRedirectsPages( async postBuild(props: Props) { const pluginContext: PluginContext = { relativeRoutesPaths: props.routesPaths.map((path) => - removePrefix(path, props.baseUrl), + trimBaseUrl(path, props.baseUrl), ), baseUrl: props.baseUrl, outDir: props.outDir, @@ -46,6 +46,6 @@ export default function pluginClientRedirectsPages( }; } -function removePrefix(str: string, prefix: string) { - return str.startsWith(prefix) ? str.slice(str.length) : str; +function trimBaseUrl(path: string, baseUrl: string) { + return path.startsWith(baseUrl) ? path.replace(baseUrl, '/') : path; } From 841ffd5c22b156dfad0a4c6025f1567b7235b2a8 Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 15:56:51 +0800 Subject: [PATCH 3/8] fix(v2): fix D2 client redirects and tests --- .../src/__tests__/extensionRedirects.test.ts | 8 ++++---- website/docusaurus.config.js | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts index 286df4303c6e..1bb284447e37 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts @@ -63,10 +63,10 @@ describe('createToExtensionsRedirects', () => { expect(createToExtensionsRedirects(['/abc.xyz'], ext)).toEqual([]); }); - test('should create "to" redirects without baseUrl when baseUrl is used', () => { + test('should create "to" redirects when relativeRoutesPath contains a prefix', () => { expect( createToExtensionsRedirects(['/prefix/file.html'], ['html']), - ).toEqual([{from: '/file', to: '/file.html'}]); + ).toEqual([{from: '/prefix/file', to: '/prefix/file.html'}]); }); test('should not create redirection for an empty extension array', () => { @@ -92,9 +92,9 @@ describe('createFromExtensionsRedirects', () => { expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]); }); - test('should create "from" redirects without baseUrl when baseUrl is used', () => { + test('should create "from" redirects when relativeRoutesPath contains a prefix', () => { expect(createFromExtensionsRedirects(['/prefix/file'], ['html'])).toEqual([ - {from: '/file.html', to: '/file'}, + {from: '/prefix/file.html', to: '/prefix/file'}, ]); }); diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 43c06f7943ef..1567e83c7f5d 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -8,9 +8,9 @@ const versions = require('./versions.json'); const allDocHomesPaths = [ - '/docs', - '/docs/next', - ...versions.slice(1).map((version) => `/docs/${version}`), + '/docs/', + '/docs/next/', + ...versions.slice(1).map((version) => `/docs/${version}/`), ]; module.exports = { From 0c05f594f24cd1dc94a9939b5b2d325a136f352f Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 18:13:07 +0800 Subject: [PATCH 4/8] feat(v2): add tests for relativeRoutesPaths --- .../src/__tests__/collectRedirects.test.ts | 9 +++++++++ packages/docusaurus-plugin-client-redirects/src/index.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts index 8cf956a077c9..6aeffa0841a4 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -9,6 +9,7 @@ import {PluginContext, UserPluginOptions} from '../types'; import collectRedirects from '../collectRedirects'; import normalizePluginOptions from '../normalizePluginOptions'; import {removeTrailingSlash} from '@docusaurus/utils'; +import {trimBaseUrl} from '..'; function createTestPluginContext( options?: UserPluginOptions, @@ -23,6 +24,14 @@ function createTestPluginContext( } describe('collectRedirects', () => { + test('should trim baseUrl properly to get correct relativeRoutesPath', () => { + const routePaths = ['/myBaseUrl/', '/myBaseUrl/path']; + expect(routePaths.map((path) => trimBaseUrl(path, '/myBaseUrl/'))).toEqual([ + '/', + '/path', + ]); + }); + test('should collect no redirect for undefined config', () => { expect( collectRedirects(createTestPluginContext(undefined, ['/', '/path'])), diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index cd66ac14f543..b21b99dac8ee 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -46,6 +46,6 @@ export default function pluginClientRedirectsPages( }; } -function trimBaseUrl(path: string, baseUrl: string) { +export function trimBaseUrl(path: string, baseUrl: string) { return path.startsWith(baseUrl) ? path.replace(baseUrl, '/') : path; } From eb848b1b4efab365a38e7e56da66892b2452fbf5 Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 18:16:09 +0800 Subject: [PATCH 5/8] fix(v2): fix some typos in configValidation --- .../docusaurus/src/server/configValidation.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index f745a35cfc35..c80525930f4e 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -90,23 +90,23 @@ export function validateConfig( abortEarly: false, }); if (error) { - const unknownFields = error.details.reduce((formatedError, err) => { + const unknownFields = error.details.reduce((formattedError, err) => { if (err.type === 'object.unknown') { - return `${formatedError}"${err.path}",`; + return `${formattedError}"${err.path}",`; } - return formatedError; + return formattedError; }, ''); - let formatedError = error.details.reduce( - (accumalatedErr, err) => + let formattedError = error.details.reduce( + (accumulatedErr, err) => err.type !== 'object.unknown' - ? `${accumalatedErr}${err.message}\n` - : accumalatedErr, + ? `${accumulatedErr}${err.message}\n` + : accumulatedErr, '', ); - formatedError = unknownFields - ? `${formatedError}These field(s) [${unknownFields}] are not recognized in ${CONFIG_FILE_NAME}.\nIf you still want these fields to be in your configuration, put them in the 'customFields' attribute.\nSee https://v2.docusaurus.io/docs/docusaurus.config.js/#customfields` - : formatedError; - throw new Error(formatedError); + formattedError = unknownFields + ? `${formattedError}These field(s) [${unknownFields}] are not recognized in ${CONFIG_FILE_NAME}.\nIf you still want these fields to be in your configuration, put them in the 'customFields' attribute.\nSee https://v2.docusaurus.io/docs/docusaurus.config.js/#customfields` + : formattedError; + throw new Error(formattedError); } else { return value; } From fa44be79d2e273de79311cde6b89d9f8bb832cac Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 20:32:45 +0800 Subject: [PATCH 6/8] fix(v2): fix an eslint warning and restart github action --- packages/docusaurus-plugin-client-redirects/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index b21b99dac8ee..5009a0643065 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -46,6 +46,6 @@ export default function pluginClientRedirectsPages( }; } -export function trimBaseUrl(path: string, baseUrl: string) { +export function trimBaseUrl(path: string, baseUrl: string): string { return path.startsWith(baseUrl) ? path.replace(baseUrl, '/') : path; } From 1e54f13235a7dcdfdd6b55218564bbe3a0c3045b Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 21:57:53 +0800 Subject: [PATCH 7/8] refactor(v2): create a removePrefix method --- .../src/__tests__/collectRedirects.test.ts | 7 ++----- .../src/index.ts | 9 ++++----- .../docusaurus-utils/src/__tests__/index.test.ts | 16 ++++++++++++++++ packages/docusaurus-utils/src/index.ts | 4 ++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts index 6aeffa0841a4..365964238bc4 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -9,7 +9,7 @@ import {PluginContext, UserPluginOptions} from '../types'; import collectRedirects from '../collectRedirects'; import normalizePluginOptions from '../normalizePluginOptions'; import {removeTrailingSlash} from '@docusaurus/utils'; -import {trimBaseUrl} from '..'; +import {trimBaseUrls} from '..'; function createTestPluginContext( options?: UserPluginOptions, @@ -26,10 +26,7 @@ function createTestPluginContext( describe('collectRedirects', () => { test('should trim baseUrl properly to get correct relativeRoutesPath', () => { const routePaths = ['/myBaseUrl/', '/myBaseUrl/path']; - expect(routePaths.map((path) => trimBaseUrl(path, '/myBaseUrl/'))).toEqual([ - '/', - '/path', - ]); + expect(trimBaseUrls(routePaths, '/myBaseUrl/')).toEqual(['/', '/path']); }); test('should collect no redirect for undefined config', () => { diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index 5009a0643065..276e5254b2c1 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -14,6 +14,7 @@ import writeRedirectFiles, { toRedirectFilesMetadata, RedirectFileMetadata, } from './writeRedirectFiles'; +import {removePrefix} from '@docusaurus/utils'; export default function pluginClientRedirectsPages( _context: LoadContext, @@ -25,9 +26,7 @@ export default function pluginClientRedirectsPages( name: 'docusaurus-plugin-client-redirects', async postBuild(props: Props) { const pluginContext: PluginContext = { - relativeRoutesPaths: props.routesPaths.map((path) => - trimBaseUrl(path, props.baseUrl), - ), + relativeRoutesPaths: trimBaseUrls(props.routesPaths, props.baseUrl), baseUrl: props.baseUrl, outDir: props.outDir, options, @@ -46,6 +45,6 @@ export default function pluginClientRedirectsPages( }; } -export function trimBaseUrl(path: string, baseUrl: string): string { - return path.startsWith(baseUrl) ? path.replace(baseUrl, '/') : path; +export function trimBaseUrls(paths: string[], baseUrl: string): string[] { + return paths.map((path) => `/${removePrefix(path, baseUrl)}`); } diff --git a/packages/docusaurus-utils/src/__tests__/index.test.ts b/packages/docusaurus-utils/src/__tests__/index.test.ts index 29cde6684ae4..23167e457351 100644 --- a/packages/docusaurus-utils/src/__tests__/index.test.ts +++ b/packages/docusaurus-utils/src/__tests__/index.test.ts @@ -22,6 +22,7 @@ import { addTrailingSlash, removeTrailingSlash, removeSuffix, + removePrefix, getFilePathForRoutePath, } from '../index'; @@ -419,6 +420,21 @@ describe('removeSuffix', () => { }); }); +describe('removePrefix', () => { + test('should no-op 1', () => { + expect(removePrefix('abcdef', 'ijk')).toEqual('abcdef'); + }); + test('should no-op 2', () => { + expect(removePrefix('abcdef', 'def')).toEqual('abcdef'); + }); + test('should no-op 3', () => { + expect(removePrefix('abcdef', '')).toEqual('abcdef'); + }); + test('should remove prefix', () => { + expect(removePrefix('abcdef', 'ab')).toEqual('cdef'); + }); +}); + describe('getFilePathForRoutePath', () => { test('works for /', () => { expect(getFilePathForRoutePath('/')).toEqual('/index.html'); diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 542b05ba2450..25acc1b709e8 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -384,6 +384,10 @@ export function removeSuffix(str: string, suffix: string): string { return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; } +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); From 727ebea5a693cda0d7e461c2ee5a64db82b5462a Mon Sep 17 00:00:00 2001 From: teikjun Date: Mon, 29 Jun 2020 22:36:09 +0800 Subject: [PATCH 8/8] refactor(v2): inline unnecessary method --- .../src/__tests__/collectRedirects.test.ts | 6 ------ packages/docusaurus-plugin-client-redirects/src/index.ts | 8 +++----- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts index 365964238bc4..8cf956a077c9 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -9,7 +9,6 @@ import {PluginContext, UserPluginOptions} from '../types'; import collectRedirects from '../collectRedirects'; import normalizePluginOptions from '../normalizePluginOptions'; import {removeTrailingSlash} from '@docusaurus/utils'; -import {trimBaseUrls} from '..'; function createTestPluginContext( options?: UserPluginOptions, @@ -24,11 +23,6 @@ function createTestPluginContext( } describe('collectRedirects', () => { - test('should trim baseUrl properly to get correct relativeRoutesPath', () => { - const routePaths = ['/myBaseUrl/', '/myBaseUrl/path']; - expect(trimBaseUrls(routePaths, '/myBaseUrl/')).toEqual(['/', '/path']); - }); - test('should collect no redirect for undefined config', () => { expect( collectRedirects(createTestPluginContext(undefined, ['/', '/path'])), diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index 276e5254b2c1..ef630903c8c7 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -26,7 +26,9 @@ export default function pluginClientRedirectsPages( name: 'docusaurus-plugin-client-redirects', async postBuild(props: Props) { const pluginContext: PluginContext = { - relativeRoutesPaths: trimBaseUrls(props.routesPaths, props.baseUrl), + relativeRoutesPaths: props.routesPaths.map( + (path) => `/${removePrefix(path, props.baseUrl)}`, + ), baseUrl: props.baseUrl, outDir: props.outDir, options, @@ -44,7 +46,3 @@ export default function pluginClientRedirectsPages( }, }; } - -export function trimBaseUrls(paths: string[], baseUrl: string): string[] { - return paths.map((path) => `/${removePrefix(path, baseUrl)}`); -}