Skip to content

Commit

Permalink
Merge pull request #36 from techfg/chore/improve-relative-link-valida…
Browse files Browse the repository at this point in the history
…tion

chore: improve relative link validation
  • Loading branch information
vernak2539 committed May 3, 2024
2 parents f428402 + 86a579f commit 0e37ee1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"debug": "^4.3.4",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"is-absolute-url": "^4.0.1",
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0",
"zod": "^3.22.4"
Expand Down
40 changes: 39 additions & 1 deletion src/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import { test, describe } from "node:test";
import { fileURLToPath } from "url";
import { fileURLToPath, pathToFileURL } from "url";
import path, { dirname } from "path";
import { rehype } from "rehype";
import { visit } from "unist-util-visit";
Expand Down Expand Up @@ -320,6 +320,44 @@ describe("astroRehypeRelativeMarkdownLinks", () => {

assert.equal(actual, expected);
});

test("should not replace external http url", async () => {
const input = `<a href="https://www.foo.com/fixtures/test.md">foo</a>`;
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src" })
.process(input);

const expected = `<html><head></head><body><a href="https://www.foo.com/fixtures/test.md">foo</a></body></html>`;

assert.equal(actual, expected);
});

test("should not replace invalid file url containing relative path", async () => {
const input = `<a href="file://./fixtures/test.md">foo</a>`;
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src" })
.process(input);

const expected = `<html><head></head><body><a href="file://./fixtures/test.md">foo</a></body></html>`;

assert.equal(actual, expected);
});

test("should not replace valid file url containing absolute path", async () => {
const absolutePath = path.resolve("./fixtures/test.md");
const url = pathToFileURL(absolutePath);
const input = `<a href="${url}">foo</a>`;
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src" })
.process(input);

const expected = `<html><head></head><body><a href="${url}">foo</a></body></html>`;

assert.equal(actual, expected);
});
});

describe("config option validation", () => {
Expand Down
7 changes: 6 additions & 1 deletion src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { statSync } from "fs";
import { slug as githubSlug } from "github-slugger";
import { z } from "zod";
import { asError } from "catch-unknown";
import isAbsoluteUrl from "is-absolute-url";

const validMarkdownExtensions = [".md", ".mdx"];
const isWindows =
Expand Down Expand Up @@ -40,14 +41,18 @@ export const isValidRelativeLink = (link) => {
return false;
}

if (!validMarkdownExtensions.includes(path.extname(link))) {
if (isAbsoluteUrl(link)) {
return false;
}

if (path.isAbsolute(link)) {
return false;
}

if (!validMarkdownExtensions.includes(path.extname(link))) {
return false;
}

return true;
};

Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ __metadata:
esmock: "npm:^2.6.4"
github-slugger: "npm:^2.0.0"
gray-matter: "npm:^4.0.3"
is-absolute-url: "npm:^4.0.1"
prettier: "npm:^4.0.0-alpha.8"
rehype: "npm:^13.0.1"
typedoc: "npm:^0.25.13"
Expand Down Expand Up @@ -524,6 +525,13 @@ __metadata:
languageName: node
linkType: hard

"is-absolute-url@npm:^4.0.1":
version: 4.0.1
resolution: "is-absolute-url@npm:4.0.1"
checksum: 10c0/6f8f603945bd9f2c6031758bbc12352fc647bd5d807cad10d96cc6300fd0e15240cc091521a61db767e4ec0bacff257b4f1015fd5249c147bbb4a4497356c72e
languageName: node
linkType: hard

"is-binary-path@npm:^2.1.0":
version: 2.1.0
resolution: "is-binary-path@npm:2.1.0"
Expand Down

0 comments on commit 0e37ee1

Please sign in to comment.