1import { describe, expect, it } from "vite-plus/test";
2
3import { inlineMarkdownUnitMarks, parseInlineMarkdown } from "./composer-rich-text";
4
5describe("composer rich text markdown", () => {
6 it("preserves unmatched markers, escaped markers, and identifiers", () => {
7 for (const text of [
8 "plain text",
9 "snake_case",
10 "unmatched **",
11 "** spaced **",
12 "\\*literal\\*",
13 ]) {
14 expect(parseInlineMarkdown(text)).toEqual([{ text, marks: [] }]);
15 }
16 });
17
18 it("renders triple markers and nested styles", () => {
19 expect(parseInlineMarkdown("***both***")).toEqual([
20 { text: "both", marks: ["bold", "italic"] },
21 ]);
22 expect(parseInlineMarkdown("*a **b** c*")).toEqual([
23 { text: "a ", marks: ["italic"] },
24 { text: "b", marks: ["italic", "bold"] },
25 { text: " c", marks: ["italic"] },
26 ]);
27 expect(parseInlineMarkdown("**a `code` c**")).toEqual([
28 { text: "a ", marks: ["bold"] },
29 { text: "code", marks: ["bold", "code"] },
30 { text: " c", marks: ["bold"] },
31 ]);
32 });
33
34 it("preserves crossing italic and bold spans", () => {
35 expect(parseInlineMarkdown("*a**b*****c**")).toEqual([
36 { text: "a", marks: ["italic"] },
37 { text: "b", marks: ["italic", "bold"] },
38 { text: "c", marks: ["bold"] },
39 ]);
40 });
41
42 it("keeps repeated and alternate delimiters literal inside an active mark", () => {
43 const repeated = "*".repeat(10_000);
44 expect(parseInlineMarkdown(repeated + "text" + repeated)).toEqual([
45 { text: repeated.slice(2) + "text", marks: ["bold"] },
46 { text: repeated.slice(2), marks: [] },
47 ]);
48 const alternate = "__".repeat(5_000);
49 expect(parseInlineMarkdown("**~~" + alternate + "text" + alternate + "~~**")).toEqual([
50 { text: alternate + "text" + alternate, marks: ["bold", "strike"] },
51 ]);
52 });
53
54 it("keeps code span contents literal", () => {
55 expect(parseInlineMarkdown("`**not bold**`")).toEqual([
56 { text: "**not bold**", marks: ["code"] },
57 ]);
58 });
59
60 it("maps each source character to its marks, or null for a dropped delimiter", () => {
61 const bold = ["bold"];
62 expect(inlineMarkdownUnitMarks("**`*` w** \\*")).toEqual([
63 null,
64 null,
65 null,
66 ["bold", "code"],
67 null,
68 bold,
69 bold,
70 null,
71 null,
72 [],
73 [],
74 [],
75 ]);
76 });
77});