mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 23:57:22 +02:00
migrate useful helper functions to docusaurus-utils
This commit is contained in:
parent
c964a1e3b6
commit
6b507630e3
7 changed files with 77 additions and 95 deletions
|
@ -8,7 +8,7 @@
|
||||||
import {PluginContext, UserPluginOptions} from '../types';
|
import {PluginContext, UserPluginOptions} from '../types';
|
||||||
import collectRedirects from '../collectRedirects';
|
import collectRedirects from '../collectRedirects';
|
||||||
import normalizePluginOptions from '../normalizePluginOptions';
|
import normalizePluginOptions from '../normalizePluginOptions';
|
||||||
import {removeTrailingSlash} from '../utils';
|
import {removeTrailingSlash} from '@docusaurus/utils';
|
||||||
|
|
||||||
function createTestPluginContext(
|
function createTestPluginContext(
|
||||||
options?: UserPluginOptions,
|
options?: UserPluginOptions,
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) 2017-present, Facebook, Inc.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {
|
|
||||||
addTrailingSlash,
|
|
||||||
removeTrailingSlash,
|
|
||||||
removeSuffix,
|
|
||||||
getFilePathForRoutePath,
|
|
||||||
} from '../utils';
|
|
||||||
|
|
||||||
describe('addTrailingSlash', () => {
|
|
||||||
test('should no-op', () => {
|
|
||||||
expect(addTrailingSlash('/abcd/')).toEqual('/abcd/');
|
|
||||||
});
|
|
||||||
test('should add /', () => {
|
|
||||||
expect(addTrailingSlash('/abcd')).toEqual('/abcd/');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('removeTrailingSlash', () => {
|
|
||||||
test('should no-op', () => {
|
|
||||||
expect(removeTrailingSlash('/abcd')).toEqual('/abcd');
|
|
||||||
});
|
|
||||||
test('should remove /', () => {
|
|
||||||
expect(removeTrailingSlash('/abcd/')).toEqual('/abcd');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('removeSuffix', () => {
|
|
||||||
test('should no-op 1', () => {
|
|
||||||
expect(removeSuffix('abcdef', 'ijk')).toEqual('abcdef');
|
|
||||||
});
|
|
||||||
test('should no-op 2', () => {
|
|
||||||
expect(removeSuffix('abcdef', 'abc')).toEqual('abcdef');
|
|
||||||
});
|
|
||||||
test('should no-op 3', () => {
|
|
||||||
expect(removeSuffix('abcdef', '')).toEqual('abcdef');
|
|
||||||
});
|
|
||||||
test('should remove suffix', () => {
|
|
||||||
expect(removeSuffix('abcdef', 'ef')).toEqual('abcd');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('getFilePathForRoutePath', () => {
|
|
||||||
test('works for /', () => {
|
|
||||||
expect(getFilePathForRoutePath('/')).toEqual('/index.html');
|
|
||||||
});
|
|
||||||
test('works for /somePath', () => {
|
|
||||||
expect(getFilePathForRoutePath('/somePath')).toEqual(
|
|
||||||
'/somePath/index.html',
|
|
||||||
);
|
|
||||||
});
|
|
||||||
test('works for /somePath/', () => {
|
|
||||||
expect(getFilePathForRoutePath('/somePath/')).toEqual(
|
|
||||||
'/somePath/index.html',
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {flatten} from 'lodash';
|
import {flatten} from 'lodash';
|
||||||
import {removeSuffix} from './utils';
|
import {removeSuffix} from '@docusaurus/utils';
|
||||||
import {RedirectMetadata} from './types';
|
import {RedirectMetadata} from './types';
|
||||||
|
|
||||||
const ExtensionAdditionalMessage =
|
const ExtensionAdditionalMessage =
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
/**
|
|
||||||
* 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 addTrailingSlash(str: string) {
|
|
||||||
return str.endsWith('/') ? str : `${str}/`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function removeTrailingSlash(str: string) {
|
|
||||||
return removeSuffix(str, '/');
|
|
||||||
}
|
|
||||||
|
|
||||||
export function removeSuffix(str: string, suffix: string) {
|
|
||||||
if (suffix === '') {
|
|
||||||
return str; // always returns "" otherwise!
|
|
||||||
}
|
|
||||||
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO does this function already exist?
|
|
||||||
export function getFilePathForRoutePath(routePath: string) {
|
|
||||||
const fileName = path.basename(routePath);
|
|
||||||
const filePath = path.dirname(routePath);
|
|
||||||
return path.join(filePath, `${fileName}/index.html`);
|
|
||||||
}
|
|
|
@ -15,7 +15,7 @@ import {
|
||||||
addTrailingSlash,
|
addTrailingSlash,
|
||||||
getFilePathForRoutePath,
|
getFilePathForRoutePath,
|
||||||
removeTrailingSlash,
|
removeTrailingSlash,
|
||||||
} from './utils';
|
} from '@docusaurus/utils';
|
||||||
|
|
||||||
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
|
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,10 @@ import {
|
||||||
aliasedSitePath,
|
aliasedSitePath,
|
||||||
createExcerpt,
|
createExcerpt,
|
||||||
isValidPathname,
|
isValidPathname,
|
||||||
|
addTrailingSlash,
|
||||||
|
removeTrailingSlash,
|
||||||
|
removeSuffix,
|
||||||
|
getFilePathForRoutePath,
|
||||||
} from '../index';
|
} from '../index';
|
||||||
|
|
||||||
describe('load utils', () => {
|
describe('load utils', () => {
|
||||||
|
@ -381,3 +385,52 @@ describe('load utils', () => {
|
||||||
expect(isValidPathname('//hey')).toBe(false);
|
expect(isValidPathname('//hey')).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('addTrailingSlash', () => {
|
||||||
|
test('should no-op', () => {
|
||||||
|
expect(addTrailingSlash('/abcd/')).toEqual('/abcd/');
|
||||||
|
});
|
||||||
|
test('should add /', () => {
|
||||||
|
expect(addTrailingSlash('/abcd')).toEqual('/abcd/');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('removeTrailingSlash', () => {
|
||||||
|
test('should no-op', () => {
|
||||||
|
expect(removeTrailingSlash('/abcd')).toEqual('/abcd');
|
||||||
|
});
|
||||||
|
test('should remove /', () => {
|
||||||
|
expect(removeTrailingSlash('/abcd/')).toEqual('/abcd');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('removeSuffix', () => {
|
||||||
|
test('should no-op 1', () => {
|
||||||
|
expect(removeSuffix('abcdef', 'ijk')).toEqual('abcdef');
|
||||||
|
});
|
||||||
|
test('should no-op 2', () => {
|
||||||
|
expect(removeSuffix('abcdef', 'abc')).toEqual('abcdef');
|
||||||
|
});
|
||||||
|
test('should no-op 3', () => {
|
||||||
|
expect(removeSuffix('abcdef', '')).toEqual('abcdef');
|
||||||
|
});
|
||||||
|
test('should remove suffix', () => {
|
||||||
|
expect(removeSuffix('abcdef', 'ef')).toEqual('abcd');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getFilePathForRoutePath', () => {
|
||||||
|
test('works for /', () => {
|
||||||
|
expect(getFilePathForRoutePath('/')).toEqual('/index.html');
|
||||||
|
});
|
||||||
|
test('works for /somePath', () => {
|
||||||
|
expect(getFilePathForRoutePath('/somePath')).toEqual(
|
||||||
|
'/somePath/index.html',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
test('works for /somePath/', () => {
|
||||||
|
expect(getFilePathForRoutePath('/somePath/')).toEqual(
|
||||||
|
'/somePath/index.html',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -339,3 +339,24 @@ export function isValidPathname(str: string): boolean {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function addTrailingSlash(str: string) {
|
||||||
|
return str.endsWith('/') ? str : `${str}/`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeTrailingSlash(str: string) {
|
||||||
|
return removeSuffix(str, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeSuffix(str: string, suffix: string) {
|
||||||
|
if (suffix === '') {
|
||||||
|
return str; // always returns "" otherwise!
|
||||||
|
}
|
||||||
|
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getFilePathForRoutePath(routePath: string) {
|
||||||
|
const fileName = path.basename(routePath);
|
||||||
|
const filePath = path.dirname(routePath);
|
||||||
|
return path.join(filePath, `${fileName}/index.html`);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue