Ignore absolute URIs when resolving refs

Do not output bogus warnings in the case of links that goes to other
websites.
This commit is contained in:
Hornwitser 2025-01-25 08:52:08 +01:00
parent cf3229423b
commit ef8aaa6f6d
2 changed files with 9 additions and 1 deletions

View file

@ -48,4 +48,10 @@ suite("function resolveRefs", () => {
resolveRefs(el, "/"); resolveRefs(el, "/");
assert.deepEqual(el, elFn()); assert.deepEqual(el, elFn());
}); });
test("ignores absolute URIs", () => {
const el = <div>Content with <a href="https://example.org/page.html">Link</a></div>;
const resEl = resolveRefs(el, "/");
assert.equal(el, resEl);
});
}); });

View file

@ -26,8 +26,10 @@ export function resolveRefs(node: Node, dir: string) {
&& node.attributes.has("href") && node.attributes.has("href")
) { ) {
const original = node.attributes.get("href")! const original = node.attributes.get("href")!
if (/^[a-z][a-z+.-]*:/i.test(original)) {
// Ignore refs that start with a URI scheme.
/* node:coverage ignore next 3 */ /* node:coverage ignore next 3 */
if (!original.startsWith("/")) { } else if (!original.startsWith("/")) {
console.log(`Warning: found relative href to ${original}`); console.log(`Warning: found relative href to ${original}`);
} else { } else {
const ref = posix.relative(dir, original); const ref = posix.relative(dir, original);