Skip to content

Commit

Permalink
refactor(v2): create a removePrefix method
Browse files Browse the repository at this point in the history
  • Loading branch information
teikjun committed Jun 29, 2020
1 parent fa44be7 commit 1e54f13
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', () => {
Expand Down
9 changes: 4 additions & 5 deletions packages/docusaurus-plugin-client-redirects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import writeRedirectFiles, {
toRedirectFilesMetadata,
RedirectFileMetadata,
} from './writeRedirectFiles';
import {removePrefix} from '@docusaurus/utils';

export default function pluginClientRedirectsPages(
_context: LoadContext,
Expand All @@ -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,
Expand All @@ -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)}`);
}
16 changes: 16 additions & 0 deletions packages/docusaurus-utils/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
addTrailingSlash,
removeTrailingSlash,
removeSuffix,
removePrefix,
getFilePathForRoutePath,
} from '../index';

Expand Down Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1e54f13

Please sign in to comment.