Skip to content

Commit

Permalink
chore: add absolute url tests
Browse files Browse the repository at this point in the history
  • Loading branch information
techfg committed Apr 13, 2024
1 parent f206c59 commit 5172cc8
Showing 1 changed file with 39 additions and 1 deletion.
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 @@ -286,6 +286,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

0 comments on commit 5172cc8

Please sign in to comment.