add isValidPathname

This commit is contained in:
slorber 2020-06-03 15:40:10 +02:00
parent 4e8b361d00
commit da8ccd0419
2 changed files with 29 additions and 0 deletions

View file

@ -12,6 +12,7 @@ import camelCase from 'lodash.camelcase';
import kebabCase from 'lodash.kebabcase';
import escapeStringRegexp from 'escape-string-regexp';
import fs from 'fs-extra';
import {URL} from 'url';
const fileHash = new Map();
export async function generate(
@ -327,3 +328,14 @@ export function getEditUrl(fileRelativePath: string, editUrl?: string) {
? normalizeUrl([editUrl, posixPath(fileRelativePath)])
: undefined;
}
export function isValidPathname(str: string): boolean {
if (!str.startsWith('/')) {
return false;
}
try {
return new URL(str, 'https://domain.com').pathname === str;
} catch (e) {
return false;
}
}