Open History to see the last commit
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 "@v12/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: &lt;; Greater than: &gt;",
120 },
121 ],
122 };
123
124 expect(nativeMarkdownTextRuns(node)).toEqual([
125 { text: "Keyboard: ⌘ + K; Less than: <; Greater than: >" },
126 ]);
127 });
128
129 it("reads inline content from nested text nodes", () => {
130 const node: MarkdownNode = {
131 type: "paragraph",
132 children: [
133 {
134 type: "text",
135 children: [{ type: "text", content: "Plain text" }],
136 },
137 { type: "text", content: " and " },
138 {
139 type: "code_inline",
140 children: [{ type: "text", content: "inline code" }],
141 },
142 ],
143 };
144
145 expect(nativeMarkdownTextRuns(node)).toEqual([
146 { text: "Plain text and " },
147 { text: "inline code", code: true },
148 ]);
149 });
150});
151
152describe("nativeMarkdownDocumentRuns", () => {
153 it("decorates known skill references as selectable skill links", () => {
154 const node: MarkdownNode = {
155 type: "document",
156 children: [
157 {
158 type: "paragraph",
159 children: [{ type: "text", content: "Use $ui for this." }],
160 },
161 ],
162 };
163
164 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toEqual([
165 { text: "Use ", role: "body" },
166 {
167 text: "$ui",
168 role: "body",
169 skillName: "ui",
170 skillLabel: "UI",
171 },
172 { text: " for this.", role: "body" },
173 ]);
174 });
175
176 it("leaves unknown skill-like text unchanged", () => {
177 const node: MarkdownNode = {
178 type: "document",
179 children: [
180 {
181 type: "paragraph",
182 children: [{ type: "text", content: "Use $unknown for this." }],
183 },
184 ],
185 };
186
187 expect(nativeMarkdownDocumentRuns(node, [])).toEqual([
188 { text: "Use $unknown for this.", role: "body" },
189 ]);
190 });
191
192 it("keeps headings, paragraphs, and lists in one continuous document", () => {
193 const node: MarkdownNode = {
194 type: "document",
195 children: [
196 {
197 type: "heading",
198 level: 1,
199 children: [{ type: "text", content: "Header One" }],
200 },
201 {
202 type: "paragraph",
203 children: [
204 { type: "text", content: "A paragraph with " },
205 { type: "bold", children: [{ type: "text", content: "bold text" }] },
206 { type: "text", content: "." },
207 ],
208 },
209 {
210 type: "list",
211 ordered: false,
212 children: [
213 {
214 type: "list_item",
215 children: [
216 {
217 type: "paragraph",
218 children: [{ type: "text", content: "First item" }],
219 },
220 ],
221 },
222 {
223 type: "list_item",
224 children: [
225 {
226 type: "paragraph",
227 children: [{ type: "text", content: "Second item" }],
228 },
229 ],
230 },
231 ],
232 },
233 ],
234 };
235
236 const runs = nativeMarkdownDocumentRuns(node);
237 expect(runs.map((run) => run.text).join("")).toBe(
238 "Header One\n\nA paragraph with bold text.\n\n•\tFirst item\n•\tSecond item",
239 );
240 expect(runs).toContainEqual({
241 text: "Header One\n",
242 role: "heading",
243 headingLevel: 1,
244 });
245 expect(runs).toContainEqual({
246 text: "bold text",
247 bold: true,
248 role: "body",
249 });
250 expect(runs).toContainEqual({
251 text: "•\t",
252 role: "list-marker",
253 depth: 1,
254 firstLineHeadIndent: 0,
255 headIndent: 24,
256 paragraphSpacing: 2,
257 });
258 });
259
260 it("uses distinct section, heading-content, and body spacing", () => {
261 const node: MarkdownNode = {
262 type: "document",
263 children: [
264 {
265 type: "paragraph",
266 children: [{ type: "text", content: "Intro" }],
267 },
268 {
269 type: "heading",
270 level: 2,
271 children: [{ type: "text", content: "Section" }],
272 },
273 {
274 type: "paragraph",
275 children: [{ type: "text", content: "First paragraph" }],
276 },
277 {
278 type: "paragraph",
279 children: [{ type: "text", content: "Second paragraph" }],
280 },
281 ],
282 };
283
284 expect(
285 nativeMarkdownDocumentRuns(node)
286 .filter((run) => run.role === "spacer")
287 .map((run) => run.spacing),
288 ).toEqual([20, 10, 12]);
289 });
290
291 it("renders tight list items whose inline nodes are direct children", () => {
292 const node: MarkdownNode = {
293 type: "document",
294 children: [
295 {
296 type: "list",
297 children: [
298 {
299 type: "list_item",
300 children: [
301 {
302 type: "bold",
303 children: [{ type: "text", content: "Finding:" }],
304 },
305 { type: "text", content: " details with " },
306 { type: "code_inline", content: "inline code" },
307 { type: "text", content: "." },
308 ],
309 },
310 ],
311 },
312 ],
313 };
314
315 expect(nativeMarkdownDocumentRuns(node)).toEqual([
316 {
317 text: "•\t",
318 role: "list-marker",
319 depth: 1,
320 firstLineHeadIndent: 0,
321 headIndent: 24,
322 paragraphSpacing: 2,
323 },
324 { text: "Finding:", bold: true, role: "body", depth: 1 },
325 { text: " details with ", role: "body", depth: 1 },
326 { text: "inline code", code: true, role: "body", depth: 1 },
327 { text: ".", role: "body", depth: 1 },
328 ]);
329 });
330
331 it("includes quotes and fenced code in the same selectable string", () => {
332 const node: MarkdownNode = {
333 type: "document",
334 children: [
335 {
336 type: "blockquote",
337 children: [
338 {
339 type: "paragraph",
340 children: [{ type: "text", content: "Read this" }],
341 },
342 ],
343 },
344 {
345 type: "code_block",
346 language: "ts",
347 content: "const answer = 42;",
348 },
349 ],
350 };
351
352 const runs = nativeMarkdownDocumentRuns(node);
353 expect(runs.map((run) => run.text).join("")).toBe("│\u00a0Read this\n\nTS\nconst answer = 42;");
354 expect(runs).toContainEqual({
355 text: "const answer = 42;",
356 code: true,
357 role: "code-block",
358 });
359 });
360
361 it("reads fenced code content from child text nodes", () => {
362 const node: MarkdownNode = {
363 type: "document",
364 children: [
365 {
366 type: "code_block",
367 language: "bash",
368 children: [{ type: "text", content: "pnpm install\n" }],
369 },
370 ],
371 };
372
373 expect(
374 nativeMarkdownDocumentRuns(node)
375 .map((run) => run.text)
376 .join(""),
377 ).toBe("BASH\npnpm install");
378 });
379});
380
381describe("nativeMarkdownListItemBlocks", () => {
382 it("groups consecutive inline nodes into one paragraph block", () => {
383 const item: MarkdownNode = {
384 type: "list_item",
385 children: [
386 { type: "text", content: "Finding: " },
387 { type: "bold", children: [{ type: "text", content: "important" }] },
388 { type: "text", content: " details." },
389 {
390 type: "list",
391 children: [
392 {
393 type: "list_item",
394 children: [{ type: "text", content: "Nested" }],
395 },
396 ],
397 },
398 { type: "text", content: "Trailing prose." },
399 ],
400 };
401
402 expect(nativeMarkdownListItemBlocks(item)).toEqual([
403 {
404 type: "paragraph",
405 children: item.children?.slice(0, 3),
406 },
407 item.children?.[3],
408 {
409 type: "paragraph",
410 children: [item.children?.[4]],
411 },
412 ]);
413 });
414});
415
416describe("nativeMarkdownDocumentChunks", () => {
417 it("keeps headings and plain lists in one selectable document", () => {
418 const document: MarkdownNode = {
419 type: "document",
420 children: [
421 {
422 type: "heading",
423 level: 2,
424 children: [{ type: "text", content: "Tasks" }],
425 },
426 {
427 type: "list",
428 children: [
429 {
430 type: "task_list_item",
431 checked: true,
432 children: [
433 {
434 type: "paragraph",
435 children: [{ type: "text", content: "Completed" }],
436 },
437 ],
438 },
439 {
440 type: "list_item",
441 children: [
442 {
443 type: "paragraph",
444 children: [{ type: "text", content: "Parent" }],
445 },
446 {
447 type: "list",
448 children: [
449 {
450 type: "list_item",
451 children: [
452 {
453 type: "paragraph",
454 children: [{ type: "text", content: "Nested" }],
455 },
456 ],
457 },
458 ],
459 },
460 ],
461 },
462 ],
463 },
464 ],
465 };
466
467 const chunks = nativeMarkdownDocumentChunks(document);
468 expect(chunks).toHaveLength(1);
469 expect(chunks[0]).toMatchObject({ kind: "selectable" });
470 expect(
471 nativeMarkdownDocumentRuns(chunks[0]?.node ?? document)
472 .map((run) => run.text)
473 .join(""),
474 ).toBe("Tasks\n\n☑︎\tCompleted\n•\tParent\n◦\tNested");
475 });
476
477 it("aligns ordered markers while keeping the list in one selectable string", () => {
478 const document: MarkdownNode = {
479 type: "document",
480 children: [
481 {
482 type: "list",
483 ordered: true,
484 start: 9,
485 children: [
486 {
487 type: "list_item",
488 children: [{ type: "text", content: "Ninth" }],
489 },
490 {
491 type: "list_item",
492 children: [{ type: "text", content: "Tenth" }],
493 },
494 ],
495 },
496 ],
497 };
498
499 expect(
500 nativeMarkdownDocumentRuns(document)
501 .map((run) => run.text)
502 .join(""),
503 ).toBe("\u20079.\tNinth\n10.\tTenth");
504 });
505
506 it("keeps prose selectable while exposing rich AST blocks", () => {
507 const document: MarkdownNode = {
508 type: "document",
509 children: [
510 {
511 type: "heading",
512 level: 1,
513 beg: 0,
514 end: 9,
515 children: [{ type: "text", content: "Install" }],
516 },
517 {
518 type: "code_block",
519 language: "bash",
520 beg: 11,
521 end: 35,
522 children: [{ type: "text", content: "pnpm install\n" }],
523 },
524 {
525 type: "paragraph",
526 beg: 37,
527 end: 42,
528 children: [{ type: "text", content: "Done." }],
529 },
530 ],
531 };
532
533 const chunks = nativeMarkdownDocumentChunks(document);
534 expect(chunks).toHaveLength(3);
535 expect(chunks[0]).toMatchObject({ kind: "selectable" });
536 expect(chunks[1]).toEqual({
537 kind: "rich",
538 key: "rich:code_block:11:35",
539 node: document.children?.[1],
540 });
541 expect(chunks[2]).toMatchObject({ kind: "selectable" });
542 });
543
544 it("keeps a list containing fenced code as one rich AST container", () => {
545 const document: MarkdownNode = {
546 type: "document",
547 children: [
548 {
549 type: "list",
550 beg: 0,
551 end: 45,
552 children: [
553 {
554 type: "list_item",
555 children: [
556 {
557 type: "paragraph",
558 children: [{ type: "text", content: "Install" }],
559 },
560 {
561 type: "code_block",
562 language: "bash",
563 children: [{ type: "text", content: "pnpm install\n" }],
564 },
565 ],
566 },
567 ],
568 },
569 ],
570 };
571
572 expect(nativeMarkdownDocumentChunks(document)).toEqual([
573 {
574 kind: "rich",
575 key: "rich:list:0:45",
576 node: document.children?.[0],
577 },
578 ]);
579 });
580
581 it("keeps surrounding prose selectable when rich nodes have no source offsets", () => {
582 const document: MarkdownNode = {
583 type: "document",
584 children: [
585 {
586 type: "heading",
587 level: 1,
588 children: [{ type: "text", content: "Before" }],
589 },
590 { type: "horizontal_rule" },
591 {
592 type: "paragraph",
593 children: [{ type: "text", content: "After." }],
594 },
595 ],
596 };
597
598 const chunks = nativeMarkdownDocumentChunks(document);
599 expect(chunks).toHaveLength(3);
600 expect(chunks[0]).toMatchObject({ kind: "selectable" });
601 expect(chunks[1]).toEqual({
602 kind: "rich",
603 key: "rich:horizontal_rule:1:1",
604 node: document.children?.[1],
605 });
606 expect(chunks[2]).toMatchObject({ kind: "selectable" });
607 });
608
609 it("keeps offset-free structural lists isolated without promoting the whole document", () => {
610 const document: MarkdownNode = {
611 type: "document",
612 children: [
613 {
614 type: "paragraph",
615 children: [{ type: "text", content: "Before." }],
616 },
617 {
618 type: "list",
619 ordered: true,
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 type: "paragraph",
639 children: [{ type: "text", content: "After." }],
640 },
641 ],
642 };
643
644 const chunks = nativeMarkdownDocumentChunks(document);
645 expect(chunks).toHaveLength(3);
646 expect(chunks[0]).toMatchObject({ kind: "selectable" });
647 expect(chunks[1]).toEqual({
648 kind: "rich",
649 key: "rich:list:1:1",
650 node: document.children?.[1],
651 });
652 expect(chunks[2]).toMatchObject({ kind: "selectable" });
653 });
654
655 it("never collapses a rich subtree into a second markdown parsing pass", () => {
656 const document: MarkdownNode = {
657 type: "document",
658 children: [
659 {
660 type: "paragraph",
661 children: [{ type: "text", content: "Before." }],
662 },
663 {
664 type: "blockquote",
665 children: [
666 {
667 type: "list",
668 children: [
669 {
670 type: "list_item",
671 children: [
672 { type: "text", content: "Run this" },
673 {
674 type: "code_block",
675 language: "sh",
676 children: [{ type: "text", content: "vp check\n" }],
677 },
678 ],
679 },
680 ],
681 },
682 ],
683 },
684 {
685 type: "paragraph",
686 children: [{ type: "text", content: "After." }],
687 },
688 ],
689 };
690
691 const chunks = nativeMarkdownDocumentChunks(document);
692 expect(chunks.map((chunk) => chunk.kind)).toEqual(["selectable", "rich", "selectable"]);
693 expect(chunks[1]).toMatchObject({
694 kind: "rich",
695 node: { type: "blockquote" },
696 });
697 });
698
699 it("keeps a plain list in one selectable native text container", () => {
700 const list: MarkdownNode = {
701 type: "list",
702 ordered: false,
703 children: [
704 {
705 type: "list_item",
706 children: [{ type: "text", content: "First" }],
707 },
708 ],
709 };
710
711 const chunks = nativeMarkdownDocumentChunks({
712 type: "document",
713 children: [list],
714 });
715
716 expect(chunks).toHaveLength(1);
717 expect(chunks[0]).toMatchObject({
718 kind: "selectable",
719 node: { type: "document", children: [list] },
720 });
721 });
722
723 it("separates sections more than related rich blocks", () => {
724 const headingChunk = {
725 kind: "selectable" as const,
726 key: "heading",
727 node: {
728 type: "document",
729 children: [
730 {
731 type: "heading",
732 level: 2,
733 children: [{ type: "text", content: "Section" }],
734 },
735 ],
736 } satisfies MarkdownNode,
737 };
738 const firstList = {
739 kind: "rich" as const,
740 key: "list-1",
741 node: { type: "list", children: [] } satisfies MarkdownNode,
742 };
743 const secondList = {
744 kind: "rich" as const,
745 key: "list-2",
746 node: { type: "list", children: [] } satisfies MarkdownNode,
747 };
748
749 expect(nativeMarkdownChunkSpacing(undefined, headingChunk)).toBe(0);
750 expect(nativeMarkdownChunkSpacing(headingChunk, firstList)).toBe(10);
751 expect(nativeMarkdownChunkSpacing(firstList, secondList)).toBe(12);
752 expect(nativeMarkdownChunkSpacing(firstList, headingChunk)).toBe(20);
753 });
754});