@docusaurus/utils 0.0.0-5955 → 0.0.0-5957
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +6 -3
- package/lib/index.js.map +1 -1
- package/lib/markdownLinks.d.ts +6 -33
- package/lib/markdownLinks.d.ts.map +1 -1
- package/lib/markdownLinks.js +22 -104
- package/lib/markdownLinks.js.map +1 -1
- package/lib/urlUtils.d.ts +14 -0
- package/lib/urlUtils.d.ts.map +1 -1
- package/lib/urlUtils.js +65 -18
- package/lib/urlUtils.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +4 -5
- package/src/markdownLinks.ts +28 -152
- package/src/urlUtils.ts +75 -21
package/lib/index.d.ts
CHANGED
|
@@ -9,11 +9,11 @@ export { generate, readOutputHTMLFile } from './emitUtils';
|
|
|
9
9
|
export { getFileCommitDate, FileNotTrackedError, GitNotFoundError, } from './gitUtils';
|
|
10
10
|
export { mergeTranslations, updateTranslationFileMessages, getPluginI18nPath, localizePath, } from './i18nUtils';
|
|
11
11
|
export { mapAsyncSequential, findAsyncSequential } from './jsUtils';
|
|
12
|
-
export { normalizeUrl, getEditUrl, fileToPath, encodePath, isValidPathname, resolvePathname, parseURLPath, serializeURLPath, hasSSHProtocol, buildHttpsUrl, buildSshUrl, } from './urlUtils';
|
|
12
|
+
export { normalizeUrl, getEditUrl, fileToPath, encodePath, isValidPathname, resolvePathname, parseURLPath, parseLocalURLPath, parseURLOrPath, toURLPath, serializeURLPath, hasSSHProtocol, buildHttpsUrl, buildSshUrl, } from './urlUtils';
|
|
13
13
|
export type { URLPath } from './urlUtils';
|
|
14
14
|
export { type Tag, type TagsListItem, type TagModule, type FrontMatterTag, normalizeFrontMatterTags, groupTaggedItems, getTagVisibility, } from './tags';
|
|
15
15
|
export { parseMarkdownHeadingId, escapeMarkdownHeadingIds, unwrapMdxCodeBlocks, admonitionTitleToDirectiveLabel, createExcerpt, DEFAULT_PARSE_FRONT_MATTER, parseMarkdownContentTitle, parseMarkdownFile, writeMarkdownHeadingId, type WriteHeadingIDOptions, } from './markdownUtils';
|
|
16
|
-
export { type ContentPaths,
|
|
16
|
+
export { type ContentPaths, resolveMarkdownLinkPathname } from './markdownLinks';
|
|
17
17
|
export { type SluggerOptions, type Slugger, createSlugger } from './slugger';
|
|
18
18
|
export { isNameTooLong, shortName, posixPath, toMessageRelativeFilePath, aliasedSitePath, aliasedSitePathToRelativePath, escapePath, addTrailingPathSeparator, } from './pathUtils';
|
|
19
19
|
export { md5Hash, simpleHash, docuHash } from './hashUtils';
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,EACtB,wBAAwB,EACxB,YAAY,EACZ,uBAAuB,EACvB,6BAA6B,EAC7B,UAAU,EACV,qBAAqB,EACrB,2BAA2B,EAC3B,YAAY,EACZ,iBAAiB,EACjB,wBAAwB,GACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,QAAQ,EAAE,kBAAkB,EAAC,MAAM,aAAa,CAAC;AACzD,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,iBAAiB,EACjB,YAAY,GACb,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,kBAAkB,EAAE,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAClE,OAAO,EACL,YAAY,EACZ,UAAU,EACV,UAAU,EACV,UAAU,EACV,eAAe,EACf,eAAe,EACf,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,aAAa,EACb,WAAW,GACZ,MAAM,YAAY,CAAC;AACpB,YAAY,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AACxC,OAAO,EACL,KAAK,GAAG,EACR,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,wBAAwB,EACxB,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,mBAAmB,EACnB,+BAA+B,EAC/B,aAAa,EACb,0BAA0B,EAC1B,yBAAyB,EACzB,iBAAiB,EACjB,sBAAsB,EACtB,KAAK,qBAAqB,GAC3B,MAAM,iBAAiB,CAAC;AACzB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,EACtB,wBAAwB,EACxB,YAAY,EACZ,uBAAuB,EACvB,6BAA6B,EAC7B,UAAU,EACV,qBAAqB,EACrB,2BAA2B,EAC3B,YAAY,EACZ,iBAAiB,EACjB,wBAAwB,GACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,QAAQ,EAAE,kBAAkB,EAAC,MAAM,aAAa,CAAC;AACzD,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,iBAAiB,EACjB,6BAA6B,EAC7B,iBAAiB,EACjB,YAAY,GACb,MAAM,aAAa,CAAC;AACrB,OAAO,EAAC,kBAAkB,EAAE,mBAAmB,EAAC,MAAM,WAAW,CAAC;AAClE,OAAO,EACL,YAAY,EACZ,UAAU,EACV,UAAU,EACV,UAAU,EACV,eAAe,EACf,eAAe,EACf,YAAY,EACZ,iBAAiB,EACjB,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,cAAc,EACd,aAAa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OAAO,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAChD,OAAO,EAAC,aAAa,EAAC,MAAM,cAAc,CAAC;AAE3C,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,qBAAqB,GAC3B,MAAM,mBAAmB,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.
|
|
10
|
-
exports.LAST_UPDATE_FALLBACK = exports.readLastUpdateData = exports.getLastUpdate = exports.getGitLastUpdate = exports.flattenRoutes = exports.askPreferredLanguage = exports.escapeRegexp = exports.isUnlisted = exports.isDraft = exports.getFolderContainingFile = exports.findFolderContainingFile = exports.getContentPathList = exports.getDataFileData = exports.getDataFilePath = exports.loadFreshModule = exports.escapeShellArg = exports.getWebpackLoaderCompilerName = exports.getFileLoaderUtils = exports.createAbsoluteFilePathMatcher = exports.createMatcher = exports.GlobExcludeDefault = exports.Globby = exports.docuHash = exports.simpleHash = exports.md5Hash = exports.addTrailingPathSeparator = exports.escapePath = exports.aliasedSitePathToRelativePath = exports.aliasedSitePath = exports.toMessageRelativeFilePath = exports.posixPath = exports.shortName = exports.isNameTooLong = exports.createSlugger = exports.
|
|
9
|
+
exports.DEFAULT_PARSE_FRONT_MATTER = exports.createExcerpt = exports.admonitionTitleToDirectiveLabel = exports.unwrapMdxCodeBlocks = exports.escapeMarkdownHeadingIds = exports.parseMarkdownHeadingId = exports.getTagVisibility = exports.groupTaggedItems = exports.normalizeFrontMatterTags = exports.buildSshUrl = exports.buildHttpsUrl = exports.hasSSHProtocol = exports.serializeURLPath = exports.toURLPath = exports.parseURLOrPath = exports.parseLocalURLPath = exports.parseURLPath = exports.resolvePathname = exports.isValidPathname = exports.encodePath = exports.fileToPath = exports.getEditUrl = exports.normalizeUrl = exports.findAsyncSequential = exports.mapAsyncSequential = exports.localizePath = exports.getPluginI18nPath = exports.updateTranslationFileMessages = exports.mergeTranslations = exports.GitNotFoundError = exports.FileNotTrackedError = exports.getFileCommitDate = exports.readOutputHTMLFile = exports.generate = exports.WEBPACK_URL_LOADER_LIMIT = exports.DEFAULT_PLUGIN_ID = exports.DEFAULT_PORT = exports.CODE_TRANSLATIONS_FILE_NAME = exports.DEFAULT_I18N_DIR_NAME = exports.THEME_PATH = exports.OUTPUT_STATIC_ASSETS_DIR_NAME = exports.DEFAULT_STATIC_DIR_NAME = exports.SRC_DIR_NAME = exports.GENERATED_FILES_DIR_NAME = exports.BABEL_CONFIG_FILE_NAME = exports.DEFAULT_CONFIG_FILE_NAME = exports.DEFAULT_BUILD_DIR_NAME = exports.DOCUSAURUS_VERSION = exports.NODE_MINOR_VERSION = exports.NODE_MAJOR_VERSION = void 0;
|
|
10
|
+
exports.LAST_UPDATE_FALLBACK = exports.readLastUpdateData = exports.getLastUpdate = exports.getGitLastUpdate = exports.flattenRoutes = exports.askPreferredLanguage = exports.escapeRegexp = exports.isUnlisted = exports.isDraft = exports.getFolderContainingFile = exports.findFolderContainingFile = exports.getContentPathList = exports.getDataFileData = exports.getDataFilePath = exports.loadFreshModule = exports.escapeShellArg = exports.getWebpackLoaderCompilerName = exports.getFileLoaderUtils = exports.createAbsoluteFilePathMatcher = exports.createMatcher = exports.GlobExcludeDefault = exports.Globby = exports.docuHash = exports.simpleHash = exports.md5Hash = exports.addTrailingPathSeparator = exports.escapePath = exports.aliasedSitePathToRelativePath = exports.aliasedSitePath = exports.toMessageRelativeFilePath = exports.posixPath = exports.shortName = exports.isNameTooLong = exports.createSlugger = exports.resolveMarkdownLinkPathname = exports.writeMarkdownHeadingId = exports.parseMarkdownFile = exports.parseMarkdownContentTitle = void 0;
|
|
11
11
|
var constants_1 = require("./constants");
|
|
12
12
|
Object.defineProperty(exports, "NODE_MAJOR_VERSION", { enumerable: true, get: function () { return constants_1.NODE_MAJOR_VERSION; } });
|
|
13
13
|
Object.defineProperty(exports, "NODE_MINOR_VERSION", { enumerable: true, get: function () { return constants_1.NODE_MINOR_VERSION; } });
|
|
@@ -48,6 +48,9 @@ Object.defineProperty(exports, "encodePath", { enumerable: true, get: function (
|
|
|
48
48
|
Object.defineProperty(exports, "isValidPathname", { enumerable: true, get: function () { return urlUtils_1.isValidPathname; } });
|
|
49
49
|
Object.defineProperty(exports, "resolvePathname", { enumerable: true, get: function () { return urlUtils_1.resolvePathname; } });
|
|
50
50
|
Object.defineProperty(exports, "parseURLPath", { enumerable: true, get: function () { return urlUtils_1.parseURLPath; } });
|
|
51
|
+
Object.defineProperty(exports, "parseLocalURLPath", { enumerable: true, get: function () { return urlUtils_1.parseLocalURLPath; } });
|
|
52
|
+
Object.defineProperty(exports, "parseURLOrPath", { enumerable: true, get: function () { return urlUtils_1.parseURLOrPath; } });
|
|
53
|
+
Object.defineProperty(exports, "toURLPath", { enumerable: true, get: function () { return urlUtils_1.toURLPath; } });
|
|
51
54
|
Object.defineProperty(exports, "serializeURLPath", { enumerable: true, get: function () { return urlUtils_1.serializeURLPath; } });
|
|
52
55
|
Object.defineProperty(exports, "hasSSHProtocol", { enumerable: true, get: function () { return urlUtils_1.hasSSHProtocol; } });
|
|
53
56
|
Object.defineProperty(exports, "buildHttpsUrl", { enumerable: true, get: function () { return urlUtils_1.buildHttpsUrl; } });
|
|
@@ -67,7 +70,7 @@ Object.defineProperty(exports, "parseMarkdownContentTitle", { enumerable: true,
|
|
|
67
70
|
Object.defineProperty(exports, "parseMarkdownFile", { enumerable: true, get: function () { return markdownUtils_1.parseMarkdownFile; } });
|
|
68
71
|
Object.defineProperty(exports, "writeMarkdownHeadingId", { enumerable: true, get: function () { return markdownUtils_1.writeMarkdownHeadingId; } });
|
|
69
72
|
var markdownLinks_1 = require("./markdownLinks");
|
|
70
|
-
Object.defineProperty(exports, "
|
|
73
|
+
Object.defineProperty(exports, "resolveMarkdownLinkPathname", { enumerable: true, get: function () { return markdownLinks_1.resolveMarkdownLinkPathname; } });
|
|
71
74
|
var slugger_1 = require("./slugger");
|
|
72
75
|
Object.defineProperty(exports, "createSlugger", { enumerable: true, get: function () { return slugger_1.createSlugger; } });
|
|
73
76
|
var pathUtils_1 = require("./pathUtils");
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,yCAiBqB;AAhBnB,+GAAA,kBAAkB,OAAA;AAClB,+GAAA,kBAAkB,OAAA;AAClB,+GAAA,kBAAkB,OAAA;AAClB,mHAAA,sBAAsB,OAAA;AACtB,qHAAA,wBAAwB,OAAA;AACxB,mHAAA,sBAAsB,OAAA;AACtB,qHAAA,wBAAwB,OAAA;AACxB,yGAAA,YAAY,OAAA;AACZ,oHAAA,uBAAuB,OAAA;AACvB,0HAAA,6BAA6B,OAAA;AAC7B,uGAAA,UAAU,OAAA;AACV,kHAAA,qBAAqB,OAAA;AACrB,wHAAA,2BAA2B,OAAA;AAC3B,yGAAA,YAAY,OAAA;AACZ,8GAAA,iBAAiB,OAAA;AACjB,qHAAA,wBAAwB,OAAA;AAE1B,yCAAyD;AAAjD,qGAAA,QAAQ,OAAA;AAAE,+GAAA,kBAAkB,OAAA;AACpC,uCAIoB;AAHlB,6GAAA,iBAAiB,OAAA;AACjB,+GAAA,mBAAmB,OAAA;AACnB,4GAAA,gBAAgB,OAAA;AAElB,yCAKqB;AAJnB,8GAAA,iBAAiB,OAAA;AACjB,0HAAA,6BAA6B,OAAA;AAC7B,8GAAA,iBAAiB,OAAA;AACjB,yGAAA,YAAY,OAAA;AAEd,qCAAkE;AAA1D,6GAAA,kBAAkB,OAAA;AAAE,8GAAA,mBAAmB,OAAA;AAC/C,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot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AA,UAAU,OAAA;AAC3B,6CAA2C;AAAnC,2GAAA,YAAY,OAAA;AACpB,uCAAgD;AAAxC,gHAAA,oBAAoB,OAAA;AAC5B,2CAA2C;AAAnC,2GAAA,aAAa,OAAA;AAErB,qDAO2B;AANzB,mHAAA,gBAAgB,OAAA;AAChB,gHAAA,aAAa,OAAA;AACb,qHAAA,kBAAkB,OAAA;AAClB,uHAAA,oBAAoB,OAAA"}
|
package/lib/markdownLinks.d.ts
CHANGED
|
@@ -33,39 +33,12 @@ export type BrokenMarkdownLink<T extends ContentPaths> = {
|
|
|
33
33
|
*/
|
|
34
34
|
link: string;
|
|
35
35
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* counterparts, e.g. `[link](./intro.md)` => `[link](/docs/intro)`, preserving
|
|
39
|
-
* everything else.
|
|
40
|
-
*
|
|
41
|
-
* This method uses best effort to find a matching file. The file reference can
|
|
42
|
-
* be relative to the directory of the current file (most likely) or any of the
|
|
43
|
-
* content paths (so `/tutorials/intro.md` can be resolved as
|
|
44
|
-
* `<siteDir>/docs/tutorials/intro.md`). Links that contain the `http(s):` or
|
|
45
|
-
* `@site/` prefix will always be ignored.
|
|
46
|
-
*/
|
|
47
|
-
export declare function replaceMarkdownLinks<T extends ContentPaths>({ siteDir, fileString, filePath, contentPaths, sourceToPermalink, }: {
|
|
48
|
-
/** Absolute path to the site directory, used to resolve aliased paths. */
|
|
49
|
-
siteDir: string;
|
|
50
|
-
/** The Markdown file content to be processed. */
|
|
51
|
-
fileString: string;
|
|
52
|
-
/** Absolute path to the current file containing `fileString`. */
|
|
53
|
-
filePath: string;
|
|
54
|
-
/** The content paths which the file reference may live in. */
|
|
55
|
-
contentPaths: T;
|
|
56
|
-
/**
|
|
57
|
-
* A map from source paths to their URLs. Source paths are `@site` aliased.
|
|
58
|
-
*/
|
|
36
|
+
export declare function resolveMarkdownLinkPathname(linkPathname: string, context: {
|
|
37
|
+
sourceFilePath: string;
|
|
59
38
|
sourceToPermalink: {
|
|
60
|
-
[
|
|
39
|
+
[aliasedFilePath: string]: string;
|
|
61
40
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
* Unresolved links are left as-is.
|
|
66
|
-
*/
|
|
67
|
-
newContent: string;
|
|
68
|
-
/** The list of broken links, */
|
|
69
|
-
brokenMarkdownLinks: BrokenMarkdownLink<T>[];
|
|
70
|
-
};
|
|
41
|
+
contentPaths: ContentPaths;
|
|
42
|
+
siteDir: string;
|
|
43
|
+
}): string | null;
|
|
71
44
|
//# sourceMappingURL=markdownLinks.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownLinks.d.ts","sourceRoot":"","sources":["../src/markdownLinks.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,4EAA4E;AAC5E,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,YAAY,IAAI;IACvD,sDAAsD;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,YAAY,EAAE,CAAC,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;
|
|
1
|
+
{"version":3,"file":"markdownLinks.d.ts","sourceRoot":"","sources":["../src/markdownLinks.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,4EAA4E;AAC5E,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,YAAY,IAAI;IACvD,sDAAsD;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,YAAY,EAAE,CAAC,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAKF,wBAAgB,2BAA2B,CACzC,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE;IACP,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE;QAAC,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAAA;KAAC,CAAC;IACvD,YAAY,EAAE,YAAY,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;CACjB,GACA,MAAM,GAAG,IAAI,CAoBf"}
|
package/lib/markdownLinks.js
CHANGED
|
@@ -6,114 +6,32 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.
|
|
9
|
+
exports.resolveMarkdownLinkPathname = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
12
12
|
const dataFileUtils_1 = require("./dataFileUtils");
|
|
13
13
|
const pathUtils_1 = require("./pathUtils");
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
// Note this is historical logic extracted during a 2024 refactor
|
|
15
|
+
// The algo has been kept exactly as before for retro compatibility
|
|
16
|
+
// See also https://github.com/facebook/docusaurus/pull/10168
|
|
17
|
+
function resolveMarkdownLinkPathname(linkPathname, context) {
|
|
18
|
+
const { sourceFilePath, sourceToPermalink, contentPaths, siteDir } = context;
|
|
19
|
+
const sourceDirsToTry = [];
|
|
20
|
+
// ./file.md and ../file.md are always relative to the current file
|
|
21
|
+
if (!linkPathname.startsWith('./') && !linkPathname.startsWith('../')) {
|
|
22
|
+
sourceDirsToTry.push(...(0, dataFileUtils_1.getContentPathList)(contentPaths), siteDir);
|
|
18
23
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
* This method uses best effort to find a matching file. The file reference can
|
|
31
|
-
* be relative to the directory of the current file (most likely) or any of the
|
|
32
|
-
* content paths (so `/tutorials/intro.md` can be resolved as
|
|
33
|
-
* `<siteDir>/docs/tutorials/intro.md`). Links that contain the `http(s):` or
|
|
34
|
-
* `@site/` prefix will always be ignored.
|
|
35
|
-
*/
|
|
36
|
-
function replaceMarkdownLinks({ siteDir, fileString, filePath, contentPaths, sourceToPermalink, }) {
|
|
37
|
-
const brokenMarkdownLinks = [];
|
|
38
|
-
// Replace internal markdown linking (except in fenced blocks).
|
|
39
|
-
let lastOpenCodeFence = null;
|
|
40
|
-
const lines = fileString.split('\n').map((line) => {
|
|
41
|
-
const codeFence = parseCodeFence(line);
|
|
42
|
-
if (codeFence) {
|
|
43
|
-
if (!lastOpenCodeFence) {
|
|
44
|
-
lastOpenCodeFence = codeFence;
|
|
45
|
-
}
|
|
46
|
-
else if (!codeFence.definitelyOpen &&
|
|
47
|
-
lastOpenCodeFence.type === codeFence.type &&
|
|
48
|
-
lastOpenCodeFence.count <= codeFence.count) {
|
|
49
|
-
// All three conditions must be met in order for this to be considered
|
|
50
|
-
// a closing fence.
|
|
51
|
-
lastOpenCodeFence = null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (lastOpenCodeFence) {
|
|
55
|
-
return line;
|
|
56
|
-
}
|
|
57
|
-
let modifiedLine = line;
|
|
58
|
-
// Replace inline-style links or reference-style links e.g:
|
|
59
|
-
// This is [Document 1](doc1.md)
|
|
60
|
-
// [doc1]: doc1.md
|
|
61
|
-
const linkTitlePattern = '(?:\\s+(?:\'.*?\'|".*?"|\\(.*?\\)))?';
|
|
62
|
-
const linkSuffixPattern = '(?:\\?[^#>\\s]+)?(?:#[^>\\s]+)?';
|
|
63
|
-
const linkCapture = (forbidden) => `((?!https?://|@site/)[^${forbidden}#?]+)`;
|
|
64
|
-
const linkURLPattern = `(?:(?!<)${linkCapture('()\\s')}${linkSuffixPattern}|<${linkCapture('>')}${linkSuffixPattern}>)`;
|
|
65
|
-
const linkPattern = new RegExp(`\\[(?:(?!\\]\\().)*\\]\\(\\s*${linkURLPattern}${linkTitlePattern}\\s*\\)|^\\s*\\[[^[\\]]*[^[\\]\\s][^[\\]]*\\]:\\s*${linkURLPattern}${linkTitlePattern}$`, 'dgm');
|
|
66
|
-
let mdMatch = linkPattern.exec(modifiedLine);
|
|
67
|
-
while (mdMatch !== null) {
|
|
68
|
-
// Replace it to correct html link.
|
|
69
|
-
const mdLink = mdMatch.slice(1, 5).find(Boolean);
|
|
70
|
-
const mdLinkRange = mdMatch.indices.slice(1, 5).find(Boolean);
|
|
71
|
-
if (!/\.mdx?$/.test(mdLink)) {
|
|
72
|
-
mdMatch = linkPattern.exec(modifiedLine);
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
const sourcesToTry = [];
|
|
76
|
-
// ./file.md and ../file.md are always relative to the current file
|
|
77
|
-
if (!mdLink.startsWith('./') && !mdLink.startsWith('../')) {
|
|
78
|
-
sourcesToTry.push(...(0, dataFileUtils_1.getContentPathList)(contentPaths), siteDir);
|
|
79
|
-
}
|
|
80
|
-
// /file.md is always relative to the content path
|
|
81
|
-
if (!mdLink.startsWith('/')) {
|
|
82
|
-
sourcesToTry.push(path_1.default.dirname(filePath));
|
|
83
|
-
}
|
|
84
|
-
const aliasedSourceMatch = sourcesToTry
|
|
85
|
-
.map((p) => path_1.default.join(p, decodeURIComponent(mdLink)))
|
|
86
|
-
.map((source) => (0, pathUtils_1.aliasedSitePath)(source, siteDir))
|
|
87
|
-
.find((source) => sourceToPermalink[source]);
|
|
88
|
-
const permalink = aliasedSourceMatch
|
|
89
|
-
? sourceToPermalink[aliasedSourceMatch]
|
|
90
|
-
: undefined;
|
|
91
|
-
if (permalink) {
|
|
92
|
-
// MDX won't be happy if the permalink contains a space, we need to
|
|
93
|
-
// convert it to %20
|
|
94
|
-
const encodedPermalink = permalink
|
|
95
|
-
.split('/')
|
|
96
|
-
.map((part) => part.replace(/\s/g, '%20'))
|
|
97
|
-
.join('/');
|
|
98
|
-
modifiedLine = `${modifiedLine.slice(0, mdLinkRange[0])}${encodedPermalink}${modifiedLine.slice(mdLinkRange[1])}`;
|
|
99
|
-
// Adjust the lastIndex to avoid passing over the next link if the
|
|
100
|
-
// newly replaced URL is shorter.
|
|
101
|
-
linkPattern.lastIndex += encodedPermalink.length - mdLink.length;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
const brokenMarkdownLink = {
|
|
105
|
-
contentPaths,
|
|
106
|
-
filePath,
|
|
107
|
-
link: mdLink,
|
|
108
|
-
};
|
|
109
|
-
brokenMarkdownLinks.push(brokenMarkdownLink);
|
|
110
|
-
}
|
|
111
|
-
mdMatch = linkPattern.exec(modifiedLine);
|
|
112
|
-
}
|
|
113
|
-
return modifiedLine;
|
|
114
|
-
});
|
|
115
|
-
const newContent = lines.join('\n');
|
|
116
|
-
return { newContent, brokenMarkdownLinks };
|
|
24
|
+
// /file.md is never relative to the source file path
|
|
25
|
+
if (!linkPathname.startsWith('/')) {
|
|
26
|
+
sourceDirsToTry.push(path_1.default.dirname(sourceFilePath));
|
|
27
|
+
}
|
|
28
|
+
const aliasedSourceMatch = sourceDirsToTry
|
|
29
|
+
.map((sourceDir) => path_1.default.join(sourceDir, decodeURIComponent(linkPathname)))
|
|
30
|
+
.map((source) => (0, pathUtils_1.aliasedSitePath)(source, siteDir))
|
|
31
|
+
.find((source) => sourceToPermalink[source]);
|
|
32
|
+
return aliasedSourceMatch
|
|
33
|
+
? sourceToPermalink[aliasedSourceMatch] ?? null
|
|
34
|
+
: null;
|
|
117
35
|
}
|
|
118
|
-
exports.
|
|
36
|
+
exports.resolveMarkdownLinkPathname = resolveMarkdownLinkPathname;
|
|
119
37
|
//# sourceMappingURL=markdownLinks.js.map
|
package/lib/markdownLinks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownLinks.js","sourceRoot":"","sources":["../src/markdownLinks.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AACxB,mDAAmD;AACnD,2CAA4C;
|
|
1
|
+
{"version":3,"file":"markdownLinks.js","sourceRoot":"","sources":["../src/markdownLinks.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AACxB,mDAAmD;AACnD,2CAA4C;AAiC5C,iEAAiE;AACjE,mEAAmE;AACnE,6DAA6D;AAC7D,SAAgB,2BAA2B,CACzC,YAAoB,EACpB,OAKC;IAED,MAAM,EAAC,cAAc,EAAE,iBAAiB,EAAE,YAAY,EAAE,OAAO,EAAC,GAAG,OAAO,CAAC;IAC3E,MAAM,eAAe,GAAa,EAAE,CAAC;IACrC,mEAAmE;IACnE,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QACtE,eAAe,CAAC,IAAI,CAAC,GAAG,IAAA,kCAAkB,EAAC,YAAY,CAAC,EAAE,OAAO,CAAC,CAAC;IACrE,CAAC;IACD,qDAAqD;IACrD,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAClC,eAAe,CAAC,IAAI,CAAC,cAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;IACrD,CAAC;IAED,MAAM,kBAAkB,GAAG,eAAe;SACvC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,kBAAkB,CAAC,YAAY,CAAC,CAAC,CAAC;SAC1E,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAA,2BAAe,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;SACjD,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;IAE/C,OAAO,kBAAkB;QACvB,CAAC,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,IAAI,IAAI;QAC/C,CAAC,CAAC,IAAI,CAAC;AACX,CAAC;AA5BD,kEA4BC"}
|
package/lib/urlUtils.d.ts
CHANGED
|
@@ -41,12 +41,26 @@ export declare function encodePath(userPath: string): string;
|
|
|
41
41
|
* special characters.
|
|
42
42
|
*/
|
|
43
43
|
export declare function isValidPathname(str: string): boolean;
|
|
44
|
+
export declare function parseURLOrPath(url: string, base?: string | URL): URL;
|
|
44
45
|
export type URLPath = {
|
|
45
46
|
pathname: string;
|
|
46
47
|
search?: string;
|
|
47
48
|
hash?: string;
|
|
48
49
|
};
|
|
50
|
+
export declare function toURLPath(url: URL): URLPath;
|
|
51
|
+
/**
|
|
52
|
+
* Let's name the concept of (pathname + search + hash) as URLPath
|
|
53
|
+
* See also https://twitter.com/kettanaito/status/1741768992866308120
|
|
54
|
+
* Note: this function also resolves relative pathnames while parsing!
|
|
55
|
+
*/
|
|
49
56
|
export declare function parseURLPath(urlPath: string, fromPath?: string): URLPath;
|
|
57
|
+
/**
|
|
58
|
+
* This returns results for strings like "foo", "../foo", "./foo.mdx?qs#hash"
|
|
59
|
+
* Unlike "parseURLPath()" above, this will not resolve the pathnames
|
|
60
|
+
* Te returned pathname of "../../foo.mdx" will be "../../foo.mdx", not "/foo"
|
|
61
|
+
* This returns null if the url is not "local" (contains domain/protocol etc)
|
|
62
|
+
*/
|
|
63
|
+
export declare function parseLocalURLPath(urlPath: string): URLPath | null;
|
|
50
64
|
export declare function serializeURLPath(urlPath: URLPath): string;
|
|
51
65
|
/**
|
|
52
66
|
* Resolve pathnames and fail-fast if resolution fails. Uses standard URL
|
package/lib/urlUtils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlUtils.d.ts","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;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pE;AAED,MAAM,MAAM,OAAO,GAAG;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAC,CAAC;AAEzE,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CA4B3C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAIxE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAwCjE;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAIzD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAIjE;AAED,gEAAgE;AAChE,wBAAgB,WAAW,CACzB,UAAU,EAAE,MAAM,EAClB,gBAAgB,EAAE,MAAM,EACxB,WAAW,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,MAAM,GAClB,MAAM,CAKR;AAED,iEAAiE;AACjE,wBAAgB,aAAa,CAC3B,cAAc,EAAE,MAAM,EACtB,UAAU,EAAE,MAAM,EAClB,gBAAgB,EAAE,MAAM,EACxB,WAAW,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,MAAM,GAClB,MAAM,CAKR;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAU7D"}
|
package/lib/urlUtils.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.hasSSHProtocol = exports.buildHttpsUrl = exports.buildSshUrl = exports.resolvePathname = exports.serializeURLPath = exports.parseURLPath = exports.isValidPathname = exports.encodePath = exports.fileToPath = exports.getEditUrl = exports.normalizeUrl = void 0;
|
|
9
|
+
exports.hasSSHProtocol = exports.buildHttpsUrl = exports.buildSshUrl = exports.resolvePathname = exports.serializeURLPath = exports.parseLocalURLPath = exports.parseURLPath = exports.toURLPath = exports.parseURLOrPath = exports.isValidPathname = exports.encodePath = exports.fileToPath = exports.getEditUrl = exports.normalizeUrl = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const resolve_pathname_1 = tslib_1.__importDefault(require("resolve-pathname"));
|
|
12
12
|
/**
|
|
@@ -144,21 +144,18 @@ function isValidPathname(str) {
|
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
exports.isValidPathname = isValidPathname;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
catch (e) {
|
|
157
|
-
throw new Error(`Can't parse URL ${url}${base ? ` with base ${base}` : ''}`, { cause: e });
|
|
158
|
-
}
|
|
147
|
+
function parseURLOrPath(url, base) {
|
|
148
|
+
try {
|
|
149
|
+
// TODO when Node supports it, use URL.parse could be faster?
|
|
150
|
+
// see https://kilianvalkhof.com/2024/javascript/the-problem-with-new-url-and-how-url-parse-fixes-that/
|
|
151
|
+
return new URL(url, base ?? 'https://example.com');
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
throw new Error(`Can't parse URL ${url}${base ? ` with base ${base}` : ''}`, { cause: e });
|
|
159
155
|
}
|
|
160
|
-
|
|
161
|
-
|
|
156
|
+
}
|
|
157
|
+
exports.parseURLOrPath = parseURLOrPath;
|
|
158
|
+
function toURLPath(url) {
|
|
162
159
|
const { pathname } = url;
|
|
163
160
|
// Fixes annoying url.search behavior
|
|
164
161
|
// "" => undefined
|
|
@@ -166,16 +163,16 @@ function parseURLPath(urlPath, fromPath) {
|
|
|
166
163
|
// "?param => "param"
|
|
167
164
|
const search = url.search
|
|
168
165
|
? url.search.slice(1)
|
|
169
|
-
:
|
|
166
|
+
: url.href.includes('?')
|
|
170
167
|
? ''
|
|
171
168
|
: undefined;
|
|
172
169
|
// Fixes annoying url.hash behavior
|
|
173
170
|
// "" => undefined
|
|
174
171
|
// "#" => ""
|
|
175
|
-
// "
|
|
172
|
+
// "#param => "param"
|
|
176
173
|
const hash = url.hash
|
|
177
174
|
? url.hash.slice(1)
|
|
178
|
-
:
|
|
175
|
+
: url.href.includes('#')
|
|
179
176
|
? ''
|
|
180
177
|
: undefined;
|
|
181
178
|
return {
|
|
@@ -184,7 +181,57 @@ function parseURLPath(urlPath, fromPath) {
|
|
|
184
181
|
hash,
|
|
185
182
|
};
|
|
186
183
|
}
|
|
184
|
+
exports.toURLPath = toURLPath;
|
|
185
|
+
/**
|
|
186
|
+
* Let's name the concept of (pathname + search + hash) as URLPath
|
|
187
|
+
* See also https://twitter.com/kettanaito/status/1741768992866308120
|
|
188
|
+
* Note: this function also resolves relative pathnames while parsing!
|
|
189
|
+
*/
|
|
190
|
+
function parseURLPath(urlPath, fromPath) {
|
|
191
|
+
const base = fromPath ? parseURLOrPath(fromPath) : undefined;
|
|
192
|
+
const url = parseURLOrPath(urlPath, base);
|
|
193
|
+
return toURLPath(url);
|
|
194
|
+
}
|
|
187
195
|
exports.parseURLPath = parseURLPath;
|
|
196
|
+
/**
|
|
197
|
+
* This returns results for strings like "foo", "../foo", "./foo.mdx?qs#hash"
|
|
198
|
+
* Unlike "parseURLPath()" above, this will not resolve the pathnames
|
|
199
|
+
* Te returned pathname of "../../foo.mdx" will be "../../foo.mdx", not "/foo"
|
|
200
|
+
* This returns null if the url is not "local" (contains domain/protocol etc)
|
|
201
|
+
*/
|
|
202
|
+
function parseLocalURLPath(urlPath) {
|
|
203
|
+
// Workaround because URL("") requires a protocol
|
|
204
|
+
const unspecifiedProtocol = 'unspecified:';
|
|
205
|
+
const url = parseURLOrPath(urlPath, `${unspecifiedProtocol}//`);
|
|
206
|
+
// Ignore links with specified protocol / host
|
|
207
|
+
// (usually fully qualified links starting with https://)
|
|
208
|
+
if (url.protocol !== unspecifiedProtocol ||
|
|
209
|
+
url.host !== '' ||
|
|
210
|
+
url.username !== '' ||
|
|
211
|
+
url.password !== '') {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
// We can't use "new URL()" result because it always tries to resolve urls
|
|
215
|
+
// IE it will remove any "./" or "../" in the pathname, which we don't want
|
|
216
|
+
// We have to parse it manually...
|
|
217
|
+
let localUrlPath = urlPath;
|
|
218
|
+
// Extract and remove the #hash part
|
|
219
|
+
const hashIndex = localUrlPath.indexOf('#');
|
|
220
|
+
const hash = hashIndex !== -1 ? localUrlPath.substring(hashIndex + 1) : undefined;
|
|
221
|
+
localUrlPath =
|
|
222
|
+
hashIndex !== -1 ? localUrlPath.substring(0, hashIndex) : localUrlPath;
|
|
223
|
+
// Extract and remove ?search part
|
|
224
|
+
const searchIndex = localUrlPath.indexOf('?');
|
|
225
|
+
const search = searchIndex !== -1 ? localUrlPath.substring(searchIndex + 1) : undefined;
|
|
226
|
+
localUrlPath =
|
|
227
|
+
searchIndex !== -1 ? localUrlPath.substring(0, searchIndex) : localUrlPath;
|
|
228
|
+
return {
|
|
229
|
+
pathname: localUrlPath,
|
|
230
|
+
search,
|
|
231
|
+
hash,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
exports.parseLocalURLPath = parseLocalURLPath;
|
|
188
235
|
function serializeURLPath(urlPath) {
|
|
189
236
|
const search = urlPath.search === undefined ? '' : `?${urlPath.search}`;
|
|
190
237
|
const hash = urlPath.hash === undefined ? '' : `#${urlPath.hash}`;
|
package/lib/urlUtils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlUtils.js","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gFAAqD;AAErD;;;;;;;;;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EnB,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,aAAa,UAAU,IAAI,UAAU,IAAI,gBAAgB,IAAI,WAAW,MAAM,CAAC;IACxF,CAAC;IACD,OAAO,OAAO,UAAU,IAAI,gBAAgB,IAAI,WAAW,MAAM,CAAC;AACpE,CAAC;AAVD,kCAUC;AAED,iEAAiE;AACjE,SAAgB,aAAa,CAC3B,cAAsB,EACtB,UAAkB,EAClB,gBAAwB,EACxB,WAAmB,EACnB,UAAmB;IAEnB,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,WAAW,cAAc,IAAI,UAAU,IAAI,UAAU,IAAI,gBAAgB,IAAI,WAAW,MAAM,CAAC;IACxG,CAAC;IACD,OAAO,WAAW,cAAc,IAAI,UAAU,IAAI,gBAAgB,IAAI,WAAW,MAAM,CAAC;AAC1F,CAAC;AAXD,sCAWC;AAED;;;;GAIG;AACH,SAAgB,cAAc,CAAC,aAAqB;IAClD,IAAI,CAAC;QACH,IAAI,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;YAC/C,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACP,oCAAoC;QACpC,OAAO,+BAA+B,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC7D,CAAC;AACH,CAAC;AAVD,wCAUC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/utils",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-5957",
|
|
4
4
|
"description": "Node utility functions for Docusaurus packages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/logger": "0.0.0-
|
|
22
|
-
"@docusaurus/utils-common": "0.0.0-
|
|
21
|
+
"@docusaurus/logger": "0.0.0-5957",
|
|
22
|
+
"@docusaurus/utils-common": "0.0.0-5957",
|
|
23
23
|
"@svgr/webpack": "^8.1.0",
|
|
24
24
|
"escape-string-regexp": "^4.0.0",
|
|
25
25
|
"file-loader": "^6.2.0",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"node": ">=18.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@docusaurus/types": "0.0.0-
|
|
45
|
+
"@docusaurus/types": "0.0.0-5957",
|
|
46
46
|
"@types/dedent": "^0.7.0",
|
|
47
47
|
"@types/github-slugger": "^1.3.0",
|
|
48
48
|
"@types/micromatch": "^4.0.2",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"optional": true
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "edb3a453b0b3a853eadfc4e9ec44d47ea76d5002"
|
|
62
62
|
}
|
package/src/index.ts
CHANGED
|
@@ -44,6 +44,9 @@ export {
|
|
|
44
44
|
isValidPathname,
|
|
45
45
|
resolvePathname,
|
|
46
46
|
parseURLPath,
|
|
47
|
+
parseLocalURLPath,
|
|
48
|
+
parseURLOrPath,
|
|
49
|
+
toURLPath,
|
|
47
50
|
serializeURLPath,
|
|
48
51
|
hasSSHProtocol,
|
|
49
52
|
buildHttpsUrl,
|
|
@@ -71,11 +74,7 @@ export {
|
|
|
71
74
|
writeMarkdownHeadingId,
|
|
72
75
|
type WriteHeadingIDOptions,
|
|
73
76
|
} from './markdownUtils';
|
|
74
|
-
export {
|
|
75
|
-
type ContentPaths,
|
|
76
|
-
type BrokenMarkdownLink,
|
|
77
|
-
replaceMarkdownLinks,
|
|
78
|
-
} from './markdownLinks';
|
|
77
|
+
export {type ContentPaths, resolveMarkdownLinkPathname} from './markdownLinks';
|
|
79
78
|
export {type SluggerOptions, type Slugger, createSlugger} from './slugger';
|
|
80
79
|
export {
|
|
81
80
|
isNameTooLong,
|
package/src/markdownLinks.ts
CHANGED
|
@@ -40,159 +40,35 @@ export type BrokenMarkdownLink<T extends ContentPaths> = {
|
|
|
40
40
|
link: string;
|
|
41
41
|
};
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
// Note this is historical logic extracted during a 2024 refactor
|
|
44
|
+
// The algo has been kept exactly as before for retro compatibility
|
|
45
|
+
// See also https://github.com/facebook/docusaurus/pull/10168
|
|
46
|
+
export function resolveMarkdownLinkPathname(
|
|
47
|
+
linkPathname: string,
|
|
48
|
+
context: {
|
|
49
|
+
sourceFilePath: string;
|
|
50
|
+
sourceToPermalink: {[aliasedFilePath: string]: string};
|
|
51
|
+
contentPaths: ContentPaths;
|
|
52
|
+
siteDir: string;
|
|
53
|
+
},
|
|
54
|
+
): string | null {
|
|
55
|
+
const {sourceFilePath, sourceToPermalink, contentPaths, siteDir} = context;
|
|
56
|
+
const sourceDirsToTry: string[] = [];
|
|
57
|
+
// ./file.md and ../file.md are always relative to the current file
|
|
58
|
+
if (!linkPathname.startsWith('./') && !linkPathname.startsWith('../')) {
|
|
59
|
+
sourceDirsToTry.push(...getContentPathList(contentPaths), siteDir);
|
|
60
|
+
}
|
|
61
|
+
// /file.md is never relative to the source file path
|
|
62
|
+
if (!linkPathname.startsWith('/')) {
|
|
63
|
+
sourceDirsToTry.push(path.dirname(sourceFilePath));
|
|
53
64
|
}
|
|
54
|
-
return {
|
|
55
|
-
type: match.groups!.fence![0]! as '`' | '~',
|
|
56
|
-
definitelyOpen: !!match.groups!.rest!,
|
|
57
|
-
count: match.groups!.fence!.length,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Takes a Markdown file and replaces relative file references with their URL
|
|
63
|
-
* counterparts, e.g. `[link](./intro.md)` => `[link](/docs/intro)`, preserving
|
|
64
|
-
* everything else.
|
|
65
|
-
*
|
|
66
|
-
* This method uses best effort to find a matching file. The file reference can
|
|
67
|
-
* be relative to the directory of the current file (most likely) or any of the
|
|
68
|
-
* content paths (so `/tutorials/intro.md` can be resolved as
|
|
69
|
-
* `<siteDir>/docs/tutorials/intro.md`). Links that contain the `http(s):` or
|
|
70
|
-
* `@site/` prefix will always be ignored.
|
|
71
|
-
*/
|
|
72
|
-
export function replaceMarkdownLinks<T extends ContentPaths>({
|
|
73
|
-
siteDir,
|
|
74
|
-
fileString,
|
|
75
|
-
filePath,
|
|
76
|
-
contentPaths,
|
|
77
|
-
sourceToPermalink,
|
|
78
|
-
}: {
|
|
79
|
-
/** Absolute path to the site directory, used to resolve aliased paths. */
|
|
80
|
-
siteDir: string;
|
|
81
|
-
/** The Markdown file content to be processed. */
|
|
82
|
-
fileString: string;
|
|
83
|
-
/** Absolute path to the current file containing `fileString`. */
|
|
84
|
-
filePath: string;
|
|
85
|
-
/** The content paths which the file reference may live in. */
|
|
86
|
-
contentPaths: T;
|
|
87
|
-
/**
|
|
88
|
-
* A map from source paths to their URLs. Source paths are `@site` aliased.
|
|
89
|
-
*/
|
|
90
|
-
sourceToPermalink: {[aliasedPath: string]: string};
|
|
91
|
-
}): {
|
|
92
|
-
/**
|
|
93
|
-
* The content with all Markdown file references replaced with their URLs.
|
|
94
|
-
* Unresolved links are left as-is.
|
|
95
|
-
*/
|
|
96
|
-
newContent: string;
|
|
97
|
-
/** The list of broken links, */
|
|
98
|
-
brokenMarkdownLinks: BrokenMarkdownLink<T>[];
|
|
99
|
-
} {
|
|
100
|
-
const brokenMarkdownLinks: BrokenMarkdownLink<T>[] = [];
|
|
101
|
-
|
|
102
|
-
// Replace internal markdown linking (except in fenced blocks).
|
|
103
|
-
let lastOpenCodeFence: CodeFence | null = null;
|
|
104
|
-
const lines = fileString.split('\n').map((line) => {
|
|
105
|
-
const codeFence = parseCodeFence(line);
|
|
106
|
-
if (codeFence) {
|
|
107
|
-
if (!lastOpenCodeFence) {
|
|
108
|
-
lastOpenCodeFence = codeFence;
|
|
109
|
-
} else if (
|
|
110
|
-
!codeFence.definitelyOpen &&
|
|
111
|
-
lastOpenCodeFence.type === codeFence.type &&
|
|
112
|
-
lastOpenCodeFence.count <= codeFence.count
|
|
113
|
-
) {
|
|
114
|
-
// All three conditions must be met in order for this to be considered
|
|
115
|
-
// a closing fence.
|
|
116
|
-
lastOpenCodeFence = null;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (lastOpenCodeFence) {
|
|
120
|
-
return line;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
let modifiedLine = line;
|
|
124
|
-
// Replace inline-style links or reference-style links e.g:
|
|
125
|
-
// This is [Document 1](doc1.md)
|
|
126
|
-
// [doc1]: doc1.md
|
|
127
|
-
const linkTitlePattern = '(?:\\s+(?:\'.*?\'|".*?"|\\(.*?\\)))?';
|
|
128
|
-
const linkSuffixPattern = '(?:\\?[^#>\\s]+)?(?:#[^>\\s]+)?';
|
|
129
|
-
const linkCapture = (forbidden: string) =>
|
|
130
|
-
`((?!https?://|@site/)[^${forbidden}#?]+)`;
|
|
131
|
-
const linkURLPattern = `(?:(?!<)${linkCapture(
|
|
132
|
-
'()\\s',
|
|
133
|
-
)}${linkSuffixPattern}|<${linkCapture('>')}${linkSuffixPattern}>)`;
|
|
134
|
-
const linkPattern = new RegExp(
|
|
135
|
-
`\\[(?:(?!\\]\\().)*\\]\\(\\s*${linkURLPattern}${linkTitlePattern}\\s*\\)|^\\s*\\[[^[\\]]*[^[\\]\\s][^[\\]]*\\]:\\s*${linkURLPattern}${linkTitlePattern}$`,
|
|
136
|
-
'dgm',
|
|
137
|
-
);
|
|
138
|
-
let mdMatch = linkPattern.exec(modifiedLine);
|
|
139
|
-
while (mdMatch !== null) {
|
|
140
|
-
// Replace it to correct html link.
|
|
141
|
-
const mdLink = mdMatch.slice(1, 5).find(Boolean)!;
|
|
142
|
-
const mdLinkRange = mdMatch.indices!.slice(1, 5).find(Boolean)!;
|
|
143
|
-
if (!/\.mdx?$/.test(mdLink)) {
|
|
144
|
-
mdMatch = linkPattern.exec(modifiedLine);
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const sourcesToTry: string[] = [];
|
|
149
|
-
// ./file.md and ../file.md are always relative to the current file
|
|
150
|
-
if (!mdLink.startsWith('./') && !mdLink.startsWith('../')) {
|
|
151
|
-
sourcesToTry.push(...getContentPathList(contentPaths), siteDir);
|
|
152
|
-
}
|
|
153
|
-
// /file.md is always relative to the content path
|
|
154
|
-
if (!mdLink.startsWith('/')) {
|
|
155
|
-
sourcesToTry.push(path.dirname(filePath));
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const aliasedSourceMatch = sourcesToTry
|
|
159
|
-
.map((p) => path.join(p, decodeURIComponent(mdLink)))
|
|
160
|
-
.map((source) => aliasedSitePath(source, siteDir))
|
|
161
|
-
.find((source) => sourceToPermalink[source]);
|
|
162
|
-
|
|
163
|
-
const permalink: string | undefined = aliasedSourceMatch
|
|
164
|
-
? sourceToPermalink[aliasedSourceMatch]
|
|
165
|
-
: undefined;
|
|
166
|
-
|
|
167
|
-
if (permalink) {
|
|
168
|
-
// MDX won't be happy if the permalink contains a space, we need to
|
|
169
|
-
// convert it to %20
|
|
170
|
-
const encodedPermalink = permalink
|
|
171
|
-
.split('/')
|
|
172
|
-
.map((part) => part.replace(/\s/g, '%20'))
|
|
173
|
-
.join('/');
|
|
174
|
-
modifiedLine = `${modifiedLine.slice(
|
|
175
|
-
0,
|
|
176
|
-
mdLinkRange[0],
|
|
177
|
-
)}${encodedPermalink}${modifiedLine.slice(mdLinkRange[1])}`;
|
|
178
|
-
// Adjust the lastIndex to avoid passing over the next link if the
|
|
179
|
-
// newly replaced URL is shorter.
|
|
180
|
-
linkPattern.lastIndex += encodedPermalink.length - mdLink.length;
|
|
181
|
-
} else {
|
|
182
|
-
const brokenMarkdownLink: BrokenMarkdownLink<T> = {
|
|
183
|
-
contentPaths,
|
|
184
|
-
filePath,
|
|
185
|
-
link: mdLink,
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
brokenMarkdownLinks.push(brokenMarkdownLink);
|
|
189
|
-
}
|
|
190
|
-
mdMatch = linkPattern.exec(modifiedLine);
|
|
191
|
-
}
|
|
192
|
-
return modifiedLine;
|
|
193
|
-
});
|
|
194
65
|
|
|
195
|
-
const
|
|
66
|
+
const aliasedSourceMatch = sourceDirsToTry
|
|
67
|
+
.map((sourceDir) => path.join(sourceDir, decodeURIComponent(linkPathname)))
|
|
68
|
+
.map((source) => aliasedSitePath(source, siteDir))
|
|
69
|
+
.find((source) => sourceToPermalink[source]);
|
|
196
70
|
|
|
197
|
-
return
|
|
71
|
+
return aliasedSourceMatch
|
|
72
|
+
? sourceToPermalink[aliasedSourceMatch] ?? null
|
|
73
|
+
: null;
|
|
198
74
|
}
|
package/src/urlUtils.ts
CHANGED
|
@@ -164,27 +164,22 @@ export function isValidPathname(str: string): boolean {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
export
|
|
168
|
-
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
} catch (e) {
|
|
178
|
-
throw new Error(
|
|
179
|
-
`Can't parse URL ${url}${base ? ` with base ${base}` : ''}`,
|
|
180
|
-
{cause: e},
|
|
181
|
-
);
|
|
182
|
-
}
|
|
167
|
+
export function parseURLOrPath(url: string, base?: string | URL): URL {
|
|
168
|
+
try {
|
|
169
|
+
// TODO when Node supports it, use URL.parse could be faster?
|
|
170
|
+
// see https://kilianvalkhof.com/2024/javascript/the-problem-with-new-url-and-how-url-parse-fixes-that/
|
|
171
|
+
return new URL(url, base ?? 'https://example.com');
|
|
172
|
+
} catch (e) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Can't parse URL ${url}${base ? ` with base ${base}` : ''}`,
|
|
175
|
+
{cause: e},
|
|
176
|
+
);
|
|
183
177
|
}
|
|
178
|
+
}
|
|
184
179
|
|
|
185
|
-
|
|
186
|
-
const url = parseURL(urlPath, base);
|
|
180
|
+
export type URLPath = {pathname: string; search?: string; hash?: string};
|
|
187
181
|
|
|
182
|
+
export function toURLPath(url: URL): URLPath {
|
|
188
183
|
const {pathname} = url;
|
|
189
184
|
|
|
190
185
|
// Fixes annoying url.search behavior
|
|
@@ -193,17 +188,17 @@ export function parseURLPath(urlPath: string, fromPath?: string): URLPath {
|
|
|
193
188
|
// "?param => "param"
|
|
194
189
|
const search = url.search
|
|
195
190
|
? url.search.slice(1)
|
|
196
|
-
:
|
|
191
|
+
: url.href.includes('?')
|
|
197
192
|
? ''
|
|
198
193
|
: undefined;
|
|
199
194
|
|
|
200
195
|
// Fixes annoying url.hash behavior
|
|
201
196
|
// "" => undefined
|
|
202
197
|
// "#" => ""
|
|
203
|
-
// "
|
|
198
|
+
// "#param => "param"
|
|
204
199
|
const hash = url.hash
|
|
205
200
|
? url.hash.slice(1)
|
|
206
|
-
:
|
|
201
|
+
: url.href.includes('#')
|
|
207
202
|
? ''
|
|
208
203
|
: undefined;
|
|
209
204
|
|
|
@@ -214,6 +209,65 @@ export function parseURLPath(urlPath: string, fromPath?: string): URLPath {
|
|
|
214
209
|
};
|
|
215
210
|
}
|
|
216
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Let's name the concept of (pathname + search + hash) as URLPath
|
|
214
|
+
* See also https://twitter.com/kettanaito/status/1741768992866308120
|
|
215
|
+
* Note: this function also resolves relative pathnames while parsing!
|
|
216
|
+
*/
|
|
217
|
+
export function parseURLPath(urlPath: string, fromPath?: string): URLPath {
|
|
218
|
+
const base = fromPath ? parseURLOrPath(fromPath) : undefined;
|
|
219
|
+
const url = parseURLOrPath(urlPath, base);
|
|
220
|
+
return toURLPath(url);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* This returns results for strings like "foo", "../foo", "./foo.mdx?qs#hash"
|
|
225
|
+
* Unlike "parseURLPath()" above, this will not resolve the pathnames
|
|
226
|
+
* Te returned pathname of "../../foo.mdx" will be "../../foo.mdx", not "/foo"
|
|
227
|
+
* This returns null if the url is not "local" (contains domain/protocol etc)
|
|
228
|
+
*/
|
|
229
|
+
export function parseLocalURLPath(urlPath: string): URLPath | null {
|
|
230
|
+
// Workaround because URL("") requires a protocol
|
|
231
|
+
const unspecifiedProtocol = 'unspecified:';
|
|
232
|
+
|
|
233
|
+
const url = parseURLOrPath(urlPath, `${unspecifiedProtocol}//`);
|
|
234
|
+
// Ignore links with specified protocol / host
|
|
235
|
+
// (usually fully qualified links starting with https://)
|
|
236
|
+
if (
|
|
237
|
+
url.protocol !== unspecifiedProtocol ||
|
|
238
|
+
url.host !== '' ||
|
|
239
|
+
url.username !== '' ||
|
|
240
|
+
url.password !== ''
|
|
241
|
+
) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// We can't use "new URL()" result because it always tries to resolve urls
|
|
246
|
+
// IE it will remove any "./" or "../" in the pathname, which we don't want
|
|
247
|
+
// We have to parse it manually...
|
|
248
|
+
let localUrlPath = urlPath;
|
|
249
|
+
|
|
250
|
+
// Extract and remove the #hash part
|
|
251
|
+
const hashIndex = localUrlPath.indexOf('#');
|
|
252
|
+
const hash =
|
|
253
|
+
hashIndex !== -1 ? localUrlPath.substring(hashIndex + 1) : undefined;
|
|
254
|
+
localUrlPath =
|
|
255
|
+
hashIndex !== -1 ? localUrlPath.substring(0, hashIndex) : localUrlPath;
|
|
256
|
+
|
|
257
|
+
// Extract and remove ?search part
|
|
258
|
+
const searchIndex = localUrlPath.indexOf('?');
|
|
259
|
+
const search =
|
|
260
|
+
searchIndex !== -1 ? localUrlPath.substring(searchIndex + 1) : undefined;
|
|
261
|
+
localUrlPath =
|
|
262
|
+
searchIndex !== -1 ? localUrlPath.substring(0, searchIndex) : localUrlPath;
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
pathname: localUrlPath,
|
|
266
|
+
search,
|
|
267
|
+
hash,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
217
271
|
export function serializeURLPath(urlPath: URLPath): string {
|
|
218
272
|
const search = urlPath.search === undefined ? '' : `?${urlPath.search}`;
|
|
219
273
|
const hash = urlPath.hash === undefined ? '' : `#${urlPath.hash}`;
|