Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Drop legacy url.parse() NodeJS API #178

Merged
merged 2 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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