xonecas.io
719
Le

lextide / files

lextide / test / markdown-language-support.test.ts

4.9 KB
1import { describe, expect, test } from "bun:test";
2
3import { common, createLowlight } from "../src/index";
4import { plainText, simplifyChildren } from "./language-test-helpers";
5
6const lowlight = createLowlight(common);
7
8describe("markdown language support", () => {
9	test("atxHeadings_scope_wholeLine_and_allow_nestedInlineMarkup", () => {
10		const source = "# Title **bold** [docs](https://example.com)\n";
11		const tree = lowlight.highlight("markdown", source);
12
13		expect(simplifyChildren(tree.children)).toEqual([
14			[
15				"span",
16				["hljs-section"],
17				[
18					"# Title ",
19					["span", ["hljs-strong"], ["**bold**"]],
20					" [",
21					["span", ["hljs-string"], ["docs"]],
22					"](",
23					["span", ["hljs-link"], ["https://example.com"]],
24					")",
25				],
26			],
27			["text", "\n"],
28		]);
29		expect(plainText(tree)).toBe(source);
30	});
31
32	test("setextHeadings_are_scoped_as_sections", () => {
33		const source = "Heading\n-----\n";
34		const tree = lowlight.highlight("markdown", source);
35
36		expect(simplifyChildren(tree.children)).toEqual([
37			["span", ["hljs-section"], ["Heading\n-----"]],
38			["text", "\n"],
39		]);
40		expect(plainText(tree)).toBe(source);
41	});
42
43	test("unordered_and_ordered_list_markers_are_scoped_as_bullets_only", () => {
44		const source = "- item\n1. first\n";
45		const tree = lowlight.highlight("markdown", source);
46
47		expect(simplifyChildren(tree.children)).toEqual([
48			["span", ["hljs-bullet"], ["-"]],
49			["text", " item\n"],
50			["span", ["hljs-bullet"], ["1."]],
51			["text", " first\n"],
52		]);
53		expect(plainText(tree)).toBe(source);
54	});
55
56	test("blockquotes_keep_inline_links_inside_the_quote_scope", () => {
57		const source = "> quote with [docs](https://example.com)\n";
58		const tree = lowlight.highlight("markdown", source);
59
60		expect(simplifyChildren(tree.children)).toEqual([
61			[
62				"span",
63				["hljs-quote"],
64				[
65					"> quote with [",
66					["span", ["hljs-string"], ["docs"]],
67					"](",
68					["span", ["hljs-link"], ["https://example.com"]],
69					")",
70				],
71			],
72			["text", "\n"],
73		]);
74		expect(plainText(tree)).toBe(source);
75	});
76
77	test("fencedCodeBlocks_keep_trailingNewlines_outside_the_code_span", () => {
78		const cases = [
79			{
80				source: "```ts\nconst value = 1\n```\n",
81				expected: [
82					["span", ["hljs-code"], ["```ts\nconst value = 1\n```"]],
83					["text", "\n"],
84				],
85			},
86			{
87				source: "~~~\ncode\n~~~\n",
88				expected: [
89					["span", ["hljs-code"], ["~~~\ncode\n~~~"]],
90					["text", "\n"],
91				],
92			},
93		] as const;
94
95		for (const { expected, source } of cases) {
96			const tree = lowlight.highlight("markdown", source);
97
98			expect(simplifyChildren(tree.children)).toEqual(expected);
99			expect(plainText(tree)).toBe(source);
100		}
101	});
102
103	test("indentedCodeBlocks_are_scoped_as_code", () => {
104		const source = "    code\n    more\n";
105		const tree = lowlight.highlight("markdown", source);
106
107		expect(simplifyChildren(tree.children)).toEqual([
108			["span", ["hljs-code"], ["    code\n    more\n"]],
109		]);
110		expect(plainText(tree)).toBe(source);
111	});
112
113	test("referenceStyle_links_scope_inline_reference_and_definition_target", () => {
114		const source = "[docs][id]\n[id]: https://example.com\n";
115		const tree = lowlight.highlight("markdown", source);
116
117		expect(simplifyChildren(tree.children)).toEqual([
118			["text", "["],
119			["span", ["hljs-string"], ["docs"]],
120			["text", "]["],
121			["span", ["hljs-symbol"], ["id"]],
122			["text", "]\n["],
123			["span", ["hljs-symbol"], ["id"]],
124			["text", "]: "],
125			["span", ["hljs-link"], ["https://example.com"]],
126			["text", "\n"],
127		]);
128		expect(plainText(tree)).toBe(source);
129	});
130
131	test("empty_inline_links_still_emit_an_empty_link_span", () => {
132		const source = "[]()\n";
133		const tree = lowlight.highlight("markdown", source);
134
135		expect(simplifyChildren(tree.children)).toEqual([
136			["text", "[]("],
137			["span", ["hljs-link"], []],
138			["text", ")\n"],
139		]);
140		expect(plainText(tree)).toBe(source);
141	});
142
143	test("strong_spans_can_nest_emphasis", () => {
144		const source = "**bold _italic_**\n";
145		const tree = lowlight.highlight("markdown", source);
146
147		expect(simplifyChildren(tree.children)).toEqual([
148			[
149				"span",
150				["hljs-strong"],
151				["**bold ", ["span", ["hljs-emphasis"], ["_italic_"]], "**"],
152			],
153			["text", "\n"],
154		]);
155		expect(plainText(tree)).toBe(source);
156	});
157
158	test("tripleDelimiter_sequences_nest_emphasis_inside_strong", () => {
159		const source = "***both***\n";
160		const tree = lowlight.highlight("markdown", source);
161
162		expect(simplifyChildren(tree.children)).toEqual([
163			[
164				"span",
165				["hljs-strong"],
166				["**", ["span", ["hljs-emphasis"], ["*both*"]], "**"],
167			],
168			["text", "\n"],
169		]);
170		expect(plainText(tree)).toBe(source);
171	});
172
173	test("entities_are_scoped_as_literals_without_changing_plain_text", () => {
174		const source = "Fish & Chips\n";
175		const tree = lowlight.highlight("markdown", source);
176
177		expect(simplifyChildren(tree.children)).toEqual([
178			["text", "Fish "],
179			["span", ["hljs-literal"], ["&"]],
180			["text", " Chips\n"],
181		]);
182		expect(plainText(tree)).toBe(source);
183	});
184});