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

nativeMarkdownText.test.ts

822 lines21.7 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { MarkdownNode } from "react-native-nitro-markdown/headless";
3​
4import {
5 nativeMarkdownChunkSpacing,
6 nativeMarkdownDocumentChunks,
7 nativeMarkdownDocumentRuns,
8 nativeMarkdownListItemBlocks,
9 nativeMarkdownTextRuns,
10 nativeMarkdownWithPreservedSoftBreaks,
11} from "@t3tools/mobile-markdown-text/markdown";
12​
13describe("nativeMarkdownTextRuns", () => {
14 it("preserves inline emphasis and code styles", () => {
15 const node: MarkdownNode = {
16 type: "paragraph",
17 children: [
18 { type: "text", content: "plain " },
19 { type: "bold", children: [{ type: "text", content: "bold" }] },
20 { type: "text", content: " " },
21 { type: "code_inline", content: "const value = 1" },
22 ],
23 };
24​
25 expect(nativeMarkdownTextRuns(node)).toEqual([
26 { text: "plain " },
27 { text: "bold", bold: true },
28 { text: " " },
29 { text: "const value = 1", code: true },
30 ]);
31 });
32​
33 it("normalizes external and file links for native presentation", () => {
34 const node: MarkdownNode = {
35 type: "paragraph",
36 children: [
37 {
38 type: "link",
39 href: "https://example.com/docs",
40 children: [{ type: "text", content: "Docs" }],
41 },
42 { type: "text", content: " " },
43 {
44 type: "link",
45 href: "file:///repo/README.md#L12",
46 children: [{ type: "text", content: "ignored label" }],
47 },
48 ],
49 };
50​
51 expect(nativeMarkdownTextRuns(node)).toEqual([
52 {
53 text: "Docs",
54 href: "https://example.com/docs",
55 externalHost: "example.com",
56 },
57 { text: " " },
58 {
59 text: "README.md:12",
60 href: "file:///repo/README.md#L12",
61 fileIcon: "readme",
62 },
63 ]);
64 });
65​
66 it("keeps hard breaks and collapses soft breaks", () => {
67 const node: MarkdownNode = {
68 type: "paragraph",
69 children: [
70 { type: "text", content: "first" },
71 { type: "soft_break" },
72 { type: "text", content: "second" },
73 { type: "line_break" },
74 { type: "text", content: "third" },
75 ],
76 };
77​
78 expect(nativeMarkdownTextRuns(node)).toEqual([{ text: "first second\nthird" }]);
79 });
80​
81 it("can preserve soft breaks for authored user messages", () => {
82 const node: MarkdownNode = {
83 type: "paragraph",
84 children: [
85 { type: "text", content: "first" },
86 { type: "soft_break" },
87 { type: "text", content: "second" },
88 ],
89 };
90​
91 expect(nativeMarkdownTextRuns(nativeMarkdownWithPreservedSoftBreaks(node))).toEqual([
92 { text: "first\nsecond" },
93 ]);
94 });
95​
96 it("normalizes common inline HTML and entities", () => {
97 const node: MarkdownNode = {
98 type: "paragraph",
99 children: [
100 { type: "text", content: "Less than: < " },
101 { type: "html_inline", content: "<kbd>" },
102 { type: "text", content: "⌘" },
103 { type: "html_inline", content: "</kbd>" },
104 { type: "html_inline", content: "<br />" },
105 { type: "html_inline", content: "<mark>highlighted</mark>" },
106 ],
107 };
108​
109 expect(nativeMarkdownTextRuns(node)).toEqual([{ text: "Less than: < ⌘\nhighlighted" }]);
110 });
111​
112 it("normalizes double-encoded entities and inline tags emitted as text", () => {
113 const node: MarkdownNode = {
114 type: "paragraph",
115 children: [
116 {
117 type: "text",
118 content:
119 "Keyboard: <kbd>⌘</kbd> + <kbd>K</kbd>; Less than: &amp;lt;; Greater than: &amp;gt;",
120 },
121 ],
122 };
123​
124 expect(nativeMarkdownTextRuns(node)).toEqual([
125 { text: "Keyboard: ⌘ + K; Less than: <; Greater than: >" },
126 ]);
127 });
128​
129 it.each([
130 ["&#128512;", "😀"],
131 ["&#x1f680;", "🚀"],
132 ["&#9999999999;", "&#9999999999;"],
133 ["&#x110000;", "&#x110000;"],
134 ["&amp;#9999999999;", "&#9999999999;"],
135 ["&amp;#x110000;", "&#x110000;"],
136 ])("normalizes numeric entity %s without throwing", (content, expected) => {
137 const node: MarkdownNode = {
138 type: "paragraph",
139 children: [{ type: "text", content }],
140 };
141​
142 expect(nativeMarkdownTextRuns(node)).toEqual([{ text: expected }]);
143 });
144​
145 it("reads inline content from nested text nodes", () => {
146 const node: MarkdownNode = {
147 type: "paragraph",
148 children: [
149 {
150 type: "text",
151 children: [{ type: "text", content: "Plain text" }],
152 },
153 { type: "text", content: " and " },
154 {
155 type: "code_inline",
156 children: [{ type: "text", content: "inline code" }],
157 },
158 ],
159 };
160​
161 expect(nativeMarkdownTextRuns(node)).toEqual([
162 { text: "Plain text and " },
163 { text: "inline code", code: true },
164 ]);
165 });
166});
167​
168describe("nativeMarkdownDocumentRuns", () => {
169 it("decorates known skill references as selectable skill links", () => {
170 const node: MarkdownNode = {
171 type: "document",
172 children: [
173 {
174 type: "paragraph",
175 children: [{ type: "text", content: "Use $ui for this." }],
176 },
177 ],
178 };
179​
180 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toEqual([
181 { text: "Use ", role: "body" },
182 {
183 text: "$ui",
184 role: "body",
185 skillName: "ui",
186 skillLabel: "UI",
187 },
188 { text: " for this.", role: "body" },
189 ]);
190 });
191​
192 it("decorates known skill references inside blockquotes", () => {
193 const node: MarkdownNode = {
194 type: "blockquote",
195 children: [
196 {
197 type: "paragraph",
198 children: [{ type: "text", content: "Use $ui for this." }],
199 },
200 ],
201 };
202​
203 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toContainEqual({
204 text: "$ui",
205 role: "body",
206 skillName: "ui",
207 skillLabel: "UI",
208 });
209 });
210​
211 it("leaves unknown skill-like text unchanged", () => {
212 const node: MarkdownNode = {
213 type: "document",
214 children: [
215 {
216 type: "paragraph",
217 children: [{ type: "text", content: "Use $unknown for this." }],
218 },
219 ],
220 };
221​
222 expect(nativeMarkdownDocumentRuns(node, [])).toEqual([
223 { text: "Use $unknown for this.", role: "body" },
224 ]);
225 });
226​
227 it("keeps headings, paragraphs, and lists in one continuous document", () => {
228 const node: MarkdownNode = {
229 type: "document",
230 children: [
231 {
232 type: "heading",
233 level: 1,
234 children: [{ type: "text", content: "Header One" }],
235 },
236 {
237 type: "paragraph",
238 children: [
239 { type: "text", content: "A paragraph with " },
240 { type: "bold", children: [{ type: "text", content: "bold text" }] },
241 { type: "text", content: "." },
242 ],
243 },
244 {
245 type: "list",
246 ordered: false,
247 children: [
248 {
249 type: "list_item",
250 children: [
251 {
252 type: "paragraph",
253 children: [{ type: "text", content: "First item" }],
254 },
255 ],
256 },
257 {
258 type: "list_item",
259 children: [
260 {
261 type: "paragraph",
262 children: [{ type: "text", content: "Second item" }],
263 },
264 ],
265 },
266 ],
267 },
268 ],
269 };
270​
271 const runs = nativeMarkdownDocumentRuns(node);
272 expect(runs.map((run) => run.text).join("")).toBe(
273 "Header One\n\nA paragraph with bold text.\n\n•\tFirst item\n•\tSecond item",
274 );
275 expect(runs).toContainEqual({
276 text: "Header One\n",
277 role: "heading",
278 headingLevel: 1,
279 });
280 expect(runs).toContainEqual({
281 text: "bold text",
282 bold: true,
283 role: "body",
284 });
285 expect(runs).toContainEqual({
286 text: "•\t",
287 role: "list-marker",
288 depth: 1,
289 firstLineHeadIndent: 0,
290 headIndent: 24,
291 paragraphSpacing: 2,
292 });
293 });
294​
295 it("uses distinct section, heading-content, and body spacing", () => {
296 const node: MarkdownNode = {
297 type: "document",
298 children: [
299 {
300 type: "paragraph",
301 children: [{ type: "text", content: "Intro" }],
302 },
303 {
304 type: "heading",
305 level: 2,
306 children: [{ type: "text", content: "Section" }],
307 },
308 {
309 type: "paragraph",
310 children: [{ type: "text", content: "First paragraph" }],
311 },
312 {
313 type: "paragraph",
314 children: [{ type: "text", content: "Second paragraph" }],
315 },
316 ],
317 };
318​
319 expect(
320 nativeMarkdownDocumentRuns(node)
321 .filter((run) => run.role === "spacer")
322 .map((run) => run.spacing),
323 ).toEqual([20, 10, 12]);
324 });
325​
326 it("renders tight list items whose inline nodes are direct children", () => {
327 const node: MarkdownNode = {
328 type: "document",
329 children: [
330 {
331 type: "list",
332 children: [
333 {
334 type: "list_item",
335 children: [
336 {
337 type: "bold",
338 children: [{ type: "text", content: "Finding:" }],
339 },
340 { type: "text", content: " details with " },
341 { type: "code_inline", content: "inline code" },
342 { type: "text", content: "." },
343 ],
344 },
345 ],
346 },
347 ],
348 };
349​
350 expect(nativeMarkdownDocumentRuns(node)).toEqual([
351 {
352 text: "•\t",
353 role: "list-marker",
354 depth: 1,
355 firstLineHeadIndent: 0,
356 headIndent: 24,
357 paragraphSpacing: 2,
358 },
359 { text: "Finding:", bold: true, role: "body", depth: 1 },
360 { text: " details with ", role: "body", depth: 1 },
361 { text: "inline code", code: true, role: "body", depth: 1 },
362 { text: ".", role: "body", depth: 1 },
363 ]);
364 });
365​
366 it("preserves quotes and fenced code in document runs", () => {
367 const node: MarkdownNode = {
368 type: "document",
369 children: [
370 {
371 type: "blockquote",
372 children: [
373 {
374 type: "paragraph",
375 children: [{ type: "text", content: "Read this" }],
376 },
377 ],
378 },
379 {
380 type: "code_block",
381 language: "ts",
382 content: "const answer = 42;",
383 },
384 ],
385 };
386​
387 const runs = nativeMarkdownDocumentRuns(node);
388 expect(runs.map((run) => run.text).join("")).toBe("│\u00a0Read this\n\nTS\nconst answer = 42;");
389 expect(runs).toContainEqual({
390 text: "const answer = 42;",
391 code: true,
392 role: "code-block",
393 });
394 });
395​
396 it("reads fenced code content from child text nodes", () => {
397 const node: MarkdownNode = {
398 type: "document",
399 children: [
400 {
401 type: "code_block",
402 language: "bash",
403 children: [{ type: "text", content: "pnpm install\n" }],
404 },
405 ],
406 };
407​
408 expect(
409 nativeMarkdownDocumentRuns(node)
410 .map((run) => run.text)
411 .join(""),
412 ).toBe("BASH\npnpm install");
413 });
414});
415​
416describe("nativeMarkdownListItemBlocks", () => {
417 it("groups consecutive inline nodes into one paragraph block", () => {
418 const item: MarkdownNode = {
419 type: "list_item",
420 children: [
421 { type: "text", content: "Finding: " },
422 { type: "bold", children: [{ type: "text", content: "important" }] },
423 { type: "text", content: " details." },
424 {
425 type: "list",
426 children: [
427 {
428 type: "list_item",
429 children: [{ type: "text", content: "Nested" }],
430 },
431 ],
432 },
433 { type: "text", content: "Trailing prose." },
434 ],
435 };
436​
437 expect(nativeMarkdownListItemBlocks(item)).toEqual([
438 {
439 type: "paragraph",
440 children: item.children?.slice(0, 3),
441 },
442 item.children?.[3],
443 {
444 type: "paragraph",
445 children: [item.children?.[4]],
446 },
447 ]);
448 });
449});
450​
451describe("nativeMarkdownDocumentChunks", () => {
452 it("renders plain blockquotes as rich blocks so their marker spans wrapped lines", () => {
453 const blockquote: MarkdownNode = {
454 type: "blockquote",
455 beg: 0,
456 end: 120,
457 children: [
458 {
459 type: "paragraph",
460 children: [
461 {
462 type: "text",
463 content:
464 "Persistent random per-result keys are the strongest design, even when this text wraps.",
465 },
466 ],
467 },
468 ],
469 };
470​
471 expect(
472 nativeMarkdownDocumentChunks({
473 type: "document",
474 children: [blockquote],
475 }),
476 ).toEqual([
477 {
478 kind: "rich",
479 key: "rich:blockquote:0:120",
480 node: blockquote,
481 },
482 ]);
483 });
484​
485 it("keeps headings and plain lists in one selectable document", () => {
486 const document: MarkdownNode = {
487 type: "document",
488 children: [
489 {
490 type: "heading",
491 level: 2,
492 children: [{ type: "text", content: "Tasks" }],
493 },
494 {
495 type: "list",
496 children: [
497 {
498 type: "task_list_item",
499 checked: true,
500 children: [
501 {
502 type: "paragraph",
503 children: [{ type: "text", content: "Completed" }],
504 },
505 ],
506 },
507 {
508 type: "list_item",
509 children: [
510 {
511 type: "paragraph",
512 children: [{ type: "text", content: "Parent" }],
513 },
514 {
515 type: "list",
516 children: [
517 {
518 type: "list_item",
519 children: [
520 {
521 type: "paragraph",
522 children: [{ type: "text", content: "Nested" }],
523 },
524 ],
525 },
526 ],
527 },
528 ],
529 },
530 ],
531 },
532 ],
533 };
534​
535 const chunks = nativeMarkdownDocumentChunks(document);
536 expect(chunks).toHaveLength(1);
537 expect(chunks[0]).toMatchObject({ kind: "selectable" });
538 expect(
539 nativeMarkdownDocumentRuns(chunks[0]?.node ?? document)
540 .map((run) => run.text)
541 .join(""),
542 ).toBe("Tasks\n\n☑︎\tCompleted\n•\tParent\n◦\tNested");
543 });
544​
545 it("aligns ordered markers while keeping the list in one selectable string", () => {
546 const document: MarkdownNode = {
547 type: "document",
548 children: [
549 {
550 type: "list",
551 ordered: true,
552 start: 9,
553 children: [
554 {
555 type: "list_item",
556 children: [{ type: "text", content: "Ninth" }],
557 },
558 {
559 type: "list_item",
560 children: [{ type: "text", content: "Tenth" }],
561 },
562 ],
563 },
564 ],
565 };
566​
567 expect(
568 nativeMarkdownDocumentRuns(document)
569 .map((run) => run.text)
570 .join(""),
571 ).toBe("\u20079.\tNinth\n10.\tTenth");
572 });
573​
574 it("keeps prose selectable while exposing rich AST blocks", () => {
575 const document: MarkdownNode = {
576 type: "document",
577 children: [
578 {
579 type: "heading",
580 level: 1,
581 beg: 0,
582 end: 9,
583 children: [{ type: "text", content: "Install" }],
584 },
585 {
586 type: "code_block",
587 language: "bash",
588 beg: 11,
589 end: 35,
590 children: [{ type: "text", content: "pnpm install\n" }],
591 },
592 {
593 type: "paragraph",
594 beg: 37,
595 end: 42,
596 children: [{ type: "text", content: "Done." }],
597 },
598 ],
599 };
600​
601 const chunks = nativeMarkdownDocumentChunks(document);
602 expect(chunks).toHaveLength(3);
603 expect(chunks[0]).toMatchObject({ kind: "selectable" });
604 expect(chunks[1]).toEqual({
605 kind: "rich",
606 key: "rich:code_block:11:35",
607 node: document.children?.[1],
608 });
609 expect(chunks[2]).toMatchObject({ kind: "selectable" });
610 });
611​
612 it("keeps a list containing fenced code as one rich AST container", () => {
613 const document: MarkdownNode = {
614 type: "document",
615 children: [
616 {
617 type: "list",
618 beg: 0,
619 end: 45,
620 children: [
621 {
622 type: "list_item",
623 children: [
624 {
625 type: "paragraph",
626 children: [{ type: "text", content: "Install" }],
627 },
628 {
629 type: "code_block",
630 language: "bash",
631 children: [{ type: "text", content: "pnpm install\n" }],
632 },
633 ],
634 },
635 ],
636 },
637 ],
638 };
639​
640 expect(nativeMarkdownDocumentChunks(document)).toEqual([
641 {
642 kind: "rich",
643 key: "rich:list:0:45",
644 node: document.children?.[0],
645 },
646 ]);
647 });
648​
649 it("keeps surrounding prose selectable when rich nodes have no source offsets", () => {
650 const document: MarkdownNode = {
651 type: "document",
652 children: [
653 {
654 type: "heading",
655 level: 1,
656 children: [{ type: "text", content: "Before" }],
657 },
658 { type: "horizontal_rule" },
659 {
660 type: "paragraph",
661 children: [{ type: "text", content: "After." }],
662 },
663 ],
664 };
665​
666 const chunks = nativeMarkdownDocumentChunks(document);
667 expect(chunks).toHaveLength(3);
668 expect(chunks[0]).toMatchObject({ kind: "selectable" });
669 expect(chunks[1]).toEqual({
670 kind: "rich",
671 key: "rich:horizontal_rule:1:1",
672 node: document.children?.[1],
673 });
674 expect(chunks[2]).toMatchObject({ kind: "selectable" });
675 });
676​
677 it("keeps offset-free structural lists isolated without promoting the whole document", () => {
678 const document: MarkdownNode = {
679 type: "document",
680 children: [
681 {
682 type: "paragraph",
683 children: [{ type: "text", content: "Before." }],
684 },
685 {
686 type: "list",
687 ordered: true,
688 children: [
689 {
690 type: "list_item",
691 children: [
692 {
693 type: "paragraph",
694 children: [{ type: "text", content: "Install" }],
695 },
696 {
697 type: "code_block",
698 language: "bash",
699 children: [{ type: "text", content: "pnpm install\n" }],
700 },
701 ],
702 },
703 ],
704 },
705 {
706 type: "paragraph",
707 children: [{ type: "text", content: "After." }],
708 },
709 ],
710 };
711​
712 const chunks = nativeMarkdownDocumentChunks(document);
713 expect(chunks).toHaveLength(3);
714 expect(chunks[0]).toMatchObject({ kind: "selectable" });
715 expect(chunks[1]).toEqual({
716 kind: "rich",
717 key: "rich:list:1:1",
718 node: document.children?.[1],
719 });
720 expect(chunks[2]).toMatchObject({ kind: "selectable" });
721 });
722​
723 it("never collapses a rich subtree into a second markdown parsing pass", () => {
724 const document: MarkdownNode = {
725 type: "document",
726 children: [
727 {
728 type: "paragraph",
729 children: [{ type: "text", content: "Before." }],
730 },
731 {
732 type: "blockquote",
733 children: [
734 {
735 type: "list",
736 children: [
737 {
738 type: "list_item",
739 children: [
740 { type: "text", content: "Run this" },
741 {
742 type: "code_block",
743 language: "sh",
744 children: [{ type: "text", content: "vp check\n" }],
745 },
746 ],
747 },
748 ],
749 },
750 ],
751 },
752 {
753 type: "paragraph",
754 children: [{ type: "text", content: "After." }],
755 },
756 ],
757 };
758​
759 const chunks = nativeMarkdownDocumentChunks(document);
760 expect(chunks.map((chunk) => chunk.kind)).toEqual(["selectable", "rich", "selectable"]);
761 expect(chunks[1]).toMatchObject({
762 kind: "rich",
763 node: { type: "blockquote" },
764 });
765 });
766​
767 it("keeps a plain list in one selectable native text container", () => {
768 const list: MarkdownNode = {
769 type: "list",
770 ordered: false,
771 children: [
772 {
773 type: "list_item",
774 children: [{ type: "text", content: "First" }],
775 },
776 ],
777 };
778​
779 const chunks = nativeMarkdownDocumentChunks({
780 type: "document",
781 children: [list],
782 });
783​
784 expect(chunks).toHaveLength(1);
785 expect(chunks[0]).toMatchObject({
786 kind: "selectable",
787 node: { type: "document", children: [list] },
788 });
789 });
790​
791 it("separates sections more than related rich blocks", () => {
792 const headingChunk = {
793 kind: "selectable" as const,
794 key: "heading",
795 node: {
796 type: "document",
797 children: [
798 {
799 type: "heading",
800 level: 2,
801 children: [{ type: "text", content: "Section" }],
802 },
803 ],
804 } satisfies MarkdownNode,
805 };
806 const firstList = {
807 kind: "rich" as const,
808 key: "list-1",
809 node: { type: "list", children: [] } satisfies MarkdownNode,
810 };
811 const secondList = {
812 kind: "rich" as const,
813 key: "list-2",
814 node: { type: "list", children: [] } satisfies MarkdownNode,
815 };
816​
817 expect(nativeMarkdownChunkSpacing(undefined, headingChunk)).toBe(0);
818 expect(nativeMarkdownChunkSpacing(headingChunk, firstList)).toBe(10);
819 expect(nativeMarkdownChunkSpacing(firstList, secondList)).toBe(12);
820 expect(nativeMarkdownChunkSpacing(firstList, headingChunk)).toBe(20);
821 });
822});