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

nativeMarkdownText.test.ts

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