@docusaurus/utils
Advanced tools
Comparing version 2.0.0-alpha.58 to 2.0.0-alpha.59
@@ -17,2 +17,3 @@ /** | ||
export declare function encodePath(userpath: string): string; | ||
export declare function simpleHash(str: string, length: number): string; | ||
/** | ||
@@ -66,7 +67,10 @@ * Given an input string, convert to kebab-case and append a hash. | ||
export declare function isValidPathname(str: string): boolean; | ||
export declare function resolvePathname(to: string, from?: string): any; | ||
export declare function addLeadingSlash(str: string): string; | ||
export declare function addTrailingSlash(str: string): string; | ||
export declare function removeTrailingSlash(str: string): string; | ||
export declare function removeSuffix(str: string, suffix: string): string; | ||
export declare function removePrefix(str: string, prefix: string): string; | ||
export declare function getFilePathForRoutePath(routePath: string): string; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -12,2 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFilePathForRoutePath = exports.removePrefix = exports.removeSuffix = exports.removeTrailingSlash = exports.addTrailingSlash = exports.addLeadingSlash = exports.resolvePathname = exports.isValidPathname = exports.getEditUrl = exports.aliasedSitePath = exports.normalizeUrl = exports.parseMarkdownFile = exports.parseMarkdownString = exports.createExcerpt = exports.getSubFolder = exports.idx = exports.genChunkName = exports.posixPath = exports.genComponentName = exports.upperFirst = exports.docuHash = exports.simpleHash = exports.encodePath = exports.fileToPath = exports.objectWithKeySorted = exports.generate = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
@@ -21,2 +22,4 @@ const gray_matter_1 = __importDefault(require("gray-matter")); | ||
const url_1 = require("url"); | ||
// @ts-expect-error: no typedefs :s | ||
const resolve_pathname_1 = __importDefault(require("resolve-pathname")); | ||
const fileHash = new Map(); | ||
@@ -58,3 +61,3 @@ async function generate(generatedFilesDir, file, content, skipCache = process.env.NODE_ENV === 'production') { | ||
const indexRE = /(^|.*\/)index\.(md|js|jsx|ts|tsx)$/i; | ||
const extRE = /\.(md|js|tsx)$/; | ||
const extRE = /\.(md|js|ts|tsx)$/; | ||
/** | ||
@@ -78,2 +81,6 @@ * Convert filepath to url path. | ||
exports.encodePath = encodePath; | ||
function simpleHash(str, length) { | ||
return crypto_1.createHash('md5').update(str).digest('hex').substr(0, length); | ||
} | ||
exports.simpleHash = simpleHash; | ||
/** | ||
@@ -87,3 +94,3 @@ * Given an input string, convert to kebab-case and append a hash. | ||
} | ||
const shortHash = crypto_1.createHash('md5').update(str).digest('hex').substr(0, 3); | ||
const shortHash = simpleHash(str, 3); | ||
return `${lodash_kebabcase_1.default(str)}-${shortHash}`; | ||
@@ -133,6 +140,3 @@ } | ||
if (shortId) { | ||
chunkName = crypto_1.createHash('md5') | ||
.update(modulePath) | ||
.digest('hex') | ||
.substr(0, 8); | ||
chunkName = simpleHash(modulePath, 8); | ||
} | ||
@@ -142,6 +146,3 @@ else { | ||
if (preferredName) { | ||
const shortHash = crypto_1.createHash('md5') | ||
.update(modulePath) | ||
.digest('hex') | ||
.substr(0, 3); | ||
const shortHash = simpleHash(modulePath, 3); | ||
str = `${preferredName}${shortHash}`; | ||
@@ -157,2 +158,4 @@ } | ||
exports.genChunkName = genChunkName; | ||
// Too dynamic | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any | ||
function idx(target, keyPaths) { | ||
@@ -187,3 +190,3 @@ return (target && | ||
const fileLines = fileContent.split('\n'); | ||
for (let fileLine of fileLines) { | ||
for (const fileLine of fileLines) { | ||
const cleanedLine = fileLine | ||
@@ -318,2 +321,3 @@ // Remove HTML tags. | ||
try { | ||
// weird, but is there a better way? | ||
return new url_1.URL(str, 'https://domain.com').pathname === str; | ||
@@ -326,2 +330,11 @@ } | ||
exports.isValidPathname = isValidPathname; | ||
// resolve pathname and fail fast if resolution fails | ||
function resolvePathname(to, from) { | ||
return resolve_pathname_1.default(to, from); | ||
} | ||
exports.resolvePathname = resolvePathname; | ||
function addLeadingSlash(str) { | ||
return str.startsWith('/') ? str : `/${str}`; | ||
} | ||
exports.addLeadingSlash = addLeadingSlash; | ||
function addTrailingSlash(str) { | ||
@@ -342,2 +355,6 @@ return str.endsWith('/') ? str : `${str}/`; | ||
exports.removeSuffix = removeSuffix; | ||
function removePrefix(str, prefix) { | ||
return str.startsWith(prefix) ? str.slice(prefix.length) : str; | ||
} | ||
exports.removePrefix = removePrefix; | ||
function getFilePathForRoutePath(routePath) { | ||
@@ -344,0 +361,0 @@ const fileName = path_1.default.basename(routePath); |
{ | ||
"name": "@docusaurus/utils", | ||
"version": "2.0.0-alpha.58", | ||
"version": "2.0.0-alpha.59", | ||
"description": "Node utility functions for Docusaurus packages", | ||
@@ -8,3 +8,3 @@ "main": "./lib/index.js", | ||
"scripts": { | ||
"tsc": "tsc", | ||
"build": "tsc", | ||
"watch": "tsc --watch" | ||
@@ -21,3 +21,4 @@ }, | ||
"lodash.camelcase": "^4.3.0", | ||
"lodash.kebabcase": "^4.1.1" | ||
"lodash.kebabcase": "^4.1.1", | ||
"resolve-pathname": "^3.0.0" | ||
}, | ||
@@ -27,3 +28,3 @@ "engines": { | ||
}, | ||
"gitHead": "16ba6e54b672151a2fcd0ded8deb56f638f6ec01" | ||
"gitHead": "a1f688789f501bf3f597acbde0f347eca9cadd6f" | ||
} |
@@ -11,2 +11,3 @@ /** | ||
fileToPath, | ||
simpleHash, | ||
docuHash, | ||
@@ -26,3 +27,5 @@ genComponentName, | ||
removeSuffix, | ||
removePrefix, | ||
getFilePathForRoutePath, | ||
addLeadingSlash, | ||
} from '../index'; | ||
@@ -75,2 +78,17 @@ | ||
test('simpleHash', () => { | ||
const asserts = { | ||
'': 'd41', | ||
'/foo-bar': '096', | ||
'/foo/bar': '1df', | ||
'/endi/lie': '9fa', | ||
'/endi-lie': 'fd3', | ||
'/yangshun/tay': '48d', | ||
'/yangshun-tay': 'f3b', | ||
}; | ||
Object.keys(asserts).forEach((file) => { | ||
expect(simpleHash(file, 3)).toBe(asserts[file]); | ||
}); | ||
}); | ||
test('docuHash', () => { | ||
@@ -401,2 +419,11 @@ const asserts = { | ||
describe('addLeadingSlash', () => { | ||
test('should no-op', () => { | ||
expect(addLeadingSlash('/abc')).toEqual('/abc'); | ||
}); | ||
test('should add /', () => { | ||
expect(addLeadingSlash('abc')).toEqual('/abc'); | ||
}); | ||
}); | ||
describe('removeTrailingSlash', () => { | ||
@@ -426,2 +453,17 @@ test('should no-op', () => { | ||
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', () => { | ||
@@ -428,0 +470,0 @@ test('works for /', () => { |
@@ -17,2 +17,5 @@ /** | ||
// @ts-expect-error: no typedefs :s | ||
import resolvePathnameUnsafe from 'resolve-pathname'; | ||
const fileHash = new Map(); | ||
@@ -64,3 +67,3 @@ export async function generate( | ||
const indexRE = /(^|.*\/)index\.(md|js|jsx|ts|tsx)$/i; | ||
const extRE = /\.(md|js|tsx)$/; | ||
const extRE = /\.(md|js|ts|tsx)$/; | ||
@@ -85,2 +88,6 @@ /** | ||
export function simpleHash(str: string, length: number): string { | ||
return createHash('md5').update(str).digest('hex').substr(0, length); | ||
} | ||
/** | ||
@@ -94,3 +101,3 @@ * Given an input string, convert to kebab-case and append a hash. | ||
} | ||
const shortHash = createHash('md5').update(str).digest('hex').substr(0, 3); | ||
const shortHash = simpleHash(str, 3); | ||
return `${kebabCase(str)}-${shortHash}`; | ||
@@ -146,13 +153,7 @@ } | ||
if (shortId) { | ||
chunkName = createHash('md5') | ||
.update(modulePath) | ||
.digest('hex') | ||
.substr(0, 8); | ||
chunkName = simpleHash(modulePath, 8); | ||
} else { | ||
let str = modulePath; | ||
if (preferredName) { | ||
const shortHash = createHash('md5') | ||
.update(modulePath) | ||
.digest('hex') | ||
.substr(0, 3); | ||
const shortHash = simpleHash(modulePath, 3); | ||
str = `${preferredName}${shortHash}`; | ||
@@ -168,2 +169,4 @@ } | ||
// Too dynamic | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any | ||
export function idx(target: any, keyPaths?: string | (string | number)[]): any { | ||
@@ -206,3 +209,3 @@ return ( | ||
for (let fileLine of fileLines) { | ||
for (const fileLine of fileLines) { | ||
const cleanedLine = fileLine | ||
@@ -241,2 +244,4 @@ // Remove HTML tags. | ||
frontMatter: { | ||
// Returned by gray-matter | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key: string]: any; | ||
@@ -248,3 +253,3 @@ }; | ||
export function parseMarkdownString(markdownString: string): ParsedMarkdown { | ||
const options: {} = { | ||
const options: Record<string, unknown> = { | ||
excerpt: (file: matter.GrayMatterFile<string>): void => { | ||
@@ -350,3 +355,3 @@ // Hacky way of stripping out import statements from the excerpt | ||
*/ | ||
export function aliasedSitePath(filePath: string, siteDir: string) { | ||
export function aliasedSitePath(filePath: string, siteDir: string): string { | ||
const relativePath = path.relative(siteDir, filePath); | ||
@@ -358,3 +363,6 @@ // Cannot use path.join() as it resolves '../' and removes | ||
export function getEditUrl(fileRelativePath: string, editUrl?: string) { | ||
export function getEditUrl( | ||
fileRelativePath: string, | ||
editUrl?: string, | ||
): string | undefined { | ||
return editUrl | ||
@@ -370,2 +378,3 @@ ? normalizeUrl([editUrl, posixPath(fileRelativePath)]) | ||
try { | ||
// weird, but is there a better way? | ||
return new URL(str, 'https://domain.com').pathname === str; | ||
@@ -377,11 +386,18 @@ } catch (e) { | ||
export function addTrailingSlash(str: string) { | ||
// resolve pathname and fail fast if resolution fails | ||
export function resolvePathname(to: string, from?: string) { | ||
return resolvePathnameUnsafe(to, from); | ||
} | ||
export function addLeadingSlash(str: string): string { | ||
return str.startsWith('/') ? str : `/${str}`; | ||
} | ||
export function addTrailingSlash(str: string): string { | ||
return str.endsWith('/') ? str : `${str}/`; | ||
} | ||
export function removeTrailingSlash(str: string) { | ||
export function removeTrailingSlash(str: string): string { | ||
return removeSuffix(str, '/'); | ||
} | ||
export function removeSuffix(str: string, suffix: string) { | ||
export function removeSuffix(str: string, suffix: string): string { | ||
if (suffix === '') { | ||
@@ -393,3 +409,7 @@ return str; // always returns "" otherwise! | ||
export function getFilePathForRoutePath(routePath: string) { | ||
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); | ||
@@ -396,0 +416,0 @@ const filePath = path.dirname(routePath); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
488437
1232
0
6
+ Addedresolve-pathname@^3.0.0
+ Addedresolve-pathname@3.0.0(transitive)