@vuepress/shared-utils
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -7,2 +7,5 @@ "use strict"; | ||
const ensureLeadingSlash_1 = __importDefault(require("./ensureLeadingSlash")); | ||
function removeLeadingSlash(path) { | ||
return path.replace(/^\//, ''); | ||
} | ||
module.exports = function getPermalink({ pattern, slug, date, regularPath, localePath = '/' }) { | ||
@@ -21,4 +24,3 @@ if (!pattern) { | ||
const day = iDay < 10 ? `0${iDay}` : iDay; | ||
// Remove leading slash | ||
pattern = pattern.replace(/^\//, ''); | ||
pattern = removeLeadingSlash(pattern); | ||
const link = localePath + | ||
@@ -34,4 +36,4 @@ pattern | ||
.replace(/:slug/, slug) | ||
.replace(/:regular/, regularPath); | ||
.replace(/:regular/, removeLeadingSlash(regularPath)); | ||
return ensureLeadingSlash_1.default(ensureEndingSlash_1.default(link)); | ||
}; |
@@ -21,12 +21,16 @@ 'use strict'; | ||
class CommonModule { | ||
constructor(entry, name, shortcut, fromDep) { | ||
constructor(entry, name, shortcut, fromDep, error) { | ||
this.entry = entry; | ||
this.name = name; | ||
this.shortcut = shortcut; | ||
this.name = name; | ||
this.fromDep = fromDep; | ||
this.error = error; | ||
} | ||
} | ||
exports.CommonModule = CommonModule; | ||
function getNoopModule(error) { | ||
return new CommonModule(null, null, null, null, error); | ||
} | ||
class ModuleResolver { | ||
constructor(type, org, allowedTypes = [String], load = false, cwd) { | ||
constructor(type, org, allowedTypes, load = false, cwd) { | ||
this.type = type; | ||
@@ -36,2 +40,7 @@ this.org = org; | ||
this.load = load; | ||
this.cwd = cwd; | ||
this.type = type; | ||
this.org = org; | ||
this.allowedTypes = allowedTypes; | ||
this.load = load; | ||
this.cwd = cwd || process.cwd(); | ||
@@ -62,11 +71,9 @@ if (org) { | ||
const isStringRequest = datatypes_1.isString(req); | ||
const isAbsolutePath = isStringRequest && upath_1.default.isAbsolute(req); | ||
const resolved = tryChain_1.default([ | ||
[this.resolveNonStringPackage.bind(this), !isStringRequest], | ||
[this.resolveAbsolutePathPackage.bind(this), isStringRequest && isAbsolutePath], | ||
[this.resolveRelativePathPackage.bind(this), isStringRequest && !isAbsolutePath], | ||
[this.resolvePathPackage.bind(this), isStringRequest], | ||
[this.resolveDepPackage.bind(this), isStringRequest] | ||
], req); | ||
if (!resolved) { | ||
return new CommonModule(null, null, null, null /* fromDep */); | ||
return getNoopModule(); | ||
} | ||
@@ -90,5 +97,8 @@ return resolved; | ||
/** | ||
* Resolve module with absolute path. | ||
* Resolve module with absolute/relative path. | ||
*/ | ||
resolveAbsolutePathPackage(req) { | ||
resolvePathPackage(req) { | ||
if (!upath_1.default.isAbsolute(req)) { | ||
req = upath_1.default.resolve(this.cwd, req); | ||
} | ||
const normalized = fallback_1.fsExistsFallback([ | ||
@@ -103,22 +113,25 @@ req, | ||
const dirname = upath_1.default.parse(normalized).name; | ||
const { shortcut, name } = this.normalizeRequest(dirname); | ||
const module = this.load ? require(normalized) : normalized; | ||
return new CommonModule(module, name, shortcut, false /* fromDep */); | ||
const { shortcut, name } = this.normalizeName(dirname); | ||
try { | ||
const module = this.load ? require(normalized) : normalized; | ||
return new CommonModule(module, name, shortcut, false /* fromDep */); | ||
} | ||
catch (error) { | ||
return getNoopModule(error); | ||
} | ||
} | ||
/** | ||
* Resolve module with absolute path. | ||
*/ | ||
resolveRelativePathPackage(req) { | ||
req = upath_1.default.resolve(process.cwd(), req); | ||
return this.resolveAbsolutePathPackage(req); | ||
} | ||
/** | ||
* Resolve module from dependency. | ||
*/ | ||
resolveDepPackage(req) { | ||
const { shortcut, name } = this.normalizeRequest(req); | ||
const entry = this.load | ||
? moduleLoader_1.loadModule(name, this.cwd) | ||
: moduleLoader_1.resolveModule(name, this.cwd); | ||
return new CommonModule(entry, name, shortcut, true /* fromDep */); | ||
const { shortcut, name } = this.normalizeName(req); | ||
try { | ||
const entry = this.load | ||
? moduleLoader_1.loadModule(name, this.cwd) | ||
: moduleLoader_1.resolveModule(name, this.cwd); | ||
return new CommonModule(entry, name, shortcut, true /* fromDep */); | ||
} | ||
catch (error) { | ||
return getNoopModule(error); | ||
} | ||
} | ||
@@ -137,4 +150,4 @@ /** | ||
normalizeName(req) { | ||
let name; | ||
let shortcut; | ||
let name = null; | ||
let shortcut = null; | ||
if (req.startsWith('@')) { | ||
@@ -161,3 +174,2 @@ const pkg = resolveScopePackage(req); | ||
} | ||
// @ts-ignore | ||
return { name, shortcut }; | ||
@@ -164,0 +176,0 @@ } |
{ | ||
"name": "@vuepress/shared-utils", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "shared-utils for vuepress", | ||
@@ -46,3 +46,3 @@ "main": "lib/index.js", | ||
"homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/shared-utils#readme", | ||
"gitHead": "d811bfbe3875b31746678dbe79b67385252dceb9" | ||
"gitHead": "f61e2d42b4b2d9a59630f95a37fab968dcbd8b39" | ||
} |
@@ -1,2 +0,2 @@ | ||
declare const _default: (content: string, include: never[] | undefined, md: any) => {}; | ||
declare const _default: (content: string, include: never[] | undefined, md: any) => unknown; | ||
export = _default; |
@@ -5,7 +5,8 @@ /** | ||
export declare class CommonModule { | ||
entry: string | null; | ||
name: string | null; | ||
entry: string | null; | ||
shortcut: string | null; | ||
fromDep: boolean | null; | ||
constructor(entry: string | null, name: string | null, shortcut: string | null, fromDep: boolean | null); | ||
error?: Error | undefined; | ||
constructor(entry: string | null, name: string | null, shortcut: string | null, fromDep: boolean | null, error?: Error | undefined); | ||
} | ||
@@ -30,3 +31,3 @@ export interface NormalizedModuleRequest { | ||
private prefixSlicePosition; | ||
constructor(type: string, org: string, allowedTypes: Type[] | undefined, load: boolean | undefined, cwd: string); | ||
constructor(type: string, org: string, allowedTypes: Type[], load: boolean, cwd: string); | ||
/** | ||
@@ -45,10 +46,6 @@ * Resolve package. | ||
/** | ||
* Resolve module with absolute path. | ||
* Resolve module with absolute/relative path. | ||
*/ | ||
resolveAbsolutePathPackage(req: string): CommonModule; | ||
resolvePathPackage(req: string): CommonModule; | ||
/** | ||
* Resolve module with absolute path. | ||
*/ | ||
private resolveRelativePathPackage; | ||
/** | ||
* Resolve module from dependency. | ||
@@ -55,0 +52,0 @@ */ |
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
42564
1152