mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
misc(v2): refactor utils code to be slightly more concise (#1633)
This commit is contained in:
parent
1c0d5f0135
commit
d8b5323836
1 changed files with 9 additions and 14 deletions
|
@ -173,19 +173,17 @@ export function normalizeUrl(rawUrls: string[]): string {
|
|||
urls[0] = first + urls[0];
|
||||
}
|
||||
|
||||
// There must be two or three slashes in the file protocol, two slashes in anything else.
|
||||
if (urls[0].match(/^file:\/\/\//)) {
|
||||
urls[0] = urls[0].replace(/^([^/:]+):\/*/, '$1:///');
|
||||
} else {
|
||||
urls[0] = urls[0].replace(/^([^/:]+):\/*/, '$1://');
|
||||
}
|
||||
// There must be two or three slashes in the file protocol,
|
||||
// two slashes in anything else.
|
||||
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
||||
urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
|
||||
|
||||
// eslint-disable-next-line
|
||||
for (let i = 0; i < urls.length; i++) {
|
||||
let component = urls[i];
|
||||
|
||||
if (typeof component !== 'string') {
|
||||
throw new TypeError(`Url must be a string. Received ${component}`);
|
||||
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
||||
}
|
||||
|
||||
if (component === '') {
|
||||
|
@ -197,13 +195,10 @@ export function normalizeUrl(rawUrls: string[]): string {
|
|||
// Removing the starting slashes for each component but the first.
|
||||
component = component.replace(/^[/]+/, '');
|
||||
}
|
||||
if (i < urls.length - 1) {
|
||||
// Removing the ending slashes for each component but the last.
|
||||
component = component.replace(/[/]+$/, '');
|
||||
} else {
|
||||
// For the last component we will combine multiple slashes to a single one.
|
||||
component = component.replace(/[/]+$/, '/');
|
||||
}
|
||||
|
||||
// Removing the ending slashes for each component but the last.
|
||||
// For the last component we will combine multiple slashes to a single one.
|
||||
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
||||
|
||||
resultArray.push(component);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue