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

markdownDocument.test.ts

90 lines3.9 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";
10​
11const BOM = "\uFEFF";
12const render = (markdown: string) =>
13 markdownForNativeRendering(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
14const embed = (markdown: string) =>
15 markdownWithFileEmbeds(markdown, (href) => (href.endsWith(".pdf") ? "report.pdf" : null))
16 .markdown;
17​
18describe("a leading byte-order mark", () => {
19 it.each([
20 ["HTML links", render, 'Hello <a href="https://example.com">world</a>!'],
21 ["HTML link labels", render, '<a href="https://example.com">a[b]</a>'],
22 ["HTML links after a bang", render, '!<a href="https://example.com">world</a>'],
23 ["inline file links", render, "See `src/app.ts` now"],
24 ["prose file paths", render, "See /tmp/notes.md"],
25 ["table breaks", render, "Intro\n| a | b |\n| - | - |\n| 1 | 2 |"],
26 ["Windows path destinations", render, "Open [app](C:\\work\\app.ts) now"],
27 ["angled Windows path destinations", render, "Open [app](<C:\\work\\app.ts>) now"],
28 ["image paragraphs", render, "Caption\n![shot](https://example.com/a.png)"],
29 ["file embeds", embed, "See ![doc](files/report.pdf) now"],
30 ["CJK-bounded autolinks", render, "参考链接:https://example.com/a"],
31 ["text direction", markdownWithTextDirection, "Hello مرحبا بالعالم كيف حالك\n\nOK مرحبا بكم"],
32 ])("leaves %s where they are", (_name, transform, markdown) => {
33 expect(transform(markdown)).not.toBe(markdown);
34 expect(transform(`${BOM}${markdown}`)).toBe(`${BOM}${transform(markdown)}`);
35 });
36​
37 it("is kept when the rewrite ends in a second table-break pass", () => {
38 const markdown = "Intro text\n| a | c |\n|---|---|\n| `./a\\|b.ts` | b |";
39 const rendered = render(markdown);
40 expect(rendered).toContain("Intro text\n\n| a | c |");
41 const table = parseMarkdownDocument(rendered).children[1];
42 expect(table?.type === "table" && table.children[1]?.children).toMatchObject([
43 { children: [{ type: "link", url: "./a|b.ts", children: [{ value: "./a|b.ts" }] }] },
44 { children: [{ type: "text", value: "b" }] },
45 ]);
46 expect(render(`${BOM}${markdown}`)).toBe(`${BOM}${rendered}`);
47 });
48});
49​
50describe("bare URLs next to CJK text", () => {
51 it.each([
52 [
53 "参考链接:https://github.com/pingdotgg/t3code",
54 "参考链接:<https://github.com/pingdotgg/t3code>",
55 ],
56 ["(https://github.com/pingdotgg/t3code)", "(<https://github.com/pingdotgg/t3code>)"],
57 [
58 "详见 https://github.com/pingdotgg/t3code,这里有说明",
59 "详见 <https://github.com/pingdotgg/t3code>,这里有说明",
60 ],
61 ["见https://example.com/a?b=1。", "见<https://example.com/a?b=1>。"],
62 [
63 "**https://example.com**,和 https://a.com,https://b.com",
64 "**https://example.com**,和 <https://a.com>,<https://b.com>",
65 ],
66 [
67 "| 链接 |\n| - |\n| 见:https://example.com |",
68 "| 链接 |\n| - |\n| 见:<https://example.com> |",
69 ],
70 ])("are wrapped for the native renderer: %s", (markdown, expected) => {
71 expect(render(markdown)).toBe(expected);
72 });
73​
74 it.each([
75 "Control: see https://github.com/pingdotgg/t3code here",
76 "中文 https://example.com 中文",
77 "参考:<https://example.com>",
78 "参考:[link](https://example.com)",
79 "参考:`https://example.com`",
80 "参考:www.example.com",
81 "$参考:https://example.com$",
82 "$$\n参考:https://example.com\n$$",
83 "| 价格 |\n| - |\n| $参考:https://example.com$ |",
84 "\\(\\text{参考:https://example.com } + x\\)",
85 "\\[\\text{参考:https://example.com } + x\\]",
86 "\\[\n\\text{参考:https://example.com } + x\n\\]",
87 ])("are left alone when md4c already decides them: %s", (markdown) => {
88 expect(render(markdown)).toBe(markdown);
89 });
90});