import rehypeExtendedLinks from "../lib/index.js"; import type { Options } from "../lib/index.ts"; import { assert, describe, it } from "vitest"; import { rehype } from "rehype"; describe("Package", () => { it("should expose the public api", () => { assert.isFunction(rehypeExtendedLinks); }); }); describe("Link type check", () => { // input: ? // expected: ? it("should not change a relative link", async () => { const input = `relative`; const result = await process(input, {}); const output = `relative`; assert.equal(result, output); }); it("should not change a fragment link", async () => { const input = 'fragment'; const expected = 'fragment'; const result = await process(input, {}); assert.equal(result, expected); }); it("should not change a search link", async () => { const input = `search`; const expected = 'search'; const result = await process(input, {}); assert.equal(result, expected); }); it("should not change a mailto link", async () => { const input = 'mailto'; const expected = 'mailto'; const result = await process(input, {}); assert.equal(result, expected); }); // input: ? // expected: ? it("should change a protocol-relative link", async () => { const input = `?`; const result = await process(input, {}); const output = `?`; assert.equal(result, output); }); it("should change a http link", async () => { const input = 'http'; const expected = 'http'; const result = await process(input, {}); assert.equal(result, expected); }); it("should change a https link", async () => { const input = 'https'; const expected = 'https'; const result = await process(input, {}); assert.equal(result, expected); }); it("should not change a www link", async () => { const input = 'www'; const expected = 'www'; const result = await process(input, {}); assert.equal(result, expected); }); }); describe("Options check", () => { it("should wrap in span because content exist", async () => { const input = `?`; const options: Options = { content: { type: "element", tagName: "span", properties: {}, children: [], }, }; const result = await process(input, options); const output = `?`; assert.equal(result, output); }); it("should add preContent", async () => { const input = `?`; const options: Options = { preContent: { type: "element", tagName: "span", properties: { className: ["content"] }, children: [{ type: "text", value: "content" }], }, }; const result = await process(input, options); const output = `content?`; assert.equal(result, output); }); it("should wrap in span if wrappedProperties contains", async () => { const input = `?`; const options: Options = { wrappedProperties: { className: ["link desc"], }, preContent: { type: "element", tagName: "span", properties: {}, children: [{ type: "text", value: "pre content" }], }, }; const result = await process(input, options); const output = `pre content?`; assert.equal(result, output); }); }); describe("Dynamically generated tests", () => { const dynPreContentOptions: Options = { preContent(node) { const url = node.properties.href; if (!url) return undefined; const regex = /^(https?:\/\/)?(www\.)?github\.com\/.*/i; if (!regex.test(url as string)) return undefined; return { type: "element", tagName: "span", properties: {}, children: [ { type: "element", tagName: "svg", properties: {}, children: [ { type: "element", tagName: "use", properties: { href: "#mdi--github", }, children: [], }, ], }, ], }; }, }; it("should add preContent to a link that matches the test function", async () => { const input = `?`; const result = await process(input, dynPreContentOptions); const output = `?`; assert.equal(result, output); }); it("should not add preContent to a link that matches the test function", async () => { const input = `?`; const result = await process(input, dynPreContentOptions); const output = `?`; assert.equal(result, output); }); it("should add rel to a link that matches the test function", async () => { const input = 'http'; const options: Options = { test: (node) => { return node.properties.href === "http://example.com"; }, }; const output = 'http'; const result = await process(input, options); assert.equal(result, output); }); it("should not add rel to a link that does not match the test function", async () => { const input = 'http'; const options: Options = { test: (node) => { return node.properties.href === "http://foobar.com"; }, }; const output = 'http'; const result = await process(input, options); assert.equal(result, output); }); }); async function process(input: string, options: Options): Promise { const result = await rehype() .use({ settings: { fragment: true } }) .use(rehypeExtendedLinks as any, options) .process(input); return result.toString(); }