SPB Git

Repository Public

linkify.test.ts 1.5 KB 28 lines typescript

1import { describe, expect, it } from "vitest";2import { linkify } from "./linkify";34describe("linkify", () => {5  it("splits text and http(s) URLs, keeping line breaks in text segments", () => {6    expect(linkify("Voir https://clustr.spboucher.ai/nodes/1\npuis relancer.")).toEqual([7      { kind: "text", value: "Voir " },8      { kind: "link", value: "https://clustr.spboucher.ai/nodes/1", href: "https://clustr.spboucher.ai/nodes/1" },9      { kind: "text", value: "\npuis relancer." },10    ]);11  });12  it("leaves trailing punctuation out of the link but keeps balanced parentheses", () => {13    expect(linkify("(voir https://example.org/a_(b)), ok.")[1]).toMatchObject({ kind: "link", value: "https://example.org/a_(b)" });14    expect(linkify("https://example.org/x.")[0]).toMatchObject({ value: "https://example.org/x" });15    expect(linkify("https://example.org/x?a=1&b=2!")[0]).toMatchObject({ value: "https://example.org/x?a=1&b=2" });16  });17  it("never links other schemes and never produces markup", () => {18    const text = 'javascript:alert(1) <img src=x onerror=alert(1)> data:text/html,<b> ftp://host/file <a href="https://evil.example">x</a>';19    const out = linkify(text);20    expect(out.filter((s) => s.kind === "link").map((s) => s.value)).toEqual(["https://evil.example"]);21    expect(out.map((s) => s.value).join("")).toBe(text); // nothing added, nothing removed22  });23  it("returns plain text unchanged", () => {24    expect(linkify("rien à lier")).toEqual([{ kind: "text", value: "rien à lier" }]);25    expect(linkify("")).toEqual([]);26  });27});28
Files main
Menu

Theme