Open History to see the last commit
1import { toHtml } from "hast-util-to-html";
2import { describe, expect, it } from "vite-plus/test";
3
4import { markdownTree, wordsHast } from "./markdown/wordsHast";
5import {
6 chatMarkdownClipboardPayload,
7 restorePastedMarkdownLinks,
8 serializeRenderedMarkdownFragment,
9 serializeTableElementToCsv,
10 serializeTableElementToMarkdown,
11} from "./markdown-clipboard";
12
13describe("restoring pasted Markdown links", () => {
14 it.each([
15 {
16 case: "repeated labels with distinct destinations",
17 plain: "Guide: Guide, Guide.",
18 html: '<p>Guide: <a href="https://example.com/start">Guide</a>, <a href="https://example.com/end">Guide</a>.</p>',
19 expected: "Guide: [Guide](https://example.com/start), [Guide](https://example.com/end).",
20 },
21 {
22 case: "line breaks within a link",
23 plain: "First\nSecond",
24 html: '<a href="https://example.com/">First<br>Second</a>',
25 expected: "[First](https://example.com/)\n[Second](https://example.com/)",
26 },
27 {
28 case: "a backslash immediately before a link",
29 plain: "\\Guide",
30 html: '\\<a href="https://example.com/">Guide</a>',
31 expected: "\\\\[Guide](https://example.com/)",
32 },
33 {
34 case: "nested labels, punctuation and native clipboard line breaks",
35 plain: "First line\n\nRead [the guide] & notes",
36 html: '<p>First line</p><p>Read <a href="https://example.com/guide(part)?a=1&b=2"><strong>[the guide]</strong></a> & notes</p>',
37 expected:
38 "First line\n\nRead [\\[the guide\\]](https://example.com/guide\\(part\\)?a=1&b=2) & notes",
39 },
40 {
41 case: "visible labels surrounded by hidden content",
42 plain: "Read the guide.",
43 html: '<span hidden>Skip</span>Read <a href="https://example.com/guide">the <span style="display: none">hidden </span>guide</a>.',
44 expected: "Read [the guide](https://example.com/guide).",
45 },
46 {
47 case: "visible link overrides inside a hidden ancestor",
48 plain: "Guide",
49 html: '<span style="visibility:hidden">Skip <a style="visibility:visible" href="https://example.com/"><span style="display:none;visibility:visible">Skip </span>Guide</a></span>',
50 expected: "[Guide](https://example.com/)",
51 },
52 {
53 case: "destinations without an external link scheme and anchors with no label",
54 plain: "Local, unavailable, and !Guide",
55 html: '<a href="/local">Local</a>, <a>unavailable</a>, and !<a href="https://example.com/guide">Guide</a><a href="https://example.com/empty"></a>',
56 expected: "Local, unavailable, and \\",
57 },
58 {
59 case: "linked identifiers inside code samples as code",
60 plain: "pub fn new(v: Vec<u8>) -> Self",
61 html: '<pre><code>pub fn new(v: <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html">Vec</a><<a href="https://doc.rust-lang.org/std/primitive.u8.html">u8</a>>) -> Self</code></pre>',
62 expected: "pub fn new(v: Vec<u8>) -> Self",
63 },
64 {
65 case: "bare web addresses that URL normalization rewrites",
66 plain:
67 "See https://t3.codes, https://de.wikipedia.org/wiki/Straße, https://T3.CODES/docs, https://t3.codes:443/docs, http://t3.codes:80/docs, https://t3.codes?q=1, and https://t3.codes#top.",
68 html: '<p>See <a href="https://t3.codes/">https://t3.codes</a>, <a href="https://de.wikipedia.org/wiki/Stra%C3%9Fe">https://de.wikipedia.org/wiki/Straße</a>, <a href="https://t3.codes/docs">https://T3.CODES/docs</a>, <a href="https://t3.codes/docs">https://t3.codes:443/docs</a>, <a href="http://t3.codes/docs">http://t3.codes:80/docs</a>, <a href="https://t3.codes/?q=1">https://t3.codes?q=1</a>, and <a href="https://t3.codes/#top">https://t3.codes#top</a>.</p>',
69 expected:
70 "See https://t3.codes, https://de.wikipedia.org/wiki/Straße, https://T3.CODES/docs, https://t3.codes:443/docs, http://t3.codes:80/docs, https://t3.codes?q=1, and https://t3.codes#top.",
71 },
72 {
73 case: "URL-shaped labels whose destination differs or cannot autolink bare",
74 plain:
75 "Check https://t3.codes, https://t3.codes/docs, http://t3.codes, https://T3.CODES/docs., and https://t3.codes/a b.",
76 html: '<p>Check <a href="https://example.org/other">https://t3.codes</a>, <a href="https://t3.codes/docs/">https://t3.codes/docs</a>, <a href="https://t3.codes/">http://t3.codes</a>, <a href="https://t3.codes/docs.">https://T3.CODES/docs.</a>, and <a href="https://t3.codes/a%20b">https://t3.codes/a b</a>.</p>',
77 expected:
78 "Check [https://t3.codes](https://example.org/other), [https://t3.codes/docs](https://t3.codes/docs/), [http://t3.codes](https://t3.codes/), [https://T3.CODES/docs.](https://t3.codes/docs.), and [https://t3.codes/a b](https://t3.codes/a%20b).",
79 },
80 ])("keeps $case", ({ plain, html, expected }) => {
81 expect(restorePastedMarkdownLinks(plain, html)).toBe(expected);
82 });
83
84 it("round trips literal entity names in the destination", () => {
85 const text = restorePastedMarkdownLinks(
86 "Guide",
87 '<a href="https://example.com/?q=&copy;">Guide</a>',
88 );
89 expect(markdownTree(text)).toMatchObject({
90 children: [
91 {
92 tagName: "p",
93 children: [{ tagName: "a", properties: { href: "https://example.com/?q=©" } }],
94 },
95 ],
96 });
97 });
98});
99
100describe("copying rendered Markdown links", () => {
101 it.each(["Guide", "https://example.com/?q=&copy;"])(
102 "round trips literal entity names in the destination of %s",
103 (label) => {
104 const container = document.createElement("div");
105 container.innerHTML = `<p>See <a href="https://example.com/?q=&copy;">${label}</a> now</p>`;
106 const range = document.createRange();
107 range.selectNodeContents(container.firstChild!);
108 const paragraph = markdownTree(serializeRenderedMarkdownFragment(range.cloneContents()))
109 .children[0];
110 expect(paragraph).toMatchObject({
111 tagName: "p",
112 children: [
113 { type: "text", value: "See " },
114 {
115 tagName: "a",
116 properties: { href: "https://example.com/?q=©" },
117 children: [{ type: "text", value: container.querySelector("a")!.textContent }],
118 },
119 { type: "text", value: " now" },
120 ],
121 });
122 },
123 );
124
125 it("keeps a trailing period in a destination its label repeats exactly", () => {
126 const url = "https://example.com/end.";
127 const container = document.createElement("div");
128 container.innerHTML = `<p><a href="${url}">${url}</a></p>`;
129 const range = document.createRange();
130 range.selectNodeContents(container.firstChild!);
131 const copied = serializeRenderedMarkdownFragment(range.cloneContents());
132 const pasted = restorePastedMarkdownLinks(url, container.innerHTML);
133 for (const markdown of [copied, pasted]) {
134 expect(markdownTree(markdown).children[0]).toMatchObject({
135 tagName: "p",
136 children: [{ tagName: "a", properties: { href: url } }],
137 });
138 }
139 });
140
141 it.each([
142 {
143 source: 'abc<a href="https://example.com/"> there </a>friend',
144 text: "abc there friend",
145 links: [["https://example.com/", " there "]],
146 },
147 {
148 source: "[ label ](https://example.com/)!",
149 text: "label !",
150 links: [["https://example.com/", " label "]],
151 },
152 {
153 source:
154 "Visit[ https://example.com/](https://example.com/)now or[ hi@example.com ](mailto:hi@example.com)today.",
155 text: "Visit https://example.com/now or hi@example.com today.",
156 links: [
157 ["https://example.com/", " https://example.com/"],
158 ["mailto:hi@example.com", " hi@example.com "],
159 ],
160 },
161 ])("keeps link edge spaces when copying $source", ({ source, text, links }) => {
162 const container = document.createElement("div");
163 const renderMarkdown = (markdown: string) => {
164 container.innerHTML = toHtml(
165 wordsHast(markdown, {
166 math: false,
167 breaks: false,
168 escapeHtml: false,
169 insights: false,
170 extensions: {},
171 }),
172 );
173 return container.querySelector("p")!;
174 };
175 const selection = window.getSelection()!;
176 document.body.append(container);
177 try {
178 const paragraph = renderMarkdown(source);
179 expect(paragraph.innerText).toBe(text);
180 selection.selectAllChildren(paragraph);
181 const copied = chatMarkdownClipboardPayload(selection);
182 const pasted = renderMarkdown(copied!.text);
183 expect(pasted.innerText).toBe(text);
184 expect(
185 [...pasted.querySelectorAll("a")].map((anchor) => [
186 anchor.getAttribute("href"),
187 anchor.textContent,
188 ]),
189 ).toEqual(links);
190 } finally {
191 selection.removeAllRanges();
192 container.remove();
193 }
194 });
195});
196
197describe("copying rendered Markdown tables", () => {
198 it("copies footer rows last, as the table draws them, when tfoot precedes tbody", () => {
199 const table = document.createElement("table");
200 table.innerHTML =
201 "<thead><tr><th>Item</th><th>Cost</th></tr></thead><tfoot><tr><td>Total</td><td>3</td></tr></tfoot><tbody><tr><td>a</td><td>1</td></tr><tr><td>b</td><td>2</td></tr></tbody>";
202
203 expect(serializeTableElementToCsv(table)).toBe("Item,Cost\na,1\nb,2\nTotal,3");
204 expect(serializeTableElementToMarkdown(table)).toBe(
205 "| Item | Cost |\n| --- | --- |\n| a | 1 |\n| b | 2 |\n| Total | 3 |",
206 );
207 });
208});
209
210describe("copying rendered Markdown details", () => {
211 it("keeps literal tags in a summary as text", () => {
212 const container = document.createElement("div");
213 container.innerHTML =
214 '<div data-markdown-details="" data-markdown-details-open="true"><button data-markdown-details-summary=""><span>Literal <em>example</em> & more</span></button><div><div data-markdown-details-content=""><p>Body</p></div></div></div>';
215 const details = markdownTree(serializeRenderedMarkdownFragment(container)).children.find(
216 (node) => node.type === "element",
217 );
218 const summary =
219 details?.type === "element"
220 ? details.children.find((node) => node.type === "element")
221 : undefined;
222 expect(summary?.type === "element" && summary.tagName).toBe("summary");
223 expect(summary?.type === "element" && summary.children).toEqual([
224 expect.objectContaining({ type: "text", value: "Literal <em>example</em> & more" }),
225 ]);
226 });
227});