import { attrs } from "@mdit/plugin-attrs"; import MarkdownIt from "markdown-it"; import type { MarkdownIt as MarkdownItType } from "markdown-it"; import { describe, expect, it } from "vitest"; import { sub } from "../src/index.js"; const markdownIt = new MarkdownIt({ linkify: true }).use(sub); describe(sub, () => { it("should render", () => { expect(markdownIt.render(`~test~`)).toBe("
test
\n"); }); it("should not render when escape", () => { expect(markdownIt.render(String.raw`~foo\~`)).toBe("~foo~
\n"); expect(markdownIt.render(String.raw`\~foo~`)).toBe("~foo~
\n"); }); it("should not render when having spaces", () => { expect(markdownIt.render(`~foo bar~`)).toBe("~foo bar~
\n"); }); it("should render when spaces are escaped", () => { expect(markdownIt.render(String.raw`~foo\ bar\ baz~`)).toBe("foo bar baz
\n"); expect(markdownIt.render(String.raw`~\ foo\ ~`)).toBe("foo
\n"); }); it(String.raw`should handle multiple '\'`, () => { expect(markdownIt.render(String.raw`~foo\\\\\ bar~`)).toBe("foo\\\\ bar
\n"); expect(markdownIt.render(String.raw`~foo\\\\ bar~`)).toBe("~foo\\\\ bar~
\n"); }); it("should work with other marker", () => { expect(markdownIt.render(`**~foo~ bar**`)).toBe("foo bar
\n"); expect(markdownIt.render(`*~f`)).toBe("*~f
\n"); expect(markdownIt.render(`b*~`)).toBe("b*~
\n"); }); }); describe("sub with attrs plugin", () => { const createMd = (): MarkdownItType => new MarkdownIt({ linkify: true }).use(attrs).use(sub); it("should not leak inline attrs between sub tokens", () => { expect(createMd().render("~a~{.x}\n\n~b~")).toBe( 'a
\nb
\n', ); expect(createMd().render("~a~{.x} ~b~")).toBe('a b
\n'); }); it("should not accumulate attrs across renders", () => { const md = createMd(); const expected = 'a
\nb
\n'; expect(md.render("~a~{.x}\n\n~b~")).toBe(expected); expect(md.render("~a~{.x}\n\n~b~")).toBe(expected); expect(md.render("~c~\n\n~d~")).toBe("c
\nd
\n"); }); it("should keep attrs independent between adjacent tokens", () => { expect(createMd().render("~a~{.x}~b~{.y}")).toBe( 'ab
\n', ); }); });