Skip to content

Commit

Permalink
feat(compatibility): Drop deprecated url.parse() NodeJS-API. (#178)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Unfortunately, there is no replacement for
the old API that is fully compatible. As a consequence this
change affects the way paths are being URL-encoded. Previously
all paths (relative or absolute) have been passed through
`url.parse(path).format()` and thereby got URL-encoded. With
this change encoded URLs are only created for option
`paths: "absolute"` in combination with a `baseUrl` other
than "/". The encoding may changed. Values for `baseUrl`
(if present) now *must* be "/" or *must* be URLs which
conform to WHATWG URL specification and which can be passed
as a 'base' parameter to Node's URL implemenation
(see https://docs.nodejs.org/api/url.html).

* test: New baseline.
  • Loading branch information
about-code committed Sep 12, 2021
1 parent 064986f commit ed8ffc8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
61 changes: 39 additions & 22 deletions lib/path/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ export function toReproducablePath(fullPath, redactString) {
export function getFileLinkUrl(context, pathFrom, pathTo, anchor) {
const { outDir, linking } = context.conf;
const { baseUrl, paths, pathComponents } = linking;
let urlBase = "";
let urlPath = "";
let urlHash = "";

let targetUrl = {
base: ""
,path: ""
,anchor: ""
};
if (anchor) {
targetUrl.anchor = anchor[0] === "#" ? anchor : `#${anchor}`;
urlHash = anchor && anchor[0] === "#" ? anchor : `#${anchor}`;
}
if (paths === "relative") {
const p = toForwardSlash(
Expand All @@ -138,25 +136,44 @@ export function getFileLinkUrl(context, pathFrom, pathTo, anchor) {
)
);
if (p !== "./") {
targetUrl.path = p;
} // else: inner link within the same file
urlPath = getPathFromComponents(p, pathComponents);
} // else: inner link within the same file doesn't need a path
} else if (paths === "absolute") {
if (baseUrl) {
targetUrl.base = baseUrl.replace(/^(.*)(\/|\\)$/, "$1");
targetUrl.path = toForwardSlash(path.resolve(outDir, pathTo))
.replace(outDir, "");
if (! baseUrl) {
urlPath = toForwardSlash(path.resolve(outDir, pathTo));
urlPath = getPathFromComponents(urlPath, pathComponents);
} else if (baseUrl === "/") {
urlBase = baseUrl;
urlPath = toForwardSlash(path.resolve(outDir, pathTo)).replace(outDir, "");
urlPath = getPathFromComponents(urlPath, pathComponents);
} else {
targetUrl.path = toForwardSlash(path.resolve(outDir, pathTo));
urlBase = url.format(new URL("/", baseUrl), { unicode: true });
urlPath = toForwardSlash(path.resolve(outDir, pathTo)).replace(outDir, "");
urlPath = getPathFromComponents(urlPath, pathComponents);
const urlPlain = concat(urlBase, urlPath, urlHash);
return url.format(new URL(urlPlain), { unicode: true });
}
}
if (pathComponents && targetUrl.path) {
const vFile = new VFile({path: targetUrl.path});
const pathTemplate = pathComponents.join(",");
const path_ = /path/.test(pathTemplate) ? (vFile.dirname + "/").replace("//", "/") : "";
const file = /file/.test(pathTemplate) ? vFile.stem : "";
const ext = /ext/.test(pathTemplate) ? vFile.extname : "";
targetUrl.path = `${path_}${file}${ext}`;
return concat(urlBase, urlPath, urlHash);
}

// const TRAILING_SLASHES = /^(.*)([/\\]{1,})$/;
const LEADING_SLASHES = /^([/\\]{1,})(.*)$/;

function getPathFromComponents(path, components) {
if (path && components) {
const vFile = new VFile({path: path});
const pathTemplate = components.join(",");
const ext = /ext/.test(pathTemplate) ? vFile.extname : "";
const file = /file/.test(pathTemplate) ? vFile.stem : "";
let path_ = /path/.test(pathTemplate) ? vFile.dirname + "/" : "";
path_ = path_.replace(LEADING_SLASHES, "$2");
return `${path_}${file}${ext}`;
} else {
return path;
}
const result = `${targetUrl.base}${targetUrl.path}${targetUrl.anchor}`;
return url.parse(result).format();
}
function concat(base, path, hash) {
path = path.replace(LEADING_SLASHES, "$2"); // remove leading slash
return `${base}${path}${hash}`.trim();
}
2 changes: 1 addition & 1 deletion test/output-expected/config-listOf/basic/list-of-label.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

[1]: ./document-infer-label.md "./document-infer-label.md"

[2]: ./document-infer-label.md#label-test-Case-B:-Label-from-%27id%27 "Test Case B: Label from 'id'"
[2]: ./document-infer-label.md#label-test-Case-B:-Label-from-'id' "Test Case B: Label from 'id'"

[3]: ./document-infer-label.md#label-no-title-but-text "Test Case C: 'innerText'"

Expand Down

0 comments on commit ed8ffc8

Please sign in to comment.