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

markdownDocument.test.ts

119 lines5.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation";
4import {
5 markdownForNativeRendering,
6 markdownWithFileEmbeds,
7 markdownWithTextDirection,
8 parseMarkdownDocument,
9} from "./markdownDocument";
10import { parseEnrichedMarkdown } from "./parseEnrichedMarkdown.test-support";
11​
12const BOM = "\uFEFF";
13const render = (markdown: string) =>
14 markdownForNativeRendering(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
15const embed = (markdown: string) =>
16 markdownWithFileEmbeds(markdown, (href) => (href.endsWith(".pdf") ? "report.pdf" : null))
17 .markdown;
18​
19it.each([
20 ["LF", "\n"],
21 ["CRLF", "\r\n"],
22])("links an inline file path split by %s", async (_name, separator) => {
23 const markdown = "`/home/alice/my" + separator + "report.pdf`";
24 const tree = await parseEnrichedMarkdown(render(markdown));
25 expect(tree.children?.[0]?.children?.[0]).toMatchObject({
26 type: "Link",
27 attributes: { url: "/home/alice/my report.pdf" },
28 });
29});
30​
31it("links an inline file path after an unmatched backtick", async () => {
32 const tree = await parseEnrichedMarkdown(render("a ` b\n\n`/home/alice/my\nreport.pdf`"));
33 expect(tree.children?.[1]?.children?.[0]).toMatchObject({
34 type: "Link",
35 attributes: { url: "/home/alice/my report.pdf" },
36 });
37});
38​
39it("links an inline file path split across blockquote lines", async () => {
40 const tree = await parseEnrichedMarkdown(render("> `/home/alice/my\n> report.pdf`"));
41 expect(tree.children?.[0]?.children?.[0]?.children?.[0]).toMatchObject({
42 type: "Link",
43 attributes: { url: "/home/alice/my report.pdf" },
44 });
45});
46​
47describe("a leading byte-order mark", () => {
48 it.each([
49 ["HTML links", render, 'Hello <a href="https://example.com">world</a>!'],
50 ["HTML link labels", render, '<a href="https://example.com">a[b]</a>'],
51 ["HTML links after a bang", render, '!<a href="https://example.com">world</a>'],
52 ["inline file links", render, "See `src/app.ts` now"],
53 ["prose file paths", render, "See /tmp/notes.md"],
54 ["table breaks", render, "Intro\n| a | b |\n| - | - |\n| 1 | 2 |"],
55 ["Windows path destinations", render, "Open [app](C:\\work\\app.ts) now"],
56 ["angled Windows path destinations", render, "Open [app](<C:\\work\\app.ts>) now"],
57 ["image paragraphs", render, "Caption\n![shot](https://example.com/a.png)"],
58 ["file embeds", embed, "See ![doc](files/report.pdf) now"],
59 ["CJK-bounded autolinks", render, "参考链接:https://example.com/a"],
60 ["text direction", markdownWithTextDirection, "Hello مرحبا بالعالم كيف حالك\n\nOK مرحبا بكم"],
61 ])("leaves %s where they are", (_name, transform, markdown) => {
62 expect(transform(markdown)).not.toBe(markdown);
63 expect(transform(`${BOM}${markdown}`)).toBe(`${BOM}${transform(markdown)}`);
64 });
65​
66 it("is kept when the rewrite ends in a second table-break pass", () => {
67 const markdown = "Intro text\n| a | c |\n|---|---|\n| `./a\\|b.ts` | b |";
68 const rendered = render(markdown);
69 expect(rendered).toContain("Intro text\n\n| a | c |");
70 const table = parseMarkdownDocument(rendered).children[1];
71 expect(table?.type === "table" && table.children[1]?.children).toMatchObject([
72 { children: [{ type: "link", url: "./a|b.ts", children: [{ value: "./a|b.ts" }] }] },
73 { children: [{ type: "text", value: "b" }] },
74 ]);
75 expect(render(`${BOM}${markdown}`)).toBe(`${BOM}${rendered}`);
76 });
77});
78​
79describe("bare URLs next to CJK text", () => {
80 it.each([
81 [
82 "参考链接:https://github.com/pingdotgg/t3code",
83 "参考链接:<https://github.com/pingdotgg/t3code>",
84 ],
85 ["(https://github.com/pingdotgg/t3code)", "(<https://github.com/pingdotgg/t3code>)"],
86 [
87 "详见 https://github.com/pingdotgg/t3code,这里有说明",
88 "详见 <https://github.com/pingdotgg/t3code>,这里有说明",
89 ],
90 ["见https://example.com/a?b=1。", "见<https://example.com/a?b=1>。"],
91 [
92 "**https://example.com**,和 https://a.com,https://b.com",
93 "**https://example.com**,和 <https://a.com>,<https://b.com>",
94 ],
95 [
96 "| 链接 |\n| - |\n| 见:https://example.com |",
97 "| 链接 |\n| - |\n| 见:<https://example.com> |",
98 ],
99 ])("are wrapped for the native renderer: %s", (markdown, expected) => {
100 expect(render(markdown)).toBe(expected);
101 });
102​
103 it.each([
104 "Control: see https://github.com/pingdotgg/t3code here",
105 "中文 https://example.com 中文",
106 "参考:<https://example.com>",
107 "参考:[link](https://example.com)",
108 "参考:`https://example.com`",
109 "参考:www.example.com",
110 "$参考:https://example.com$",
111 "$$\n参考:https://example.com\n$$",
112 "| 价格 |\n| - |\n| $参考:https://example.com$ |",
113 "\\(\\text{参考:https://example.com } + x\\)",
114 "\\[\\text{参考:https://example.com } + x\\]",
115 "\\[\n\\text{参考:https://example.com } + x\n\\]",
116 ])("are left alone when md4c already decides them: %s", (markdown) => {
117 expect(render(markdown)).toBe(markdown);
118 });
119});