Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-rich-text.test.ts

129 lines4.4 KB

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 // regression: #25180 underscores in URLs and paths were read as emphasis
55 it("keeps underscores inside URLs, paths, and filenames literal", () => {
56 for (const text of [
57 "Read https://example.com/_handle_/status/1 and open src/__test__.tsx",
58 "https://example.com/~_user_/a",
59 "_build_/file.ts",
60 "edit __init__.py, then",
61 "see __init__.py:42",
62 "open src/_a.ts_ and example.com/_handle_.",
63 "/" + "_".repeat(50_000) + "a",
64 ]) {
65 expect(parseInlineMarkdown(text)).toEqual([{ text, marks: [] }]);
66 }
67 for (const [text, styled, mark, rest] of [
68 ["_see src/a_b.ts_ now", "see src/a_b.ts", "italic", " now"],
69 ["_read/write access_ now", "read/write access", "italic", " now"],
70 ["__read/write access__ now", "read/write access", "bold", " now"],
71 ["_see src/_a.ts_(optional)", "see src/_a.ts", "italic", "(optional)"],
72 ["__see src/__a.ts__(optional)", "see src/__a.ts", "bold", "(optional)"],
73 ["_see src/_a.ts_.", "see src/_a.ts", "italic", "."],
74 ["_notes.md_ here", "notes.md", "italic", " here"],
75 ["_e.g._ this", "e.g.", "italic", " this"],
76 ["_and_/or", "and", "italic", "/or"],
77 ] as const) {
78 expect(parseInlineMarkdown(text)).toEqual([
79 { text: styled, marks: [mark] },
80 { text: rest, marks: [] },
81 ]);
82 }
83 for (const [text, first, second, mark] of [
84 ["_yes_/_no_", "yes", "no", "italic"],
85 ["__yes__/__no__.", "yes", "no", "bold"],
86 ["_a_b_/_no_", "a_b", "no", "italic"],
87 ] as const) {
88 expect(parseInlineMarkdown(text).slice(0, 3)).toEqual([
89 { text: first, marks: [mark] },
90 { text: "/", marks: [] },
91 { text: second, marks: [mark] },
92 ]);
93 }
94 expect(parseInlineMarkdown("and/_or_")).toEqual([
95 { text: "and/", marks: [] },
96 { text: "or", marks: ["italic"] },
97 ]);
98 expect(parseInlineMarkdown("_build_/file.ts and _x_, __init__.py and __y__")).toEqual([
99 { text: "_build_/file.ts and ", marks: [] },
100 { text: "x", marks: ["italic"] },
101 { text: ", __init__.py and ", marks: [] },
102 { text: "y", marks: ["bold"] },
103 ]);
104 });
105​
106 it("keeps code span contents literal", () => {
107 expect(parseInlineMarkdown("`**not bold**`")).toEqual([
108 { text: "**not bold**", marks: ["code"] },
109 ]);
110 });
111​
112 it("maps each source character to its marks, or null for a dropped delimiter", () => {
113 const bold = ["bold"];
114 expect(inlineMarkdownUnitMarks("**`*` w** \\*")).toEqual([
115 null,
116 null,
117 null,
118 ["bold", "code"],
119 null,
120 bold,
121 bold,
122 null,
123 null,
124 [],
125 [],
126 [],
127 ]);
128 });
129});