@fairyhunter13/opentui-core 0.1.88 → 0.1.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (570) hide show
  1. package/dev/keypress-debug-renderer.ts +148 -0
  2. package/dev/keypress-debug.ts +43 -0
  3. package/dev/print-env-vars.ts +32 -0
  4. package/dev/test-tmux-graphics-334.sh +68 -0
  5. package/dev/thai-debug-test.ts +68 -0
  6. package/docs/development.md +141 -0
  7. package/docs/env-vars.md +140 -0
  8. package/docs/getting-started.md +353 -0
  9. package/docs/renderables-vs-constructs.md +159 -0
  10. package/docs/tree-sitter.md +311 -0
  11. package/package.json +61 -52
  12. package/scripts/build.ts +400 -0
  13. package/scripts/publish.ts +60 -0
  14. package/src/3d/SpriteResourceManager.ts +286 -0
  15. package/src/3d/SpriteUtils.ts +71 -0
  16. package/src/3d/TextureUtils.ts +196 -0
  17. package/src/3d/ThreeRenderable.ts +197 -0
  18. package/src/3d/WGPURenderer.ts +294 -0
  19. package/src/3d/animation/ExplodingSpriteEffect.ts +513 -0
  20. package/src/3d/animation/PhysicsExplodingSpriteEffect.ts +429 -0
  21. package/src/3d/animation/SpriteAnimator.ts +633 -0
  22. package/src/3d/animation/SpriteParticleGenerator.ts +435 -0
  23. package/src/3d/canvas.ts +464 -0
  24. package/src/3d/index.ts +12 -0
  25. package/src/3d/physics/PlanckPhysicsAdapter.ts +72 -0
  26. package/src/3d/physics/RapierPhysicsAdapter.ts +66 -0
  27. package/src/3d/physics/physics-interface.ts +31 -0
  28. package/src/3d/shaders/supersampling.wgsl +201 -0
  29. package/src/3d.ts +3 -0
  30. package/src/NativeSpanFeed.ts +300 -0
  31. package/src/Renderable.ts +1698 -0
  32. package/src/__snapshots__/buffer.test.ts.snap +28 -0
  33. package/src/animation/Timeline.test.ts +2709 -0
  34. package/src/animation/Timeline.ts +598 -0
  35. package/src/ansi.ts +18 -0
  36. package/src/benchmark/latest-all-bench-run.json +707 -0
  37. package/src/benchmark/latest-async-bench-run.json +336 -0
  38. package/src/benchmark/latest-default-bench-run.json +657 -0
  39. package/src/benchmark/latest-large-bench-run.json +707 -0
  40. package/src/benchmark/latest-quick-bench-run.json +207 -0
  41. package/src/benchmark/markdown-benchmark.ts +1804 -0
  42. package/src/benchmark/native-span-feed-async-benchmark.ts +355 -0
  43. package/src/benchmark/native-span-feed-benchmark.md +56 -0
  44. package/src/benchmark/native-span-feed-benchmark.ts +596 -0
  45. package/src/benchmark/native-span-feed-compare.ts +280 -0
  46. package/src/benchmark/renderer-benchmark.ts +754 -0
  47. package/src/benchmark/text-table-benchmark.ts +947 -0
  48. package/src/buffer.test.ts +291 -0
  49. package/src/buffer.ts +519 -0
  50. package/src/console.test.ts +612 -0
  51. package/src/console.ts +1255 -0
  52. package/src/edit-buffer.test.ts +1769 -0
  53. package/src/edit-buffer.ts +411 -0
  54. package/src/editor-view.test.ts +1032 -0
  55. package/src/editor-view.ts +284 -0
  56. package/src/examples/ascii-font-selection-demo.ts +245 -0
  57. package/src/examples/assets/Water_2_M_Normal.jpg +0 -0
  58. package/src/examples/assets/concrete.png +0 -0
  59. package/src/examples/assets/crate.png +0 -0
  60. package/src/examples/assets/crate_emissive.png +0 -0
  61. package/src/examples/assets/forrest_background.png +0 -0
  62. package/src/examples/assets/hast-example.json +1018 -0
  63. package/src/examples/assets/heart.png +0 -0
  64. package/src/examples/assets/main_char_heavy_attack.png +0 -0
  65. package/src/examples/assets/main_char_idle.png +0 -0
  66. package/src/examples/assets/main_char_jump_end.png +0 -0
  67. package/src/examples/assets/main_char_jump_landing.png +0 -0
  68. package/src/examples/assets/main_char_jump_start.png +0 -0
  69. package/src/examples/assets/main_char_run_loop.png +0 -0
  70. package/src/examples/assets/roughness_map.jpg +0 -0
  71. package/src/examples/build.ts +115 -0
  72. package/src/examples/code-demo.ts +584 -0
  73. package/src/examples/console-demo.ts +358 -0
  74. package/src/examples/core-plugin-slots-demo.ts +759 -0
  75. package/src/examples/diff-demo.ts +699 -0
  76. package/src/examples/draggable-three-demo.ts +259 -0
  77. package/src/examples/editor-demo.ts +322 -0
  78. package/src/examples/extmarks-demo.ts +204 -0
  79. package/src/examples/focus-restore-demo.ts +310 -0
  80. package/src/examples/fonts.ts +245 -0
  81. package/src/examples/fractal-shader-demo.ts +268 -0
  82. package/src/examples/framebuffer-demo.ts +674 -0
  83. package/src/examples/full-unicode-demo.ts +181 -0
  84. package/src/examples/golden-star-demo.ts +933 -0
  85. package/src/examples/grayscale-buffer-demo.ts +249 -0
  86. package/src/examples/hast-syntax-highlighting-demo.ts +129 -0
  87. package/src/examples/index.ts +925 -0
  88. package/src/examples/input-demo.ts +377 -0
  89. package/src/examples/input-select-layout-demo.ts +425 -0
  90. package/src/examples/install.sh +143 -0
  91. package/src/examples/keypress-debug-demo.ts +452 -0
  92. package/src/examples/lib/HexList.ts +122 -0
  93. package/src/examples/lib/PaletteGrid.ts +125 -0
  94. package/src/examples/lib/standalone-keys.ts +25 -0
  95. package/src/examples/lib/tab-controller.ts +243 -0
  96. package/src/examples/lights-phong-demo.ts +290 -0
  97. package/src/examples/link-demo.ts +220 -0
  98. package/src/examples/live-state-demo.ts +480 -0
  99. package/src/examples/markdown-demo.ts +620 -0
  100. package/src/examples/mouse-interaction-demo.ts +428 -0
  101. package/src/examples/nested-zindex-demo.ts +357 -0
  102. package/src/examples/opacity-example.ts +235 -0
  103. package/src/examples/opentui-demo.ts +1057 -0
  104. package/src/examples/physx-planck-2d-demo.ts +507 -0
  105. package/src/examples/physx-rapier-2d-demo.ts +526 -0
  106. package/src/examples/relative-positioning-demo.ts +323 -0
  107. package/src/examples/scroll-example.ts +214 -0
  108. package/src/examples/scrollbox-mouse-test.ts +112 -0
  109. package/src/examples/scrollbox-overlay-hit-test.ts +206 -0
  110. package/src/examples/select-demo.ts +237 -0
  111. package/src/examples/shader-cube-demo.ts +772 -0
  112. package/src/examples/simple-layout-example.ts +591 -0
  113. package/src/examples/slider-demo.ts +617 -0
  114. package/src/examples/split-mode-demo.ts +445 -0
  115. package/src/examples/sprite-animation-demo.ts +443 -0
  116. package/src/examples/sprite-particle-generator-demo.ts +486 -0
  117. package/src/examples/static-sprite-demo.ts +193 -0
  118. package/src/examples/sticky-scroll-example.ts +308 -0
  119. package/src/examples/styled-text-demo.ts +282 -0
  120. package/src/examples/tab-select-demo.ts +219 -0
  121. package/src/examples/terminal-title.ts +29 -0
  122. package/src/examples/terminal.ts +305 -0
  123. package/src/examples/text-node-demo.ts +416 -0
  124. package/src/examples/text-selection-demo.ts +377 -0
  125. package/src/examples/text-table-demo.ts +503 -0
  126. package/src/examples/text-truncation-demo.ts +481 -0
  127. package/src/examples/text-wrap.ts +757 -0
  128. package/src/examples/texture-loading-demo.ts +259 -0
  129. package/src/examples/timeline-example.ts +670 -0
  130. package/src/examples/transparency-demo.ts +241 -0
  131. package/src/examples/vnode-composition-demo.ts +404 -0
  132. package/src/index.ts +22 -0
  133. package/src/lib/KeyHandler.integration.test.ts +292 -0
  134. package/src/lib/KeyHandler.stopPropagation.test.ts +289 -0
  135. package/src/lib/KeyHandler.test.ts +662 -0
  136. package/src/lib/KeyHandler.ts +222 -0
  137. package/src/lib/RGBA.test.ts +984 -0
  138. package/src/lib/RGBA.ts +204 -0
  139. package/src/lib/ascii.font.ts +330 -0
  140. package/src/lib/border.test.ts +83 -0
  141. package/src/lib/border.ts +168 -0
  142. package/src/lib/bunfs.test.ts +27 -0
  143. package/src/lib/bunfs.ts +18 -0
  144. package/src/lib/clipboard.test.ts +41 -0
  145. package/src/lib/clipboard.ts +47 -0
  146. package/src/lib/clock.ts +31 -0
  147. package/src/lib/data-paths.test.ts +133 -0
  148. package/src/lib/data-paths.ts +109 -0
  149. package/src/lib/debounce.ts +106 -0
  150. package/src/lib/detect-links.test.ts +98 -0
  151. package/src/lib/detect-links.ts +56 -0
  152. package/src/lib/env.test.ts +228 -0
  153. package/src/lib/env.ts +209 -0
  154. package/src/lib/extmarks-history.ts +51 -0
  155. package/src/lib/extmarks-multiwidth.test.ts +322 -0
  156. package/src/lib/extmarks.test.ts +3457 -0
  157. package/src/lib/extmarks.ts +843 -0
  158. package/src/lib/fonts/block.json +405 -0
  159. package/src/lib/fonts/grid.json +265 -0
  160. package/src/lib/fonts/huge.json +741 -0
  161. package/src/lib/fonts/pallet.json +314 -0
  162. package/src/lib/fonts/shade.json +591 -0
  163. package/src/lib/fonts/slick.json +321 -0
  164. package/src/lib/fonts/tiny.json +69 -0
  165. package/src/lib/hast-styled-text.ts +59 -0
  166. package/src/lib/index.ts +21 -0
  167. package/src/lib/keymapping.test.ts +280 -0
  168. package/src/lib/keymapping.ts +87 -0
  169. package/src/lib/objects-in-viewport.test.ts +787 -0
  170. package/src/lib/objects-in-viewport.ts +153 -0
  171. package/src/lib/output.capture.ts +58 -0
  172. package/src/lib/parse.keypress-kitty.protocol.test.ts +340 -0
  173. package/src/lib/parse.keypress-kitty.test.ts +663 -0
  174. package/src/lib/parse.keypress-kitty.ts +439 -0
  175. package/src/lib/parse.keypress.test.ts +1849 -0
  176. package/src/lib/parse.keypress.ts +397 -0
  177. package/src/lib/parse.mouse.test.ts +552 -0
  178. package/src/lib/parse.mouse.ts +232 -0
  179. package/src/lib/paste.ts +16 -0
  180. package/src/lib/queue.ts +65 -0
  181. package/src/lib/renderable.validations.test.ts +87 -0
  182. package/src/lib/renderable.validations.ts +83 -0
  183. package/src/lib/scroll-acceleration.ts +98 -0
  184. package/src/lib/selection.ts +240 -0
  185. package/src/lib/singleton.ts +28 -0
  186. package/src/lib/stdin-parser.test.ts +1676 -0
  187. package/src/lib/stdin-parser.ts +1248 -0
  188. package/src/lib/styled-text.ts +178 -0
  189. package/src/lib/terminal-capability-detection.test.ts +202 -0
  190. package/src/lib/terminal-capability-detection.ts +79 -0
  191. package/src/lib/terminal-palette.test.ts +878 -0
  192. package/src/lib/terminal-palette.ts +383 -0
  193. package/src/lib/tree-sitter/assets/README.md +118 -0
  194. package/src/lib/tree-sitter/assets/update.ts +331 -0
  195. package/src/lib/tree-sitter/assets.d.ts +9 -0
  196. package/src/lib/tree-sitter/cache.test.ts +270 -0
  197. package/src/lib/tree-sitter/client.test.ts +1061 -0
  198. package/src/lib/tree-sitter/client.ts +615 -0
  199. package/src/lib/tree-sitter/default-parsers.ts +80 -0
  200. package/src/lib/tree-sitter/download-utils.ts +148 -0
  201. package/src/lib/tree-sitter/index.ts +28 -0
  202. package/src/lib/tree-sitter/parser.worker.ts +1001 -0
  203. package/src/lib/tree-sitter/parsers-config.ts +75 -0
  204. package/src/lib/tree-sitter/resolve-ft.ts +62 -0
  205. package/src/lib/tree-sitter/types.ts +81 -0
  206. package/src/lib/tree-sitter-styled-text.test.ts +1253 -0
  207. package/src/lib/tree-sitter-styled-text.ts +306 -0
  208. package/src/lib/validate-dir-name.ts +55 -0
  209. package/src/lib/yoga.options.test.ts +628 -0
  210. package/src/lib/yoga.options.ts +346 -0
  211. package/src/plugins/core-slot.ts +579 -0
  212. package/src/plugins/registry.ts +377 -0
  213. package/src/plugins/types.ts +46 -0
  214. package/src/post/filters.ts +888 -0
  215. package/src/renderables/ASCIIFont.ts +219 -0
  216. package/src/renderables/Box.test.ts +160 -0
  217. package/src/renderables/Box.ts +295 -0
  218. package/src/renderables/Code.test.ts +2062 -0
  219. package/src/renderables/Code.ts +357 -0
  220. package/src/renderables/Diff.regression.test.ts +226 -0
  221. package/src/renderables/Diff.test.ts +3027 -0
  222. package/src/renderables/Diff.ts +1209 -0
  223. package/src/renderables/EditBufferRenderable.ts +764 -0
  224. package/src/renderables/FrameBuffer.ts +47 -0
  225. package/src/renderables/Input.test.ts +1228 -0
  226. package/src/renderables/Input.ts +245 -0
  227. package/src/renderables/LineNumberRenderable.ts +675 -0
  228. package/src/renderables/Markdown.ts +1106 -0
  229. package/src/renderables/ScrollBar.ts +422 -0
  230. package/src/renderables/ScrollBox.ts +883 -0
  231. package/src/renderables/Select.test.ts +1010 -0
  232. package/src/renderables/Select.ts +523 -0
  233. package/src/renderables/Slider.test.ts +456 -0
  234. package/src/renderables/Slider.ts +347 -0
  235. package/src/renderables/TabSelect.test.ts +197 -0
  236. package/src/renderables/TabSelect.ts +455 -0
  237. package/src/renderables/Text.selection-buffer.test.ts +123 -0
  238. package/src/renderables/Text.test.ts +2660 -0
  239. package/src/renderables/Text.ts +147 -0
  240. package/src/renderables/TextBufferRenderable.ts +518 -0
  241. package/src/renderables/TextNode.test.ts +1058 -0
  242. package/src/renderables/TextNode.ts +325 -0
  243. package/src/renderables/TextTable.test.ts +1421 -0
  244. package/src/renderables/TextTable.ts +1344 -0
  245. package/src/renderables/Textarea.ts +732 -0
  246. package/src/renderables/TimeToFirstDraw.ts +89 -0
  247. package/src/renderables/__snapshots__/Code.test.ts.snap +13 -0
  248. package/src/renderables/__snapshots__/Diff.test.ts.snap +785 -0
  249. package/src/renderables/__snapshots__/Text.test.ts.snap +421 -0
  250. package/src/renderables/__snapshots__/TextTable.test.ts.snap +215 -0
  251. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +144 -0
  252. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +816 -0
  253. package/src/renderables/__tests__/LineNumberRenderable.test.ts +1787 -0
  254. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +85 -0
  255. package/src/renderables/__tests__/Markdown.test.ts +2287 -0
  256. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +87 -0
  257. package/src/renderables/__tests__/Textarea.buffer.test.ts +682 -0
  258. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +675 -0
  259. package/src/renderables/__tests__/Textarea.editing.test.ts +2041 -0
  260. package/src/renderables/__tests__/Textarea.error-handling.test.ts +35 -0
  261. package/src/renderables/__tests__/Textarea.events.test.ts +738 -0
  262. package/src/renderables/__tests__/Textarea.highlights.test.ts +590 -0
  263. package/src/renderables/__tests__/Textarea.keybinding.test.ts +3149 -0
  264. package/src/renderables/__tests__/Textarea.paste.test.ts +357 -0
  265. package/src/renderables/__tests__/Textarea.rendering.test.ts +1864 -0
  266. package/src/renderables/__tests__/Textarea.scroll.test.ts +733 -0
  267. package/src/renderables/__tests__/Textarea.selection.test.ts +1590 -0
  268. package/src/renderables/__tests__/Textarea.stress.test.ts +670 -0
  269. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +383 -0
  270. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +310 -0
  271. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +221 -0
  272. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +89 -0
  273. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +457 -0
  274. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +158 -0
  275. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +387 -0
  276. package/src/renderables/__tests__/markdown-parser.test.ts +217 -0
  277. package/src/renderables/__tests__/renderable-test-utils.ts +60 -0
  278. package/src/renderables/composition/README.md +8 -0
  279. package/src/renderables/composition/VRenderable.ts +32 -0
  280. package/src/renderables/composition/constructs.ts +127 -0
  281. package/src/renderables/composition/vnode.ts +289 -0
  282. package/src/renderables/index.ts +22 -0
  283. package/src/renderables/markdown-parser.ts +66 -0
  284. package/src/renderer.ts +2363 -0
  285. package/src/runtime-plugin-support.ts +39 -0
  286. package/src/runtime-plugin.ts +144 -0
  287. package/src/syntax-style.test.ts +841 -0
  288. package/src/syntax-style.ts +264 -0
  289. package/src/testing/README.md +210 -0
  290. package/src/testing/capture-spans.test.ts +194 -0
  291. package/src/testing/integration.test.ts +276 -0
  292. package/src/testing/manual-clock.ts +106 -0
  293. package/src/testing/mock-keys.test.ts +1356 -0
  294. package/src/testing/mock-keys.ts +449 -0
  295. package/src/testing/mock-mouse.test.ts +218 -0
  296. package/src/testing/mock-mouse.ts +247 -0
  297. package/src/testing/mock-tree-sitter-client.ts +73 -0
  298. package/src/testing/spy.ts +13 -0
  299. package/src/testing/test-recorder.test.ts +415 -0
  300. package/src/testing/test-recorder.ts +145 -0
  301. package/src/testing/test-renderer.ts +116 -0
  302. package/src/testing.ts +7 -0
  303. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +481 -0
  304. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +19 -0
  305. package/src/tests/__snapshots__/scrollbox.test.ts.snap +29 -0
  306. package/src/tests/absolute-positioning.snapshot.test.ts +638 -0
  307. package/src/tests/allocator-stats.test.ts +38 -0
  308. package/src/tests/destroy-during-render.test.ts +200 -0
  309. package/src/tests/hover-cursor.test.ts +98 -0
  310. package/src/tests/native-span-feed-async.test.ts +173 -0
  311. package/src/tests/native-span-feed-close.test.ts +120 -0
  312. package/src/tests/native-span-feed-coverage.test.ts +227 -0
  313. package/src/tests/native-span-feed-edge-cases.test.ts +352 -0
  314. package/src/tests/native-span-feed-use-after-free.test.ts +45 -0
  315. package/src/tests/opacity.test.ts +123 -0
  316. package/src/tests/renderable.snapshot.test.ts +524 -0
  317. package/src/tests/renderable.test.ts +1281 -0
  318. package/src/tests/renderer.console-startup.test.ts +65 -0
  319. package/src/tests/renderer.control.test.ts +364 -0
  320. package/src/tests/renderer.core-slot-binding.test.ts +952 -0
  321. package/src/tests/renderer.cursor.test.ts +26 -0
  322. package/src/tests/renderer.destroy-during-render.test.ts +110 -0
  323. package/src/tests/renderer.focus-restore.test.ts +228 -0
  324. package/src/tests/renderer.focus.test.ts +251 -0
  325. package/src/tests/renderer.idle.test.ts +219 -0
  326. package/src/tests/renderer.input.test.ts +2145 -0
  327. package/src/tests/renderer.kitty-flags.test.ts +195 -0
  328. package/src/tests/renderer.mouse.test.ts +1269 -0
  329. package/src/tests/renderer.palette.test.ts +629 -0
  330. package/src/tests/renderer.selection.test.ts +49 -0
  331. package/src/tests/renderer.slot-registry.test.ts +649 -0
  332. package/src/tests/renderer.useMouse.test.ts +50 -0
  333. package/src/tests/runtime-plugin-support.fixture.ts +11 -0
  334. package/src/tests/runtime-plugin-support.test.ts +28 -0
  335. package/src/tests/runtime-plugin.fixture.ts +40 -0
  336. package/src/tests/runtime-plugin.test.ts +190 -0
  337. package/src/tests/scrollbox-culling-bug.test.ts +114 -0
  338. package/src/tests/scrollbox-hitgrid-resize.test.ts +136 -0
  339. package/src/tests/scrollbox-hitgrid.test.ts +909 -0
  340. package/src/tests/scrollbox.test.ts +1530 -0
  341. package/src/tests/wrap-resize-perf.test.ts +229 -0
  342. package/src/tests/yoga-setters.test.ts +921 -0
  343. package/src/text-buffer-view.test.ts +705 -0
  344. package/src/text-buffer-view.ts +189 -0
  345. package/src/text-buffer.test.ts +347 -0
  346. package/src/text-buffer.ts +250 -0
  347. package/src/types.ts +152 -0
  348. package/src/utils.ts +88 -0
  349. package/src/zig/ansi.zig +268 -0
  350. package/src/zig/bench/README.md +50 -0
  351. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +887 -0
  352. package/src/zig/bench/edit-buffer_bench.zig +476 -0
  353. package/src/zig/bench/native-span-feed_bench.zig +100 -0
  354. package/src/zig/bench/rope-markers_bench.zig +713 -0
  355. package/src/zig/bench/rope_bench.zig +514 -0
  356. package/src/zig/bench/styled-text_bench.zig +470 -0
  357. package/src/zig/bench/text-buffer-coords_bench.zig +362 -0
  358. package/src/zig/bench/text-buffer-view_bench.zig +459 -0
  359. package/src/zig/bench/text-chunk-graphemes_bench.zig +273 -0
  360. package/src/zig/bench/utf8_bench.zig +799 -0
  361. package/src/zig/bench-utils.zig +431 -0
  362. package/src/zig/bench.zig +217 -0
  363. package/src/zig/buffer.zig +2223 -0
  364. package/src/zig/build.zig +289 -0
  365. package/src/zig/build.zig.zon +16 -0
  366. package/src/zig/edit-buffer.zig +825 -0
  367. package/src/zig/editor-view.zig +802 -0
  368. package/src/zig/event-bus.zig +13 -0
  369. package/src/zig/event-emitter.zig +65 -0
  370. package/src/zig/file-logger.zig +92 -0
  371. package/src/zig/grapheme.zig +599 -0
  372. package/src/zig/lib.zig +1834 -0
  373. package/src/zig/link.zig +333 -0
  374. package/src/zig/logger.zig +43 -0
  375. package/src/zig/mem-registry.zig +125 -0
  376. package/src/zig/native-span-feed-bench-lib.zig +7 -0
  377. package/src/zig/native-span-feed.zig +708 -0
  378. package/src/zig/renderer.zig +1386 -0
  379. package/src/zig/rope.zig +1220 -0
  380. package/src/zig/syntax-style.zig +161 -0
  381. package/src/zig/terminal.zig +975 -0
  382. package/src/zig/test.zig +70 -0
  383. package/src/zig/tests/README.md +18 -0
  384. package/src/zig/tests/buffer_test.zig +2526 -0
  385. package/src/zig/tests/edit-buffer-history_test.zig +271 -0
  386. package/src/zig/tests/edit-buffer_test.zig +1689 -0
  387. package/src/zig/tests/editor-view_test.zig +3299 -0
  388. package/src/zig/tests/event-emitter_test.zig +249 -0
  389. package/src/zig/tests/grapheme_test.zig +1304 -0
  390. package/src/zig/tests/link_test.zig +190 -0
  391. package/src/zig/tests/mem-registry_test.zig +473 -0
  392. package/src/zig/tests/memory_leak_regression_test.zig +159 -0
  393. package/src/zig/tests/native-span-feed_test.zig +1264 -0
  394. package/src/zig/tests/renderer_test.zig +1010 -0
  395. package/src/zig/tests/rope-nested_test.zig +712 -0
  396. package/src/zig/tests/rope_fuzz_test.zig +238 -0
  397. package/src/zig/tests/rope_test.zig +2362 -0
  398. package/src/zig/tests/segment-merge.test.zig +148 -0
  399. package/src/zig/tests/syntax-style_test.zig +557 -0
  400. package/src/zig/tests/terminal_test.zig +719 -0
  401. package/src/zig/tests/text-buffer-drawing_test.zig +3237 -0
  402. package/src/zig/tests/text-buffer-highlights_test.zig +666 -0
  403. package/src/zig/tests/text-buffer-iterators_test.zig +776 -0
  404. package/src/zig/tests/text-buffer-segment_test.zig +320 -0
  405. package/src/zig/tests/text-buffer-selection_test.zig +1035 -0
  406. package/src/zig/tests/text-buffer-selection_viewport_test.zig +358 -0
  407. package/src/zig/tests/text-buffer-view_test.zig +3649 -0
  408. package/src/zig/tests/text-buffer_test.zig +2191 -0
  409. package/src/zig/tests/unicode-width-map.zon +3909 -0
  410. package/src/zig/tests/utf8_no_zwj_test.zig +260 -0
  411. package/src/zig/tests/utf8_test.zig +4057 -0
  412. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +267 -0
  413. package/src/zig/tests/utf8_wcwidth_test.zig +357 -0
  414. package/src/zig/tests/word-wrap-editing_test.zig +498 -0
  415. package/src/zig/tests/wrap-cache-perf_test.zig +113 -0
  416. package/src/zig/text-buffer-iterators.zig +499 -0
  417. package/src/zig/text-buffer-segment.zig +404 -0
  418. package/src/zig/text-buffer-view.zig +1371 -0
  419. package/src/zig/text-buffer.zig +1180 -0
  420. package/src/zig/utf8.zig +1948 -0
  421. package/src/zig/utils.zig +9 -0
  422. package/src/zig-structs.ts +261 -0
  423. package/src/zig.ts +3843 -0
  424. package/tsconfig.build.json +22 -0
  425. package/tsconfig.json +28 -0
  426. package/3d/SpriteResourceManager.d.ts +0 -74
  427. package/3d/SpriteUtils.d.ts +0 -13
  428. package/3d/TextureUtils.d.ts +0 -24
  429. package/3d/ThreeRenderable.d.ts +0 -40
  430. package/3d/WGPURenderer.d.ts +0 -61
  431. package/3d/animation/ExplodingSpriteEffect.d.ts +0 -71
  432. package/3d/animation/PhysicsExplodingSpriteEffect.d.ts +0 -76
  433. package/3d/animation/SpriteAnimator.d.ts +0 -124
  434. package/3d/animation/SpriteParticleGenerator.d.ts +0 -62
  435. package/3d/canvas.d.ts +0 -44
  436. package/3d/index.d.ts +0 -12
  437. package/3d/physics/PlanckPhysicsAdapter.d.ts +0 -19
  438. package/3d/physics/RapierPhysicsAdapter.d.ts +0 -19
  439. package/3d/physics/physics-interface.d.ts +0 -27
  440. package/3d.d.ts +0 -2
  441. package/3d.js +0 -34042
  442. package/3d.js.map +0 -155
  443. package/LICENSE +0 -21
  444. package/NativeSpanFeed.d.ts +0 -41
  445. package/Renderable.d.ts +0 -334
  446. package/animation/Timeline.d.ts +0 -126
  447. package/ansi.d.ts +0 -13
  448. package/buffer.d.ts +0 -107
  449. package/console.d.ts +0 -143
  450. package/edit-buffer.d.ts +0 -98
  451. package/editor-view.d.ts +0 -73
  452. package/index-e4hzc2j2.js +0 -113
  453. package/index-e4hzc2j2.js.map +0 -10
  454. package/index-nkrr8a4c.js +0 -18415
  455. package/index-nkrr8a4c.js.map +0 -64
  456. package/index-nyw5p3ep.js +0 -12619
  457. package/index-nyw5p3ep.js.map +0 -43
  458. package/index.d.ts +0 -21
  459. package/index.js +0 -430
  460. package/index.js.map +0 -9
  461. package/lib/KeyHandler.d.ts +0 -61
  462. package/lib/RGBA.d.ts +0 -25
  463. package/lib/ascii.font.d.ts +0 -508
  464. package/lib/border.d.ts +0 -49
  465. package/lib/bunfs.d.ts +0 -7
  466. package/lib/clipboard.d.ts +0 -17
  467. package/lib/clock.d.ts +0 -15
  468. package/lib/data-paths.d.ts +0 -26
  469. package/lib/debounce.d.ts +0 -42
  470. package/lib/detect-links.d.ts +0 -6
  471. package/lib/env.d.ts +0 -42
  472. package/lib/extmarks-history.d.ts +0 -17
  473. package/lib/extmarks.d.ts +0 -89
  474. package/lib/hast-styled-text.d.ts +0 -17
  475. package/lib/index.d.ts +0 -21
  476. package/lib/keymapping.d.ts +0 -25
  477. package/lib/objects-in-viewport.d.ts +0 -24
  478. package/lib/output.capture.d.ts +0 -24
  479. package/lib/parse.keypress-kitty.d.ts +0 -2
  480. package/lib/parse.keypress.d.ts +0 -26
  481. package/lib/parse.mouse.d.ts +0 -30
  482. package/lib/paste.d.ts +0 -7
  483. package/lib/queue.d.ts +0 -15
  484. package/lib/renderable.validations.d.ts +0 -12
  485. package/lib/scroll-acceleration.d.ts +0 -43
  486. package/lib/selection.d.ts +0 -63
  487. package/lib/singleton.d.ts +0 -7
  488. package/lib/stdin-parser.d.ts +0 -76
  489. package/lib/styled-text.d.ts +0 -63
  490. package/lib/terminal-capability-detection.d.ts +0 -30
  491. package/lib/terminal-palette.d.ts +0 -50
  492. package/lib/tree-sitter/assets/update.d.ts +0 -11
  493. package/lib/tree-sitter/client.d.ts +0 -47
  494. package/lib/tree-sitter/default-parsers.d.ts +0 -2
  495. package/lib/tree-sitter/download-utils.d.ts +0 -21
  496. package/lib/tree-sitter/index.d.ts +0 -8
  497. package/lib/tree-sitter/parser.worker.d.ts +0 -1
  498. package/lib/tree-sitter/parsers-config.d.ts +0 -38
  499. package/lib/tree-sitter/resolve-ft.d.ts +0 -2
  500. package/lib/tree-sitter/types.d.ts +0 -81
  501. package/lib/tree-sitter-styled-text.d.ts +0 -14
  502. package/lib/validate-dir-name.d.ts +0 -1
  503. package/lib/yoga.options.d.ts +0 -32
  504. package/parser.worker.js +0 -869
  505. package/parser.worker.js.map +0 -12
  506. package/plugins/core-slot.d.ts +0 -72
  507. package/plugins/registry.d.ts +0 -38
  508. package/plugins/types.d.ts +0 -34
  509. package/post/filters.d.ts +0 -105
  510. package/renderables/ASCIIFont.d.ts +0 -52
  511. package/renderables/Box.d.ts +0 -72
  512. package/renderables/Code.d.ts +0 -78
  513. package/renderables/Diff.d.ts +0 -142
  514. package/renderables/EditBufferRenderable.d.ts +0 -162
  515. package/renderables/FrameBuffer.d.ts +0 -16
  516. package/renderables/Input.d.ts +0 -67
  517. package/renderables/LineNumberRenderable.d.ts +0 -74
  518. package/renderables/Markdown.d.ts +0 -173
  519. package/renderables/ScrollBar.d.ts +0 -77
  520. package/renderables/ScrollBox.d.ts +0 -124
  521. package/renderables/Select.d.ts +0 -115
  522. package/renderables/Slider.d.ts +0 -44
  523. package/renderables/TabSelect.d.ts +0 -96
  524. package/renderables/Text.d.ts +0 -36
  525. package/renderables/TextBufferRenderable.d.ts +0 -105
  526. package/renderables/TextNode.d.ts +0 -91
  527. package/renderables/TextTable.d.ts +0 -140
  528. package/renderables/Textarea.d.ts +0 -114
  529. package/renderables/TimeToFirstDraw.d.ts +0 -24
  530. package/renderables/__tests__/renderable-test-utils.d.ts +0 -12
  531. package/renderables/composition/VRenderable.d.ts +0 -16
  532. package/renderables/composition/constructs.d.ts +0 -35
  533. package/renderables/composition/vnode.d.ts +0 -46
  534. package/renderables/index.d.ts +0 -22
  535. package/renderables/markdown-parser.d.ts +0 -10
  536. package/renderer.d.ts +0 -388
  537. package/runtime-plugin-support.d.ts +0 -3
  538. package/runtime-plugin-support.js +0 -29
  539. package/runtime-plugin-support.js.map +0 -10
  540. package/runtime-plugin.d.ts +0 -11
  541. package/runtime-plugin.js +0 -16
  542. package/runtime-plugin.js.map +0 -9
  543. package/syntax-style.d.ts +0 -54
  544. package/testing/manual-clock.d.ts +0 -16
  545. package/testing/mock-keys.d.ts +0 -81
  546. package/testing/mock-mouse.d.ts +0 -38
  547. package/testing/mock-tree-sitter-client.d.ts +0 -23
  548. package/testing/spy.d.ts +0 -7
  549. package/testing/test-recorder.d.ts +0 -61
  550. package/testing/test-renderer.d.ts +0 -23
  551. package/testing.d.ts +0 -6
  552. package/testing.js +0 -675
  553. package/testing.js.map +0 -15
  554. package/text-buffer-view.d.ts +0 -42
  555. package/text-buffer.d.ts +0 -67
  556. package/types.d.ts +0 -131
  557. package/utils.d.ts +0 -14
  558. package/zig-structs.d.ts +0 -155
  559. package/zig.d.ts +0 -351
  560. /package/{assets → src/lib/tree-sitter/assets}/javascript/highlights.scm +0 -0
  561. /package/{assets → src/lib/tree-sitter/assets}/javascript/tree-sitter-javascript.wasm +0 -0
  562. /package/{assets → src/lib/tree-sitter/assets}/markdown/highlights.scm +0 -0
  563. /package/{assets → src/lib/tree-sitter/assets}/markdown/injections.scm +0 -0
  564. /package/{assets → src/lib/tree-sitter/assets}/markdown/tree-sitter-markdown.wasm +0 -0
  565. /package/{assets → src/lib/tree-sitter/assets}/markdown_inline/highlights.scm +0 -0
  566. /package/{assets → src/lib/tree-sitter/assets}/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  567. /package/{assets → src/lib/tree-sitter/assets}/typescript/highlights.scm +0 -0
  568. /package/{assets → src/lib/tree-sitter/assets}/typescript/tree-sitter-typescript.wasm +0 -0
  569. /package/{assets → src/lib/tree-sitter/assets}/zig/highlights.scm +0 -0
  570. /package/{assets → src/lib/tree-sitter/assets}/zig/tree-sitter-zig.wasm +0 -0
@@ -0,0 +1,2287 @@
1
+ import { test, expect, beforeAll, beforeEach, afterEach, afterAll } from "bun:test"
2
+ import { MarkdownRenderable, type MarkdownOptions } from "../Markdown.js"
3
+ import { CodeRenderable } from "../Code.js"
4
+ import { TextRenderable } from "../Text.js"
5
+ import { TextTableRenderable } from "../TextTable.js"
6
+ import { SyntaxStyle } from "../../syntax-style.js"
7
+ import { RGBA } from "../../lib/RGBA.js"
8
+ import { TreeSitterClient } from "../../lib/tree-sitter/index.js"
9
+ import { tmpdir } from "node:os"
10
+ import { join } from "node:path"
11
+ import { mkdir } from "node:fs/promises"
12
+ import {
13
+ createTestRenderer,
14
+ type MockMouse,
15
+ type TestRenderer,
16
+ MockTreeSitterClient,
17
+ TestRecorder,
18
+ } from "../../testing.js"
19
+ import { TextAttributes, type CapturedFrame } from "../../types.js"
20
+
21
+ let renderer: TestRenderer
22
+ let mockMouse: MockMouse
23
+ let renderOnce: () => Promise<void>
24
+ let captureFrame: () => string
25
+ let captureSpans: () => CapturedFrame
26
+ let markdownTreeSitterClient: TreeSitterClient
27
+
28
+ const syntaxStyle = SyntaxStyle.fromStyles({
29
+ default: { fg: RGBA.fromValues(1, 1, 1, 1) },
30
+ })
31
+
32
+ beforeAll(async () => {
33
+ const dataPath = join(tmpdir(), "tree-sitter-markdown-renderable-test-data")
34
+ await mkdir(dataPath, { recursive: true })
35
+
36
+ markdownTreeSitterClient = new TreeSitterClient({ dataPath })
37
+ await markdownTreeSitterClient.initialize()
38
+ })
39
+
40
+ beforeEach(async () => {
41
+ const testRenderer = await createTestRenderer({ width: 60, height: 40 })
42
+ renderer = testRenderer.renderer
43
+ mockMouse = testRenderer.mockMouse
44
+ renderOnce = testRenderer.renderOnce
45
+ captureFrame = testRenderer.captureCharFrame
46
+ captureSpans = testRenderer.captureSpans
47
+ })
48
+
49
+ afterEach(async () => {
50
+ if (renderer) {
51
+ renderer.destroy()
52
+ }
53
+ })
54
+
55
+ afterAll(async () => {
56
+ await markdownTreeSitterClient.destroy()
57
+ })
58
+
59
+ function createMarkdownRenderable(options: MarkdownOptions): MarkdownRenderable {
60
+ return new MarkdownRenderable(renderer, {
61
+ treeSitterClient: markdownTreeSitterClient,
62
+ ...options,
63
+ })
64
+ }
65
+
66
+ async function renderMarkdownRenderable(md: MarkdownRenderable, timeoutMs: number = 2000): Promise<void> {
67
+ const hasPendingMarkdownParagraphHighlights = (): boolean =>
68
+ md
69
+ .getChildren()
70
+ .some((child) => child instanceof CodeRenderable && child.filetype === "markdown" && child.isHighlighting)
71
+
72
+ const startedAt = Date.now()
73
+
74
+ await renderOnce()
75
+
76
+ while (hasPendingMarkdownParagraphHighlights() && Date.now() - startedAt < timeoutMs) {
77
+ await Bun.sleep(10)
78
+ await renderOnce()
79
+ }
80
+
81
+ if (hasPendingMarkdownParagraphHighlights()) {
82
+ throw new Error("Timed out waiting for markdown paragraph highlights")
83
+ }
84
+
85
+ await renderOnce()
86
+ }
87
+
88
+ async function renderMarkdown(markdown: string, conceal: boolean = true): Promise<string> {
89
+ const md = createMarkdownRenderable({
90
+ id: "markdown",
91
+ content: markdown,
92
+ syntaxStyle,
93
+ conceal,
94
+ tableOptions: { widthMode: "content" },
95
+ })
96
+
97
+ renderer.root.add(md)
98
+ await renderMarkdownRenderable(md)
99
+
100
+ const lines = captureFrame()
101
+ .split("\n")
102
+ .map((line) => line.trimEnd())
103
+ return "\n" + lines.join("\n").trimEnd()
104
+ }
105
+
106
+ test("basic table alignment", async () => {
107
+ const markdown = `| Name | Age |
108
+ |---|---|
109
+ | Alice | 30 |
110
+ | Bob | 5 |`
111
+
112
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
113
+ "
114
+ ┌─────┬───┐
115
+ │Name │Age│
116
+ ├─────┼───┤
117
+ │Alice│30 │
118
+ ├─────┼───┤
119
+ │Bob │5 │
120
+ └─────┴───┘"
121
+ `)
122
+ })
123
+
124
+ test("tableOptions.widthMode configures markdown table layout", async () => {
125
+ const md = createMarkdownRenderable({
126
+ id: "markdown-table-width-mode",
127
+ content: "| Name | Age |\n|---|---|\n| Alice | 30 |",
128
+ syntaxStyle,
129
+ tableOptions: {
130
+ widthMode: "full",
131
+ columnFitter: "balanced",
132
+ },
133
+ })
134
+
135
+ renderer.root.add(md)
136
+ await renderer.idle()
137
+
138
+ const table = md._blockStates[0]?.renderable as TextTableRenderable
139
+ expect(table).toBeInstanceOf(TextTableRenderable)
140
+ expect(table.columnWidthMode).toBe("full")
141
+ expect(table.columnFitter).toBe("balanced")
142
+ })
143
+
144
+ test("tableOptions updates existing markdown table renderable", async () => {
145
+ const md = createMarkdownRenderable({
146
+ id: "markdown-table-updates",
147
+ content: "| Name | Age |\n|---|---|\n| Alice | 30 |",
148
+ syntaxStyle,
149
+ })
150
+
151
+ renderer.root.add(md)
152
+ await renderer.idle()
153
+
154
+ const table = md._blockStates[0]?.renderable as TextTableRenderable
155
+ expect(table).toBeInstanceOf(TextTableRenderable)
156
+ expect(table.columnWidthMode).toBe("full")
157
+
158
+ md.tableOptions = {
159
+ widthMode: "full",
160
+ columnFitter: "balanced",
161
+ wrapMode: "word",
162
+ cellPadding: 1,
163
+ borders: false,
164
+ selectable: false,
165
+ }
166
+
167
+ await renderer.idle()
168
+
169
+ const updatedTable = md._blockStates[0]?.renderable as TextTableRenderable
170
+ expect(updatedTable).toBe(table)
171
+ expect(updatedTable.columnWidthMode).toBe("full")
172
+ expect(updatedTable.columnFitter).toBe("balanced")
173
+ expect(updatedTable.wrapMode).toBe("word")
174
+ expect(updatedTable.cellPadding).toBe(1)
175
+ expect(updatedTable.border).toBe(false)
176
+ expect(updatedTable.outerBorder).toBe(false)
177
+ expect(updatedTable.showBorders).toBe(false)
178
+ expect(updatedTable.selectable).toBe(false)
179
+ })
180
+
181
+ test("table with inline code (backticks)", async () => {
182
+ const markdown = `| Command | Description |
183
+ |---|---|
184
+ | \`npm install\` | Install deps |
185
+ | \`npm run build\` | Build project |
186
+ | \`npm test\` | Run tests |`
187
+
188
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
189
+ "
190
+ ┌─────────────┬─────────────┐
191
+ │Command │Description │
192
+ ├─────────────┼─────────────┤
193
+ │npm install │Install deps │
194
+ ├─────────────┼─────────────┤
195
+ │npm run build│Build project│
196
+ ├─────────────┼─────────────┤
197
+ │npm test │Run tests │
198
+ └─────────────┴─────────────┘"
199
+ `)
200
+ })
201
+
202
+ test("table with bold text", async () => {
203
+ const markdown = `| Feature | Status |
204
+ |---|---|
205
+ | **Authentication** | Done |
206
+ | **API** | WIP |`
207
+
208
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
209
+ "
210
+ ┌──────────────┬──────┐
211
+ │Feature │Status│
212
+ ├──────────────┼──────┤
213
+ │Authentication│Done │
214
+ ├──────────────┼──────┤
215
+ │API │WIP │
216
+ └──────────────┴──────┘"
217
+ `)
218
+ })
219
+
220
+ test("table with italic text", async () => {
221
+ const markdown = `| Item | Note |
222
+ |---|---|
223
+ | One | *important* |
224
+ | Two | *ok* |`
225
+
226
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
227
+ "
228
+ ┌────┬─────────┐
229
+ │Item│Note │
230
+ ├────┼─────────┤
231
+ │One │important│
232
+ ├────┼─────────┤
233
+ │Two │ok │
234
+ └────┴─────────┘"
235
+ `)
236
+ })
237
+
238
+ test("table with mixed formatting", async () => {
239
+ const markdown = `| Type | Value | Notes |
240
+ |---|---|---|
241
+ | **Bold** | \`code\` | *italic* |
242
+ | Plain | **strong** | \`cmd\` |`
243
+
244
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
245
+ "
246
+ ┌─────┬──────┬──────┐
247
+ │Type │Value │Notes │
248
+ ├─────┼──────┼──────┤
249
+ │Bold │code │italic│
250
+ ├─────┼──────┼──────┤
251
+ │Plain│strong│cmd │
252
+ └─────┴──────┴──────┘"
253
+ `)
254
+ })
255
+
256
+ test("table with alignment markers (left, center, right)", async () => {
257
+ const markdown = `| Left | Center | Right |
258
+ |:---|:---:|---:|
259
+ | A | B | C |
260
+ | Long text | X | Y |`
261
+
262
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
263
+ "
264
+ ┌─────────┬──────┬─────┐
265
+ │Left │Center│Right│
266
+ ├─────────┼──────┼─────┤
267
+ │A │B │C │
268
+ ├─────────┼──────┼─────┤
269
+ │Long text│X │Y │
270
+ └─────────┴──────┴─────┘"
271
+ `)
272
+ })
273
+
274
+ test("table with empty cells", async () => {
275
+ const markdown = `| A | B |
276
+ |---|---|
277
+ | X | |
278
+ | | Y |`
279
+
280
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
281
+ "
282
+ ┌─┬─┐
283
+ │A│B│
284
+ ├─┼─┤
285
+ │X│ │
286
+ ├─┼─┤
287
+ │ │Y│
288
+ └─┴─┘"
289
+ `)
290
+ })
291
+
292
+ test("table with long header and short content", async () => {
293
+ const markdown = `| Very Long Column Header | Short |
294
+ |---|---|
295
+ | A | B |`
296
+
297
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
298
+ "
299
+ ┌───────────────────────┬─────┐
300
+ │Very Long Column Header│Short│
301
+ ├───────────────────────┼─────┤
302
+ │A │B │
303
+ └───────────────────────┴─────┘"
304
+ `)
305
+ })
306
+
307
+ test("table with short header and long content", async () => {
308
+ const markdown = `| X | Y |
309
+ |---|---|
310
+ | This is very long content | Short |`
311
+
312
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
313
+ "
314
+ ┌─────────────────────────┬─────┐
315
+ │X │Y │
316
+ ├─────────────────────────┼─────┤
317
+ │This is very long content│Short│
318
+ └─────────────────────────┴─────┘"
319
+ `)
320
+ })
321
+
322
+ test("table inside code block should NOT be formatted", async () => {
323
+ const markdown = `\`\`\`
324
+ | Not | A | Table |
325
+ |---|---|---|
326
+ | Should | Stay | Raw |
327
+ \`\`\`
328
+
329
+ | Real | Table |
330
+ |---|---|
331
+ | Is | Formatted |`
332
+
333
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
334
+ "
335
+ | Not | A | Table |
336
+ |---|---|---|
337
+ | Should | Stay | Raw |
338
+
339
+ ┌────┬─────────┐
340
+ │Real│Table │
341
+ ├────┼─────────┤
342
+ │Is │Formatted│
343
+ └────┴─────────┘"
344
+ `)
345
+ })
346
+
347
+ test("multiple tables in same document", async () => {
348
+ const markdown = `| Table1 | A |
349
+ |---|---|
350
+ | X | Y |
351
+
352
+ Some text between.
353
+
354
+ | Table2 | BB |
355
+ |---|---|
356
+ | Long content | Z |`
357
+
358
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
359
+ "
360
+ ┌──────┬─┐
361
+ │Table1│A│
362
+ ├──────┼─┤
363
+ │X │Y│
364
+ └──────┴─┘
365
+
366
+ Some text between.
367
+ ┌────────────┬──┐
368
+ │Table2 │BB│
369
+ ├────────────┼──┤
370
+ │Long content│Z │
371
+ └────────────┴──┘"
372
+ `)
373
+ })
374
+
375
+ test("table with escaped pipe character", async () => {
376
+ const markdown = `| Command | Output |
377
+ |---|---|
378
+ | echo | Hello |
379
+ | ls \\| grep | Filtered |`
380
+
381
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
382
+ "
383
+ ┌─────────┬────────┐
384
+ │Command │Output │
385
+ ├─────────┼────────┤
386
+ │echo │Hello │
387
+ ├─────────┼────────┤
388
+ │ls | grep│Filtered│
389
+ └─────────┴────────┘"
390
+ `)
391
+ })
392
+
393
+ test("table with unicode characters", async () => {
394
+ const markdown = `| Emoji | Name |
395
+ |---|---|
396
+ | 🎉 | Party |
397
+ | 🚀 | Rocket |
398
+ | 日本語 | Japanese |`
399
+
400
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
401
+ "
402
+ ┌──────┬────────┐
403
+ │Emoji │Name │
404
+ ├──────┼────────┤
405
+ │🎉 │Party │
406
+ ├──────┼────────┤
407
+ │🚀 │Rocket │
408
+ ├──────┼────────┤
409
+ │日本語│Japanese│
410
+ └──────┴────────┘"
411
+ `)
412
+ })
413
+
414
+ test("table with links", async () => {
415
+ const markdown = `| Name | Link |
416
+ |---|---|
417
+ | Google | [link](https://google.com) |
418
+ | GitHub | [gh](https://github.com) |`
419
+
420
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
421
+ "
422
+ ┌──────┬─────────────────────────┐
423
+ │Name │Link │
424
+ ├──────┼─────────────────────────┤
425
+ │Google│link (https://google.com)│
426
+ ├──────┼─────────────────────────┤
427
+ │GitHub│gh (https://github.com) │
428
+ └──────┴─────────────────────────┘"
429
+ `)
430
+ })
431
+
432
+ test("single row table (header + delimiter only)", async () => {
433
+ const markdown = `| Only | Header |
434
+ |---|---|`
435
+
436
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
437
+ "
438
+ | Only | Header |
439
+ |---|---|"
440
+ `)
441
+ })
442
+
443
+ test("table with many columns", async () => {
444
+ const markdown = `| A | B | C | D | E |
445
+ |---|---|---|---|---|
446
+ | 1 | 2 | 3 | 4 | 5 |`
447
+
448
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
449
+ "
450
+ ┌─┬─┬─┬─┬─┐
451
+ │A│B│C│D│E│
452
+ ├─┼─┼─┼─┼─┤
453
+ │1│2│3│4│5│
454
+ └─┴─┴─┴─┴─┘"
455
+ `)
456
+ })
457
+
458
+ test("no tables returns original content", async () => {
459
+ const markdown = `# Just a heading
460
+
461
+ Some paragraph text.
462
+
463
+ - List item`
464
+
465
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
466
+ "
467
+ Just a heading
468
+
469
+ Some paragraph text.
470
+
471
+ - List item"
472
+ `)
473
+ })
474
+
475
+ test("table with nested inline formatting", async () => {
476
+ const markdown = `| Description |
477
+ |---|
478
+ | This has **bold and \`code\`** together |
479
+ | And *italic with **nested bold*** |`
480
+
481
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
482
+ "
483
+ ┌───────────────────────────────┐
484
+ │Description │
485
+ ├───────────────────────────────┤
486
+ │This has bold and code together│
487
+ ├───────────────────────────────┤
488
+ │And italic with nested bold │
489
+ └───────────────────────────────┘"
490
+ `)
491
+ })
492
+
493
+ // Tests with conceal=false - formatting markers should be visible and columns sized accordingly
494
+
495
+ test("conceal=false: table with bold text", async () => {
496
+ const markdown = `| Feature | Status |
497
+ |---|---|
498
+ | **Authentication** | Done |
499
+ | **API** | WIP |`
500
+
501
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
502
+ "
503
+ ┌──────────────────┬──────┐
504
+ │Feature │Status│
505
+ ├──────────────────┼──────┤
506
+ │**Authentication**│Done │
507
+ ├──────────────────┼──────┤
508
+ │**API** │WIP │
509
+ └──────────────────┴──────┘"
510
+ `)
511
+ })
512
+
513
+ test("conceal=false: table with inline code", async () => {
514
+ const markdown = `| Command | Description |
515
+ |---|---|
516
+ | \`npm install\` | Install deps |
517
+ | \`npm run build\` | Build project |`
518
+
519
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
520
+ "
521
+ ┌───────────────┬─────────────┐
522
+ │Command │Description │
523
+ ├───────────────┼─────────────┤
524
+ │\`npm install\` │Install deps │
525
+ ├───────────────┼─────────────┤
526
+ │\`npm run build\`│Build project│
527
+ └───────────────┴─────────────┘"
528
+ `)
529
+ })
530
+
531
+ test("conceal=false: table with italic text", async () => {
532
+ const markdown = `| Item | Note |
533
+ |---|---|
534
+ | One | *important* |
535
+ | Two | *ok* |`
536
+
537
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
538
+ "
539
+ ┌────┬───────────┐
540
+ │Item│Note │
541
+ ├────┼───────────┤
542
+ │One │*important*│
543
+ ├────┼───────────┤
544
+ │Two │*ok* │
545
+ └────┴───────────┘"
546
+ `)
547
+ })
548
+
549
+ test("conceal=false: table with mixed formatting", async () => {
550
+ const markdown = `| Type | Value | Notes |
551
+ |---|---|---|
552
+ | **Bold** | \`code\` | *italic* |
553
+ | Plain | **strong** | \`cmd\` |`
554
+
555
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
556
+ "
557
+ ┌────────┬──────────┬────────┐
558
+ │Type │Value │Notes │
559
+ ├────────┼──────────┼────────┤
560
+ │**Bold**│\`code\` │*italic*│
561
+ ├────────┼──────────┼────────┤
562
+ │Plain │**strong**│\`cmd\` │
563
+ └────────┴──────────┴────────┘"
564
+ `)
565
+ })
566
+
567
+ test("conceal=false: table with unicode characters", async () => {
568
+ const markdown = `| Emoji | Name |
569
+ |---|---|
570
+ | 🎉 | Party |
571
+ | 🚀 | Rocket |
572
+ | 日本語 | Japanese |`
573
+
574
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
575
+ "
576
+ ┌──────┬────────┐
577
+ │Emoji │Name │
578
+ ├──────┼────────┤
579
+ │🎉 │Party │
580
+ ├──────┼────────┤
581
+ │🚀 │Rocket │
582
+ ├──────┼────────┤
583
+ │日本語│Japanese│
584
+ └──────┴────────┘"
585
+ `)
586
+ })
587
+
588
+ test("conceal=false: basic table alignment", async () => {
589
+ const markdown = `| Name | Age |
590
+ |---|---|
591
+ | Alice | 30 |
592
+ | Bob | 5 |`
593
+
594
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
595
+ "
596
+ ┌─────┬───┐
597
+ │Name │Age│
598
+ ├─────┼───┤
599
+ │Alice│30 │
600
+ ├─────┼───┤
601
+ │Bob │5 │
602
+ └─────┴───┘"
603
+ `)
604
+ })
605
+
606
+ test("table with paragraphs before and after", async () => {
607
+ const markdown = `This is a paragraph before the table.
608
+
609
+ | Name | Age |
610
+ |---|---|
611
+ | Alice | 30 |
612
+
613
+ This is a paragraph after the table.`
614
+
615
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
616
+ "
617
+ This is a paragraph before the table.
618
+ ┌─────┬───┐
619
+ │Name │Age│
620
+ ├─────┼───┤
621
+ │Alice│30 │
622
+ └─────┴───┘
623
+
624
+ This is a paragraph after the table."
625
+ `)
626
+ })
627
+
628
+ test("selection across markdown table includes table data", async () => {
629
+ const markdown = `Intro line above table.
630
+
631
+ | Component | Status | Notes |
632
+ |---|---|---|
633
+ | Authentication | **Done** | OAuth2 + SSO |
634
+ | Payments API | *In Progress* | Retry + idempotency |
635
+ | Search Indexer | \`Done\` | Ranking + typo fix |
636
+
637
+ Outro line below table.`
638
+
639
+ const md = createMarkdownRenderable({
640
+ id: "markdown",
641
+ content: markdown,
642
+ syntaxStyle,
643
+ })
644
+
645
+ renderer.root.add(md)
646
+ await renderMarkdownRenderable(md)
647
+
648
+ const topBlock = md._blockStates[0]?.renderable as CodeRenderable | undefined
649
+ const tableBlock = md._blockStates[1]?.renderable as TextTableRenderable | undefined
650
+ const bottomBlock = md._blockStates[2]?.renderable as CodeRenderable | undefined
651
+
652
+ expect(topBlock).toBeInstanceOf(CodeRenderable)
653
+ expect(tableBlock).toBeInstanceOf(TextTableRenderable)
654
+ expect(bottomBlock).toBeInstanceOf(CodeRenderable)
655
+
656
+ const startX = topBlock!.x + 1
657
+ const startY = topBlock!.y
658
+ const endX = Math.max(bottomBlock!.x + bottomBlock!.width - 2, startX + 1)
659
+ const endY = bottomBlock!.y
660
+
661
+ await mockMouse.drag(startX, startY, endX, endY)
662
+ await renderer.idle()
663
+
664
+ const selectedText = renderer.getSelection()?.getSelectedText() ?? ""
665
+
666
+ expect(selectedText).toContain("Authentication")
667
+ expect(selectedText).toContain("Payments API")
668
+ expect(selectedText).toContain("Retry + idempotency")
669
+ })
670
+
671
+ // Code block tests
672
+
673
+ test("code block with language", async () => {
674
+ const markdown = `\`\`\`typescript
675
+ const x = 1;
676
+ console.log(x);
677
+ \`\`\``
678
+
679
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
680
+ "
681
+ const x = 1;
682
+ console.log(x);"
683
+ `)
684
+ })
685
+
686
+ test("code block without language", async () => {
687
+ const markdown = `\`\`\`
688
+ plain code block
689
+ with multiple lines
690
+ \`\`\``
691
+
692
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
693
+ "
694
+ plain code block
695
+ with multiple lines"
696
+ `)
697
+ })
698
+
699
+ test("code block mixed with text", async () => {
700
+ const markdown = `Here is some code:
701
+
702
+ \`\`\`js
703
+ function hello() {
704
+ return "world";
705
+ }
706
+ \`\`\`
707
+
708
+ And here is more text after.`
709
+
710
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
711
+ "
712
+ Here is some code:
713
+ function hello() {
714
+ return "world";
715
+ }
716
+
717
+ And here is more text after."
718
+ `)
719
+ })
720
+
721
+ test("multiple code blocks", async () => {
722
+ const markdown = `First block:
723
+
724
+ \`\`\`python
725
+ print("hello")
726
+ \`\`\`
727
+
728
+ Second block:
729
+
730
+ \`\`\`rust
731
+ fn main() {}
732
+ \`\`\``
733
+
734
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
735
+ "
736
+ First block:
737
+ print("hello")
738
+
739
+ Second block:
740
+ fn main() {}"
741
+ `)
742
+ })
743
+
744
+ test("code block in conceal=false mode", async () => {
745
+ const markdown = `\`\`\`js
746
+ const x = 1;
747
+ \`\`\``
748
+
749
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
750
+ "
751
+ const x = 1;"
752
+ `)
753
+ })
754
+
755
+ test("code block concealment is disabled by default", async () => {
756
+ const mockTreeSitterClient = new MockTreeSitterClient()
757
+ mockTreeSitterClient.setMockResult({
758
+ highlights: [[0, 1, "conceal", { conceal: "" }]],
759
+ })
760
+
761
+ const md = createMarkdownRenderable({
762
+ id: "markdown-code-default-conceal",
763
+ content: "```markdown\n# Hidden heading\n```",
764
+ syntaxStyle,
765
+ conceal: true,
766
+ treeSitterClient: mockTreeSitterClient,
767
+ })
768
+
769
+ renderer.root.add(md)
770
+ await renderer.idle()
771
+ expect(mockTreeSitterClient.isHighlighting()).toBe(true)
772
+
773
+ mockTreeSitterClient.resolveAllHighlightOnce()
774
+ await Bun.sleep(10)
775
+ await renderer.idle()
776
+
777
+ const frame = captureFrame()
778
+ expect(frame).toContain("# Hidden heading")
779
+ })
780
+
781
+ test("code block concealment can be enabled with concealCode", async () => {
782
+ const mockTreeSitterClient = new MockTreeSitterClient()
783
+ mockTreeSitterClient.setMockResult({
784
+ highlights: [[0, 1, "conceal", { conceal: "" }]],
785
+ })
786
+
787
+ const md = createMarkdownRenderable({
788
+ id: "markdown-code-conceal-enabled",
789
+ content: "```markdown\n# Hidden heading\n```",
790
+ syntaxStyle,
791
+ conceal: true,
792
+ concealCode: true,
793
+ treeSitterClient: mockTreeSitterClient,
794
+ })
795
+
796
+ renderer.root.add(md)
797
+ await renderer.idle()
798
+ expect(mockTreeSitterClient.isHighlighting()).toBe(true)
799
+
800
+ mockTreeSitterClient.resolveAllHighlightOnce()
801
+ await Bun.sleep(10)
802
+ await renderer.idle()
803
+
804
+ const frame = captureFrame()
805
+ expect(frame).not.toContain("# Hidden heading")
806
+ expect(frame).toContain("Hidden heading")
807
+ })
808
+
809
+ test("toggling concealCode updates existing code block renderables", async () => {
810
+ const mockTreeSitterClient = new MockTreeSitterClient()
811
+ mockTreeSitterClient.setMockResult({
812
+ highlights: [[0, 1, "conceal", { conceal: "" }]],
813
+ })
814
+
815
+ const md = createMarkdownRenderable({
816
+ id: "markdown-code-conceal-toggle",
817
+ content: "```markdown\n# Hidden heading\n```",
818
+ syntaxStyle,
819
+ conceal: true,
820
+ concealCode: false,
821
+ treeSitterClient: mockTreeSitterClient,
822
+ })
823
+
824
+ renderer.root.add(md)
825
+ await renderer.idle()
826
+ expect(mockTreeSitterClient.isHighlighting()).toBe(true)
827
+
828
+ mockTreeSitterClient.resolveAllHighlightOnce()
829
+ await Bun.sleep(10)
830
+ await renderer.idle()
831
+
832
+ const frameBefore = captureFrame()
833
+ expect(frameBefore).toContain("# Hidden heading")
834
+
835
+ md.concealCode = true
836
+ renderer.requestRender()
837
+ await renderer.idle()
838
+ expect(mockTreeSitterClient.isHighlighting()).toBe(true)
839
+
840
+ mockTreeSitterClient.resolveAllHighlightOnce()
841
+ await Bun.sleep(10)
842
+ await renderer.idle()
843
+
844
+ const frameAfter = captureFrame()
845
+ expect(frameAfter).not.toContain("# Hidden heading")
846
+ expect(frameAfter).toContain("Hidden heading")
847
+ })
848
+
849
+ // Heading tests
850
+
851
+ test("headings h1 through h3", async () => {
852
+ const markdown = `# Heading 1
853
+
854
+ ## Heading 2
855
+
856
+ ### Heading 3`
857
+
858
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
859
+ "
860
+ Heading 1
861
+
862
+ Heading 2
863
+
864
+ Heading 3"
865
+ `)
866
+ })
867
+
868
+ test("headings with conceal=false show markers", async () => {
869
+ const markdown = `# Heading 1
870
+
871
+ ## Heading 2`
872
+
873
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
874
+ "
875
+ # Heading 1
876
+
877
+ ## Heading 2"
878
+ `)
879
+ })
880
+
881
+ // List tests
882
+
883
+ test("unordered list", async () => {
884
+ const markdown = `- Item one
885
+ - Item two
886
+ - Item three`
887
+
888
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
889
+ "
890
+ - Item one
891
+ - Item two
892
+ - Item three"
893
+ `)
894
+ })
895
+
896
+ test("ordered list", async () => {
897
+ const markdown = `1. First item
898
+ 2. Second item
899
+ 3. Third item`
900
+
901
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
902
+ "
903
+ 1. First item
904
+ 2. Second item
905
+ 3. Third item"
906
+ `)
907
+ })
908
+
909
+ test("list with inline formatting", async () => {
910
+ const markdown = `- **Bold** item
911
+ - *Italic* item
912
+ - \`Code\` item`
913
+
914
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
915
+ "
916
+ - Bold item
917
+ - Italic item
918
+ - Code item"
919
+ `)
920
+ })
921
+
922
+ // Blockquote tests
923
+
924
+ test("simple blockquote", async () => {
925
+ const markdown = `> This is a quote
926
+ > spanning multiple lines`
927
+
928
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
929
+ "
930
+ > This is a quote
931
+ > spanning multiple lines"
932
+ `)
933
+ })
934
+
935
+ // Inline formatting tests
936
+
937
+ test("bold text", async () => {
938
+ const markdown = `This has **bold** text in it.`
939
+
940
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
941
+ "
942
+ This has bold text in it."
943
+ `)
944
+ })
945
+
946
+ test("italic text", async () => {
947
+ const markdown = `This has *italic* text in it.`
948
+
949
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
950
+ "
951
+ This has italic text in it."
952
+ `)
953
+ })
954
+
955
+ test("inline code", async () => {
956
+ const markdown = `Use \`console.log()\` to debug.`
957
+
958
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
959
+ "
960
+ Use console.log() to debug."
961
+ `)
962
+ })
963
+
964
+ test("mixed inline formatting", async () => {
965
+ const markdown = `**Bold**, *italic*, and \`code\` together.`
966
+
967
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
968
+ "
969
+ Bold, italic, and code together."
970
+ `)
971
+ })
972
+
973
+ test("inline formatting with conceal=false", async () => {
974
+ const markdown = `**Bold**, *italic*, and \`code\` together.`
975
+
976
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
977
+ "
978
+ **Bold**, *italic*, and \`code\` together."
979
+ `)
980
+ })
981
+
982
+ // Link tests
983
+
984
+ test("links with conceal mode", async () => {
985
+ const markdown = `Check out [OpenTUI](https://github.com/sst/opentui) for more.`
986
+
987
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
988
+ "
989
+ Check out OpenTUI (https://github.com/sst/opentui) for more."
990
+ `)
991
+ })
992
+
993
+ test("links with conceal=false", async () => {
994
+ const markdown = `Check out [OpenTUI](https://github.com/sst/opentui) for more.`
995
+
996
+ expect(await renderMarkdown(markdown, false)).toMatchInlineSnapshot(`
997
+ "
998
+ Check out [OpenTUI](https://github.com/sst/opentui) for
999
+ more."
1000
+ `)
1001
+ })
1002
+
1003
+ // Horizontal rule
1004
+
1005
+ test("horizontal rule", async () => {
1006
+ const markdown = `Before
1007
+
1008
+ ---
1009
+
1010
+ After`
1011
+
1012
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1013
+ "
1014
+ Before
1015
+
1016
+ ---
1017
+
1018
+ After"
1019
+ `)
1020
+ })
1021
+
1022
+ // Complex document
1023
+
1024
+ test("complex markdown document", async () => {
1025
+ const markdown = `# Project Title
1026
+
1027
+ Welcome to **OpenTUI**, a terminal UI library.
1028
+
1029
+ ## Features
1030
+
1031
+ - Automatic table alignment
1032
+ - \`inline code\` support
1033
+ - *Italic* and **bold** text
1034
+
1035
+ ## Code Example
1036
+
1037
+ \`\`\`typescript
1038
+ const md = new MarkdownRenderable(ctx, {
1039
+ content: "# Hello",
1040
+ })
1041
+ \`\`\`
1042
+
1043
+ ## Links
1044
+
1045
+ Visit [GitHub](https://github.com) for more.
1046
+
1047
+ ---
1048
+
1049
+ *Press \`?\` for help*`
1050
+
1051
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1052
+ "
1053
+ Project Title
1054
+
1055
+ Welcome to OpenTUI, a terminal UI library.
1056
+
1057
+ Features
1058
+
1059
+ - Automatic table alignment
1060
+ - inline code support
1061
+ - Italic and bold text
1062
+
1063
+ Code Example
1064
+
1065
+ const md = new MarkdownRenderable(ctx, {
1066
+ content: "# Hello",
1067
+ })
1068
+
1069
+ Links
1070
+
1071
+ Visit GitHub (https://github.com) for more.
1072
+
1073
+ ---
1074
+
1075
+ Press ? for help"
1076
+ `)
1077
+ })
1078
+
1079
+ // Custom renderNode tests
1080
+
1081
+ test("custom renderNode can override heading rendering", async () => {
1082
+ const { TextRenderable } = await import("../Text")
1083
+ const { StyledText } = await import("../../lib/styled-text")
1084
+
1085
+ // Helper to extract text from marked tokens
1086
+ const extractText = (node: any): string => {
1087
+ if (node.type === "text") return node.text
1088
+ if (node.tokens) return node.tokens.map(extractText).join("")
1089
+ return ""
1090
+ }
1091
+
1092
+ const md = createMarkdownRenderable({
1093
+ id: "custom-heading",
1094
+ content: `# Custom Heading
1095
+
1096
+ Regular paragraph.`,
1097
+ syntaxStyle,
1098
+ renderNode: (node, ctx) => {
1099
+ if (node.type === "heading") {
1100
+ const text = extractText(node)
1101
+ return new TextRenderable(renderer, {
1102
+ id: "custom",
1103
+ content: new StyledText([{ __isChunk: true, text: `[CUSTOM] ${text}`, attributes: 0 }]),
1104
+ width: "100%",
1105
+ })
1106
+ }
1107
+ return ctx.defaultRender()
1108
+ },
1109
+ })
1110
+
1111
+ renderer.root.add(md)
1112
+ await renderMarkdownRenderable(md)
1113
+
1114
+ const lines = captureFrame()
1115
+ .split("\n")
1116
+ .map((line) => line.trimEnd())
1117
+ expect("\n" + lines.join("\n").trimEnd()).toMatchInlineSnapshot(`
1118
+ "
1119
+ [CUSTOM] Custom Heading
1120
+ Regular paragraph."
1121
+ `)
1122
+ })
1123
+
1124
+ test("custom renderNode can override code block rendering", async () => {
1125
+ const { BoxRenderable } = await import("../Box")
1126
+ const { TextRenderable } = await import("../Text")
1127
+
1128
+ const md = createMarkdownRenderable({
1129
+ id: "custom-code",
1130
+ content: `\`\`\`js
1131
+ const x = 1;
1132
+ \`\`\``,
1133
+ syntaxStyle,
1134
+ renderNode: (node, ctx) => {
1135
+ if (node.type === "code") {
1136
+ const box = new BoxRenderable(renderer, {
1137
+ id: "code-box",
1138
+ border: true,
1139
+ borderStyle: "single",
1140
+ })
1141
+ box.add(
1142
+ new TextRenderable(renderer, {
1143
+ id: "code-text",
1144
+ content: `CODE: ${(node as any).text}`,
1145
+ }),
1146
+ )
1147
+ return box
1148
+ }
1149
+ return ctx.defaultRender()
1150
+ },
1151
+ })
1152
+
1153
+ renderer.root.add(md)
1154
+ await renderMarkdownRenderable(md)
1155
+
1156
+ const lines = captureFrame()
1157
+ .split("\n")
1158
+ .map((line) => line.trimEnd())
1159
+ expect("\n" + lines.join("\n").trimEnd()).toMatchInlineSnapshot(`
1160
+ "
1161
+ ┌──────────────────────────────────────────────────────────┐
1162
+ │CODE: const x = 1; │
1163
+ └──────────────────────────────────────────────────────────┘"
1164
+ `)
1165
+ })
1166
+
1167
+ test("custom renderNode returning null uses default", async () => {
1168
+ const md = createMarkdownRenderable({
1169
+ id: "custom-null",
1170
+ content: `# Heading
1171
+
1172
+ Paragraph text.`,
1173
+ syntaxStyle,
1174
+ renderNode: () => null,
1175
+ })
1176
+
1177
+ renderer.root.add(md)
1178
+ await renderMarkdownRenderable(md)
1179
+
1180
+ const lines = captureFrame()
1181
+ .split("\n")
1182
+ .map((line) => line.trimEnd())
1183
+ expect("\n" + lines.join("\n").trimEnd()).toMatchInlineSnapshot(`
1184
+ "
1185
+ Heading
1186
+
1187
+
1188
+ Paragraph text."
1189
+ `)
1190
+ })
1191
+
1192
+ // Incomplete/invalid markdown tests
1193
+
1194
+ test("incomplete code block (no closing fence)", async () => {
1195
+ const markdown = `Here is some code:
1196
+
1197
+ \`\`\`javascript
1198
+ const x = 1;
1199
+ console.log(x);`
1200
+
1201
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1202
+ "
1203
+ Here is some code:
1204
+ const x = 1;
1205
+ console.log(x);"
1206
+ `)
1207
+ })
1208
+
1209
+ test("incomplete bold (no closing **)", async () => {
1210
+ const markdown = `This has **unclosed bold text`
1211
+
1212
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1213
+ "
1214
+ This has **unclosed bold text"
1215
+ `)
1216
+ })
1217
+
1218
+ test("incomplete italic (no closing *)", async () => {
1219
+ const markdown = `This has *unclosed italic text`
1220
+
1221
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1222
+ "
1223
+ This has *unclosed italic text"
1224
+ `)
1225
+ })
1226
+
1227
+ test("incomplete link (no closing paren)", async () => {
1228
+ const markdown = `Check out [this link](https://example.com`
1229
+
1230
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1231
+ "
1232
+ Check out this link(https://example.com"
1233
+ `)
1234
+ })
1235
+
1236
+ test("incomplete table (only header)", async () => {
1237
+ const markdown = `| Header1 | Header2 |`
1238
+
1239
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1240
+ "
1241
+ | Header1 | Header2 |"
1242
+ `)
1243
+ })
1244
+
1245
+ test("incomplete table (header + delimiter, no rows)", async () => {
1246
+ const markdown = `| Header1 | Header2 |
1247
+ |---|---|`
1248
+
1249
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1250
+ "
1251
+ | Header1 | Header2 |
1252
+ |---|---|"
1253
+ `)
1254
+ })
1255
+
1256
+ test("streaming-like content with partial code block", async () => {
1257
+ const markdown = `# Title
1258
+
1259
+ Some text before code.
1260
+
1261
+ \`\`\`py`
1262
+
1263
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1264
+ "
1265
+ Title
1266
+
1267
+ Some text before code."
1268
+ `)
1269
+ })
1270
+
1271
+ test("malformed table with missing pipes", async () => {
1272
+ const markdown = `| A | B
1273
+ |---|---
1274
+ | 1 | 2`
1275
+
1276
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1277
+ "
1278
+ ┌─┬─┐
1279
+ │A│B│
1280
+ ├─┼─┤
1281
+ │1│2│
1282
+ └─┴─┘"
1283
+ `)
1284
+ })
1285
+
1286
+ test("trailing blank lines do not add spacing", async () => {
1287
+ const markdown = `# Heading
1288
+
1289
+ Paragraph text.
1290
+
1291
+
1292
+ `
1293
+
1294
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1295
+ "
1296
+ Heading
1297
+
1298
+ Paragraph text."
1299
+ `)
1300
+ })
1301
+
1302
+ test("multiple trailing blank lines do not add spacing", async () => {
1303
+ const markdown = `First paragraph.
1304
+
1305
+ Second paragraph.
1306
+
1307
+
1308
+
1309
+ `
1310
+
1311
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1312
+ "
1313
+ First paragraph.
1314
+
1315
+ Second paragraph."
1316
+ `)
1317
+ })
1318
+
1319
+ test("blank lines between blocks add spacing", async () => {
1320
+ const markdown = `First
1321
+
1322
+ Second
1323
+
1324
+ Third`
1325
+
1326
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1327
+ "
1328
+ First
1329
+
1330
+ Second
1331
+
1332
+ Third"
1333
+ `)
1334
+ })
1335
+
1336
+ test("code block at end with trailing blank lines", async () => {
1337
+ const markdown = `Text before
1338
+
1339
+ \`\`\`js
1340
+ const x = 1;
1341
+ \`\`\`
1342
+
1343
+ `
1344
+
1345
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1346
+ "
1347
+ Text before
1348
+ const x = 1;"
1349
+ `)
1350
+ })
1351
+
1352
+ test("table at end with trailing blank lines", async () => {
1353
+ const markdown = `| A | B |
1354
+ |---|---|
1355
+ | 1 | 2 |
1356
+
1357
+
1358
+ `
1359
+
1360
+ expect(await renderMarkdown(markdown)).toMatchInlineSnapshot(`
1361
+ "
1362
+ ┌─┬─┐
1363
+ │A│B│
1364
+ ├─┼─┤
1365
+ │1│2│
1366
+ └─┴─┘"
1367
+ `)
1368
+ })
1369
+
1370
+ // Incremental parsing tests
1371
+ test("incremental update reuses unchanged blocks when appending", async () => {
1372
+ const md = createMarkdownRenderable({
1373
+ id: "markdown",
1374
+ content: "# Hello\n\nParagraph 1",
1375
+ syntaxStyle,
1376
+ streaming: true,
1377
+ })
1378
+
1379
+ renderer.root.add(md)
1380
+ await renderer.idle()
1381
+
1382
+ // Get reference to first block
1383
+ const firstBlockBefore = md._blockStates[0]?.renderable
1384
+
1385
+ // Append content
1386
+ md.content = "# Hello\n\nParagraph 1\n\nParagraph 2"
1387
+ await renderer.idle()
1388
+
1389
+ // First block should be reused (same object reference)
1390
+ const firstBlockAfter = md._blockStates[0]?.renderable
1391
+ expect(firstBlockAfter).toBe(firstBlockBefore)
1392
+ })
1393
+
1394
+ test("streaming mode keeps trailing tokens unstable", async () => {
1395
+ const md = createMarkdownRenderable({
1396
+ id: "markdown",
1397
+ content: "# Hello",
1398
+ syntaxStyle,
1399
+ streaming: true,
1400
+ })
1401
+
1402
+ renderer.root.add(md)
1403
+ await renderMarkdownRenderable(md)
1404
+
1405
+ const frame1 = captureFrame()
1406
+ .split("\n")
1407
+ .map((line) => line.trimEnd())
1408
+ .join("\n")
1409
+ .trimEnd()
1410
+ expect(frame1).toContain("Hello")
1411
+
1412
+ // Extend the heading
1413
+ md.content = "# Hello World"
1414
+ await renderMarkdownRenderable(md)
1415
+
1416
+ const frame2 = captureFrame()
1417
+ .split("\n")
1418
+ .map((line) => line.trimEnd())
1419
+ .join("\n")
1420
+ .trimEnd()
1421
+ expect(frame2).toContain("Hello World")
1422
+ })
1423
+
1424
+ test("streaming code blocks with concealCode=true do not flash unconcealed markdown", async () => {
1425
+ const mockTreeSitterClient = new MockTreeSitterClient()
1426
+ mockTreeSitterClient.setMockResult({
1427
+ highlights: [[0, 1, "conceal", { conceal: "" }]],
1428
+ })
1429
+
1430
+ const recorder = new TestRecorder(renderer)
1431
+ recorder.rec()
1432
+
1433
+ const md = createMarkdownRenderable({
1434
+ id: "markdown-streaming-conceal-flicker",
1435
+ content: "# Stream\n\n```markdown\n# Hidden heading\n```",
1436
+ syntaxStyle,
1437
+ conceal: true,
1438
+ concealCode: true,
1439
+ streaming: true,
1440
+ treeSitterClient: mockTreeSitterClient,
1441
+ })
1442
+
1443
+ renderer.root.add(md)
1444
+ await renderer.idle()
1445
+
1446
+ expect(mockTreeSitterClient.isHighlighting()).toBe(true)
1447
+
1448
+ mockTreeSitterClient.resolveAllHighlightOnce()
1449
+ await Bun.sleep(10)
1450
+ await renderer.idle()
1451
+
1452
+ recorder.stop()
1453
+
1454
+ const frames = recorder.recordedFrames.map((frame) => frame.frame)
1455
+ const unconcealedFrames = frames.filter((frame) => frame.includes("# Hidden heading"))
1456
+ expect(unconcealedFrames.length).toBe(0)
1457
+ })
1458
+
1459
+ test("non-streaming mode parses all tokens as stable", async () => {
1460
+ const md = createMarkdownRenderable({
1461
+ id: "markdown",
1462
+ content: "# Hello\n\nPara 1\n\nPara 2",
1463
+ syntaxStyle,
1464
+ streaming: false,
1465
+ })
1466
+
1467
+ renderer.root.add(md)
1468
+ await renderer.idle()
1469
+
1470
+ // Get parse state
1471
+ const parseState = md._parseState
1472
+ expect(parseState).not.toBeNull()
1473
+ expect(parseState!.tokens.length).toBeGreaterThan(0)
1474
+ })
1475
+
1476
+ test("content update with same text does not rebuild", async () => {
1477
+ const md = createMarkdownRenderable({
1478
+ id: "markdown",
1479
+ content: "# Hello",
1480
+ syntaxStyle,
1481
+ })
1482
+
1483
+ renderer.root.add(md)
1484
+ await renderer.idle()
1485
+
1486
+ const blockBefore = md._blockStates[0]?.renderable
1487
+
1488
+ // Set same content
1489
+ md.content = "# Hello"
1490
+ await renderer.idle()
1491
+
1492
+ const blockAfter = md._blockStates[0]?.renderable
1493
+ expect(blockAfter).toBe(blockBefore)
1494
+ })
1495
+
1496
+ test("block type change creates new renderable", async () => {
1497
+ const md = createMarkdownRenderable({
1498
+ id: "markdown",
1499
+ content: "# Hello",
1500
+ syntaxStyle,
1501
+ })
1502
+
1503
+ renderer.root.add(md)
1504
+ await renderer.idle()
1505
+
1506
+ const blockBefore = md._blockStates[0]?.renderable
1507
+
1508
+ // Change from heading to paragraph
1509
+ md.content = "Hello"
1510
+ await renderer.idle()
1511
+
1512
+ const blockAfter = md._blockStates[0]?.renderable
1513
+ // Non-special markdown blocks are merged and reused as one markdown code renderable
1514
+ expect(blockAfter).toBe(blockBefore)
1515
+ })
1516
+
1517
+ test("streaming property can be toggled", async () => {
1518
+ const md = createMarkdownRenderable({
1519
+ id: "markdown",
1520
+ content: "# Hello",
1521
+ syntaxStyle,
1522
+ streaming: false,
1523
+ })
1524
+
1525
+ renderer.root.add(md)
1526
+ await renderMarkdownRenderable(md)
1527
+
1528
+ expect(md.streaming).toBe(false)
1529
+ const blockBefore = md._blockStates[0]?.renderable
1530
+
1531
+ md.streaming = true
1532
+ expect(md.streaming).toBe(true)
1533
+
1534
+ await renderMarkdownRenderable(md)
1535
+
1536
+ const blockAfter = md._blockStates[0]?.renderable
1537
+ expect(blockAfter).toBe(blockBefore)
1538
+
1539
+ const frame = captureFrame()
1540
+ .split("\n")
1541
+ .map((line) => line.trimEnd())
1542
+ .join("\n")
1543
+ .trimEnd()
1544
+ expect(frame).toContain("Hello")
1545
+ })
1546
+
1547
+ test("clearCache forces full rebuild", async () => {
1548
+ const md = createMarkdownRenderable({
1549
+ id: "markdown",
1550
+ content: "# Hello\n\nWorld",
1551
+ syntaxStyle,
1552
+ })
1553
+
1554
+ renderer.root.add(md)
1555
+ await renderer.idle()
1556
+
1557
+ const parseStateBefore = md._parseState
1558
+
1559
+ md.clearCache()
1560
+ await renderer.idle()
1561
+
1562
+ const parseStateAfter = md._parseState
1563
+ // Parse state should be different (was cleared and rebuilt)
1564
+ expect(parseStateAfter).not.toBe(parseStateBefore)
1565
+ })
1566
+
1567
+ test("streaming->non-streaming transition keeps final table row visible", async () => {
1568
+ const md = createMarkdownRenderable({
1569
+ id: "markdown",
1570
+ content: "| Value |\n|---|\n| first |\n| second |",
1571
+ syntaxStyle,
1572
+ streaming: true,
1573
+ })
1574
+
1575
+ renderer.root.add(md)
1576
+ await renderer.idle()
1577
+
1578
+ const tableWhileStreaming = md._blockStates[0]?.renderable
1579
+
1580
+ let frame = captureFrame()
1581
+ .split("\n")
1582
+ .map((line) => line.trimEnd())
1583
+ .join("\n")
1584
+
1585
+ expect(frame).toContain("first")
1586
+ expect(frame).toContain("second")
1587
+
1588
+ md.streaming = false
1589
+ await renderer.idle()
1590
+
1591
+ frame = captureFrame()
1592
+ .split("\n")
1593
+ .map((line) => line.trimEnd())
1594
+ .join("\n")
1595
+
1596
+ expect(frame).toContain("first")
1597
+ expect(frame).toContain("second")
1598
+ expect(md._blockStates[0]?.renderable).toBe(tableWhileStreaming)
1599
+ })
1600
+
1601
+ test("streaming table remains visible when a new block starts", async () => {
1602
+ const tableMarkdown = "| Value |\n|---|\n| first |\n| second |"
1603
+ const md = createMarkdownRenderable({
1604
+ id: "markdown",
1605
+ content: tableMarkdown,
1606
+ syntaxStyle,
1607
+ streaming: true,
1608
+ })
1609
+
1610
+ renderer.root.add(md)
1611
+ await renderer.idle()
1612
+
1613
+ const tableWhileTrailing = md._blockStates[0]?.renderable
1614
+
1615
+ let frame = captureFrame()
1616
+ .split("\n")
1617
+ .map((line) => line.trimEnd())
1618
+ .join("\n")
1619
+
1620
+ expect(frame).toContain("first")
1621
+ expect(frame).toContain("second")
1622
+
1623
+ md.content = `${tableMarkdown}\n\nAfter table block.`
1624
+ await renderer.idle()
1625
+
1626
+ frame = captureFrame()
1627
+ .split("\n")
1628
+ .map((line) => line.trimEnd())
1629
+ .join("\n")
1630
+
1631
+ expect(md.streaming).toBe(true)
1632
+ expect(frame).toContain("first")
1633
+ expect(frame).toContain("second")
1634
+ expect(md._blockStates.length).toBeGreaterThan(1)
1635
+ expect(md._blockStates[0]?.renderable).toBe(tableWhileTrailing)
1636
+ })
1637
+
1638
+ test("stream end mid-table finalizes full table snapshot", async () => {
1639
+ const md = createMarkdownRenderable({
1640
+ id: "markdown",
1641
+ content: "",
1642
+ syntaxStyle,
1643
+ streaming: true,
1644
+ })
1645
+
1646
+ renderer.root.add(md)
1647
+
1648
+ md.content = "| Name | Score |\n|---|---|\n"
1649
+ await renderer.idle()
1650
+
1651
+ md.content = "| Name | Score |\n|---|---|\n| Alpha | 10 |\n"
1652
+ await renderer.idle()
1653
+
1654
+ md.content = "| Name | Score |\n|---|---|\n| Alpha | 10 |\n| Bravo | 20 |\n"
1655
+ await renderer.idle()
1656
+
1657
+ md.content = "| Name | Score |\n|---|---|\n| Alpha | 10 |\n| Bravo | 20 |\n| Charlie | 30 |"
1658
+ await renderer.idle()
1659
+
1660
+ let frame = captureFrame()
1661
+ .split("\n")
1662
+ .map((line) => line.trimEnd())
1663
+ .join("\n")
1664
+
1665
+ expect(frame).toContain("Charlie")
1666
+
1667
+ md.streaming = false
1668
+ await renderer.idle()
1669
+
1670
+ frame = captureFrame()
1671
+ .split("\n")
1672
+ .map((line) => line.trimEnd())
1673
+ .join("\n")
1674
+ .trimEnd()
1675
+
1676
+ expect(frame).toMatchInlineSnapshot(`
1677
+ "┌──────────────────────────────┬───────────────────────────┐
1678
+ │Name │Score │
1679
+ ├──────────────────────────────┼───────────────────────────┤
1680
+ │Alpha │10 │
1681
+ ├──────────────────────────────┼───────────────────────────┤
1682
+ │Bravo │20 │
1683
+ ├──────────────────────────────┼───────────────────────────┤
1684
+ │Charlie │30 │
1685
+ └──────────────────────────────┴───────────────────────────┘"
1686
+ `)
1687
+ })
1688
+
1689
+ test("ignores content updates after markdown renderable is destroyed during streaming", async () => {
1690
+ const md = createMarkdownRenderable({
1691
+ id: "markdown",
1692
+ content: "",
1693
+ syntaxStyle,
1694
+ streaming: true,
1695
+ })
1696
+
1697
+ renderer.root.add(md)
1698
+
1699
+ md.content = "| Name | Score |\n|---|---|\n| Alpha | 10 |\n"
1700
+ await renderer.idle()
1701
+
1702
+ md.destroyRecursively()
1703
+ expect(md.isDestroyed).toBe(true)
1704
+
1705
+ expect(() => {
1706
+ md.content = "| Name | Score |\n|---|---|\n| Alpha | 10 |\n| Bravo | 20 |\n"
1707
+ md.streaming = false
1708
+ }).not.toThrow()
1709
+
1710
+ await renderer.idle()
1711
+ })
1712
+
1713
+ test("non-streaming->streaming transition keeps final table row visible", async () => {
1714
+ const md = createMarkdownRenderable({
1715
+ id: "markdown",
1716
+ content: "| Value |\n|---|\n| first |\n| second |",
1717
+ syntaxStyle,
1718
+ streaming: false,
1719
+ })
1720
+
1721
+ renderer.root.add(md)
1722
+ await renderer.idle()
1723
+
1724
+ const tableWhileStable = md._blockStates[0]?.renderable
1725
+
1726
+ let frame = captureFrame()
1727
+ .split("\n")
1728
+ .map((line) => line.trimEnd())
1729
+ .join("\n")
1730
+
1731
+ expect(frame).toContain("first")
1732
+ expect(frame).toContain("second")
1733
+
1734
+ md.streaming = true
1735
+ await renderer.idle()
1736
+
1737
+ frame = captureFrame()
1738
+ .split("\n")
1739
+ .map((line) => line.trimEnd())
1740
+ .join("\n")
1741
+
1742
+ expect(frame).toContain("first")
1743
+ expect(frame).toContain("second")
1744
+ expect(md._blockStates[0]?.renderable).toBe(tableWhileStable)
1745
+ })
1746
+
1747
+ test("streaming table reuses renderable while updating row content", async () => {
1748
+ const md = createMarkdownRenderable({
1749
+ id: "markdown",
1750
+ content: "| A |\n|---|\n| 1 |",
1751
+ syntaxStyle,
1752
+ streaming: true,
1753
+ })
1754
+
1755
+ renderer.root.add(md)
1756
+ await renderer.idle()
1757
+
1758
+ const tableBefore = md._blockStates[0]?.renderable
1759
+
1760
+ md.content = "| B |\n|---|\n| 2 |"
1761
+ await renderer.idle()
1762
+
1763
+ const tableAfterSameRows = md._blockStates[0]?.renderable
1764
+ expect(tableAfterSameRows).toBe(tableBefore)
1765
+
1766
+ md.content = "| B |\n|---|\n| 2 |\n| 3 |"
1767
+ await renderer.idle()
1768
+
1769
+ const tableAfterNewRow = md._blockStates[0]?.renderable
1770
+ expect(tableAfterNewRow).toBe(tableBefore)
1771
+ })
1772
+
1773
+ test("table shows all rows when streaming is false", async () => {
1774
+ const md = createMarkdownRenderable({
1775
+ id: "markdown",
1776
+ content: "| A |\n|---|\n| 1 |",
1777
+ syntaxStyle,
1778
+ streaming: false,
1779
+ })
1780
+
1781
+ renderer.root.add(md)
1782
+ await renderer.idle()
1783
+
1784
+ // Non-streaming should show all rows including the last
1785
+ const frame = captureFrame()
1786
+ .split("\n")
1787
+ .map((line) => line.trimEnd())
1788
+ .join("\n")
1789
+ expect(frame).toContain("1")
1790
+ })
1791
+
1792
+ test("table updates content when not streaming", async () => {
1793
+ const md = createMarkdownRenderable({
1794
+ id: "markdown",
1795
+ content: "| A |\n|---|\n| 1 |",
1796
+ syntaxStyle,
1797
+ streaming: false,
1798
+ })
1799
+
1800
+ renderer.root.add(md)
1801
+ await renderer.idle()
1802
+
1803
+ const frame1 = captureFrame()
1804
+ expect(frame1).toContain("1")
1805
+
1806
+ // Change cell content - should update immediately when not streaming
1807
+ md.content = "| A |\n|---|\n| 2 |"
1808
+ await renderer.idle()
1809
+
1810
+ const frame2 = captureFrame()
1811
+ expect(frame2).toContain("2")
1812
+ expect(frame2).not.toContain("1")
1813
+ })
1814
+
1815
+ test("table keeps unchanged cell chunks stable across updates", async () => {
1816
+ const md = createMarkdownRenderable({
1817
+ id: "markdown",
1818
+ content: "| A | B |\n|---|---|\n| 1 | 2 |\n| 3 | 4 |",
1819
+ syntaxStyle,
1820
+ streaming: false,
1821
+ })
1822
+
1823
+ renderer.root.add(md)
1824
+ await renderer.idle()
1825
+
1826
+ const table = md._blockStates[0]?.renderable as TextTableRenderable
1827
+ expect(table).toBeInstanceOf(TextTableRenderable)
1828
+
1829
+ const headerBefore = table.content[0]?.[0]
1830
+ const firstRowBefore = table.content[1]?.[0]
1831
+ const secondRowSecondCellBefore = table.content[2]?.[1]
1832
+ const changedCellBefore = table.content[2]?.[0]
1833
+
1834
+ md.content = "| A | B |\n|---|---|\n| 1 | 2 |\n| 33 | 4 |"
1835
+ await renderer.idle()
1836
+
1837
+ const tableAfter = md._blockStates[0]?.renderable as TextTableRenderable
1838
+ expect(tableAfter).toBe(table)
1839
+ expect(tableAfter.content[0]?.[0]).toBe(headerBefore)
1840
+ expect(tableAfter.content[1]?.[0]).toBe(firstRowBefore)
1841
+ expect(tableAfter.content[2]?.[1]).toBe(secondRowSecondCellBefore)
1842
+ expect(tableAfter.content[2]?.[0]).not.toBe(changedCellBefore)
1843
+ })
1844
+
1845
+ test("streaming table updates trailing row content", async () => {
1846
+ const md = createMarkdownRenderable({
1847
+ id: "markdown",
1848
+ content: "| A |\n|---|\n| 1 |\n| 2 |",
1849
+ syntaxStyle,
1850
+ streaming: true,
1851
+ })
1852
+
1853
+ renderer.root.add(md)
1854
+ await renderer.idle()
1855
+
1856
+ const table = md._blockStates[0]?.renderable as TextTableRenderable
1857
+ const contentBefore = table.content
1858
+
1859
+ md.content = "| A |\n|---|\n| 1 |\n| 200 |"
1860
+ await renderer.idle()
1861
+
1862
+ const tableAfter = md._blockStates[0]?.renderable as TextTableRenderable
1863
+ const frame = captureFrame()
1864
+ expect(tableAfter).toBe(table)
1865
+ expect(tableAfter.content).not.toBe(contentBefore)
1866
+ expect(frame).toContain("200")
1867
+ })
1868
+
1869
+ test("streaming complex tables keep final rows visible (issue #15244)", async () => {
1870
+ const vmHeader = "| VM | 状态 | Owner | Zone | CPU | Mem(GB) | Disk(GB) | Net | Uptime | Cost/月 | Notes |"
1871
+ const vmDelimiter = "|---|---|---|---|---|---|---|---|---|---|---|"
1872
+ const vmRows = [
1873
+ "| vm-api-01 | 🟢 运行中 | alice | us-east-1a | 8 | 32 | 500 | 1.2Gbps | 99.99% | 12,345 | 主节点 — steady |",
1874
+ "| vm-job-02 | 🟢 运行中 | bob | ap-south-1b | 16 | 64 | 1,024 | 950Mbps | 98.70% | 23,456 | 批处理 — spikes |",
1875
+ "| vm-batch-03 | 🟡 维护中 | carol | eu-west-1c | 32 | 128 | 2,048 | 2.4Gbps | 97.10% | 34,567 | 最后一行 — must stay |",
1876
+ ] as const
1877
+
1878
+ const storageHeader = "| 存储池 | 状态 | 使用率 | 可用(GB) | 已用(GB) | 冗余 | 备注 |"
1879
+ const storageDelimiter = "|---|---|---|---|---|---|---|"
1880
+ const storageRows = [
1881
+ "| 热池A | 🟢 正常 | 72% | 12,500 | 32,500 | 3x | 混合负载 |",
1882
+ "| 温池B | 🟢 正常 | 81% | 8,250 | 35,750 | 2x | 历史数据 |",
1883
+ "| 冷池C | 🟡 告警 | 93% | 2,100 | 27,900 | 2x | 最后一行 — must stay |",
1884
+ ] as const
1885
+
1886
+ const buildContent = (vmRowCount: number, storageRowCount: number): string =>
1887
+ `### VM details\n\n${vmHeader}\n${vmDelimiter}\n${vmRows.slice(0, vmRowCount).join("\n")}\n\n### Storage details\n\n${storageHeader}\n${storageDelimiter}\n${storageRows.slice(0, storageRowCount).join("\n")}`
1888
+
1889
+ const md = createMarkdownRenderable({
1890
+ id: "markdown",
1891
+ content: "",
1892
+ syntaxStyle,
1893
+ streaming: true,
1894
+ })
1895
+
1896
+ renderer.root.add(md)
1897
+
1898
+ for (const [vmRowCount, storageRowCount] of [
1899
+ [2, 2],
1900
+ [3, 2],
1901
+ [3, 3],
1902
+ ] as const) {
1903
+ md.content = buildContent(vmRowCount, storageRowCount)
1904
+ await renderMarkdownRenderable(md)
1905
+ }
1906
+
1907
+ const tableBlocks = md._blockStates
1908
+ .map((state) => state.renderable)
1909
+ .filter((renderable): renderable is TextTableRenderable => renderable instanceof TextTableRenderable)
1910
+
1911
+ const cellText = (cell: { text: string }[] | null | undefined): string =>
1912
+ cell?.map((chunk) => chunk.text).join("") ?? ""
1913
+
1914
+ expect(tableBlocks).toHaveLength(2)
1915
+
1916
+ const vmTable = tableBlocks[0]
1917
+ const storageTable = tableBlocks[1]
1918
+
1919
+ expect(vmTable.content.length).toBe(4)
1920
+ expect(storageTable.content.length).toBe(4)
1921
+ expect(cellText(vmTable.content[3]?.[0])).toContain("vm-batch-03")
1922
+ expect(cellText(storageTable.content[3]?.[0])).toContain("冷池C")
1923
+ })
1924
+
1925
+ test("streaming table with incomplete first row is rendered with padded cells", async () => {
1926
+ const md = createMarkdownRenderable({
1927
+ id: "markdown",
1928
+ content: "| A |\n|---|\n|",
1929
+ syntaxStyle,
1930
+ streaming: true,
1931
+ })
1932
+
1933
+ renderer.root.add(md)
1934
+ await renderMarkdownRenderable(md)
1935
+
1936
+ const frame1 = captureFrame()
1937
+ .split("\n")
1938
+ .map((line) => line.trimEnd())
1939
+ .join("\n")
1940
+
1941
+ expect(frame1).toMatch(/[┌│└]/)
1942
+ expect(frame1).toContain("A")
1943
+
1944
+ md.content = "| A |\n|---|\n| 1"
1945
+ await renderMarkdownRenderable(md)
1946
+
1947
+ const frame2 = captureFrame()
1948
+ .split("\n")
1949
+ .map((line) => line.trimEnd())
1950
+ .join("\n")
1951
+
1952
+ expect(frame2).toMatch(/[┌│└]/)
1953
+ expect(frame2).toContain("1")
1954
+
1955
+ md.content = "| A |\n|---|\n| 1 |\n| 2 |"
1956
+ await renderMarkdownRenderable(md)
1957
+
1958
+ const frame3 = captureFrame()
1959
+ .split("\n")
1960
+ .map((line) => line.trimEnd())
1961
+ .join("\n")
1962
+
1963
+ expect(frame3).toMatch(/[┌│└]/)
1964
+ expect(frame3).toContain("1")
1965
+ expect(frame3).toContain("2")
1966
+ })
1967
+
1968
+ test("streaming table transitions from raw text to table once first row appears", async () => {
1969
+ const md = createMarkdownRenderable({
1970
+ id: "markdown",
1971
+ content: "| Header |",
1972
+ syntaxStyle,
1973
+ streaming: true,
1974
+ })
1975
+
1976
+ renderer.root.add(md)
1977
+ await renderMarkdownRenderable(md)
1978
+
1979
+ let frame = captureFrame()
1980
+ .split("\n")
1981
+ .map((line) => line.trimEnd())
1982
+ .join("\n")
1983
+ expect(frame).toContain("| Header |")
1984
+ expect(frame).not.toMatch(/[┌│└]/)
1985
+
1986
+ md.content = "| Header |\n|---|"
1987
+ await renderMarkdownRenderable(md)
1988
+
1989
+ frame = captureFrame()
1990
+ .split("\n")
1991
+ .map((line) => line.trimEnd())
1992
+ .join("\n")
1993
+ expect(frame).toContain("|---|")
1994
+ expect(frame).not.toMatch(/[┌│└]/)
1995
+
1996
+ md.content = "| Header |\n|---|\n| D"
1997
+ await renderMarkdownRenderable(md)
1998
+
1999
+ frame = captureFrame()
2000
+ .split("\n")
2001
+ .map((line) => line.trimEnd())
2002
+ .join("\n")
2003
+ expect(frame).toMatch(/[┌│└]/)
2004
+ expect(frame).toContain("Header")
2005
+ expect(frame).toContain("D")
2006
+ expect(frame).not.toContain("|---|")
2007
+ })
2008
+
2009
+ test("streaming table remains rendered when row count decreases", async () => {
2010
+ const md = createMarkdownRenderable({
2011
+ id: "markdown",
2012
+ content: "| A |\n|---|\n| 1 |\n| 2 |",
2013
+ syntaxStyle,
2014
+ streaming: true,
2015
+ })
2016
+
2017
+ renderer.root.add(md)
2018
+ await renderMarkdownRenderable(md)
2019
+
2020
+ let frame = captureFrame()
2021
+ .split("\n")
2022
+ .map((line) => line.trimEnd())
2023
+ .join("\n")
2024
+ expect(frame).toMatch(/[┌│└]/)
2025
+ expect(frame).toContain("1")
2026
+ expect(frame).toContain("2")
2027
+
2028
+ md.content = "| A |\n|---|\n| 1 |"
2029
+ await renderMarkdownRenderable(md)
2030
+
2031
+ frame = captureFrame()
2032
+ .split("\n")
2033
+ .map((line) => line.trimEnd())
2034
+ .join("\n")
2035
+ expect(frame).toMatch(/[┌│└]/)
2036
+ expect(frame).toContain("1")
2037
+ expect(frame).not.toContain("|---|")
2038
+ })
2039
+
2040
+ test("conceal change updates rendered content", async () => {
2041
+ const md = createMarkdownRenderable({
2042
+ id: "markdown",
2043
+ content: "# Hello **bold**",
2044
+ syntaxStyle,
2045
+ conceal: true,
2046
+ })
2047
+
2048
+ renderer.root.add(md)
2049
+ await renderMarkdownRenderable(md)
2050
+
2051
+ const frame1 = captureFrame()
2052
+ expect(frame1).not.toContain("**")
2053
+ expect(frame1).not.toContain("#")
2054
+
2055
+ md.conceal = false
2056
+ renderer.requestRender()
2057
+ await renderMarkdownRenderable(md)
2058
+
2059
+ const frame2 = captureFrame()
2060
+ expect(frame2).toContain("**")
2061
+ expect(frame2).toContain("#")
2062
+ })
2063
+
2064
+ test("theme switching (syntaxStyle change)", async () => {
2065
+ const theme1 = SyntaxStyle.fromStyles({
2066
+ default: { fg: RGBA.fromValues(1, 0, 0, 1) }, // Red
2067
+ "markup.heading.1": { fg: RGBA.fromValues(0, 1, 0, 1), bold: true }, // Green
2068
+ })
2069
+
2070
+ const theme2 = SyntaxStyle.fromStyles({
2071
+ default: { fg: RGBA.fromValues(0, 0, 1, 1) }, // Blue
2072
+ "markup.heading.1": { fg: RGBA.fromValues(1, 1, 0, 1), bold: true }, // Yellow
2073
+ })
2074
+
2075
+ // Use the EXACT content from markdown-demo.ts to reproduce the issue
2076
+ const content = `# OpenTUI Markdown Demo
2077
+
2078
+ Welcome to the **MarkdownRenderable** showcase! This demonstrates automatic table alignment and syntax highlighting.
2079
+
2080
+ ## Features
2081
+
2082
+ - Automatic **table column alignment** based on content width
2083
+ - Proper handling of \`inline code\`, **bold**, and *italic* in tables
2084
+ - Multiple syntax themes to choose from
2085
+ - Conceal mode hides formatting markers
2086
+
2087
+ ## Comparison Table
2088
+
2089
+ | Feature | Status | Priority | Notes |
2090
+ |---|---|---|---|
2091
+ | Table alignment | **Done** | High | Uses \`marked\` parser |
2092
+ | Conceal mode | *Working* | Medium | Hides \`**\`, \`\`\`, etc. |
2093
+ | Theme switching | **Done** | Low | 3 themes available |
2094
+ | Unicode support | 日本語 | High | CJK characters |
2095
+
2096
+ ## Code Examples
2097
+
2098
+ Here's how to use it:
2099
+
2100
+ \`\`\`typescript
2101
+ import { MarkdownRenderable } from "@opentui/core"
2102
+
2103
+ const md = createMarkdownRenderable({
2104
+ content: "# Hello World",
2105
+ syntaxStyle: mySyntaxStyle,
2106
+ conceal: true, // Hide formatting markers
2107
+ })
2108
+ \`\`\`
2109
+
2110
+ ### API Reference
2111
+
2112
+ | Method | Parameters | Returns | Description |
2113
+ |---|---|---|---|
2114
+ | \`constructor\` | \`ctx, options\` | \`MarkdownRenderable\` | Create new instance |
2115
+ | \`clearCache\` | none | \`void\` | Force re-render content |
2116
+
2117
+ ## Inline Formatting Examples
2118
+
2119
+ | Style | Syntax | Rendered |
2120
+ |---|---|---|
2121
+ | Bold | \`**text**\` | **bold text** |
2122
+ | Italic | \`*text*\` | *italic text* |
2123
+ | Code | \`code\` | \`inline code\` |
2124
+ | Link | \`[text](url)\` | [OpenTUI](https://github.com) |
2125
+
2126
+ ## Mixed Content
2127
+
2128
+ > **Note**: This blockquote contains **bold** and \`code\` formatting.
2129
+ > It should render correctly with proper styling.
2130
+
2131
+ ### Emoji Support
2132
+
2133
+ | Emoji | Name | Category |
2134
+ |---|---|---|
2135
+ | 🚀 | Rocket | Transport |
2136
+ | 🎨 | Palette | Art |
2137
+ | ⚡ | Lightning | Nature |
2138
+ | 🔥 | Fire | Nature |
2139
+
2140
+ ---
2141
+
2142
+ ## Alignment Examples
2143
+
2144
+ | Left | Center | Right |
2145
+ |:---|:---:|---:|
2146
+ | L1 | C1 | R1 |
2147
+ | Left aligned | Centered text | Right aligned |
2148
+ | Short | Medium length | Longer content here |
2149
+
2150
+ ## Performance
2151
+
2152
+ The table alignment uses:
2153
+ 1. AST-based parsing with \`marked\`
2154
+ 2. Caching for repeated content
2155
+ 3. Smart width calculation accounting for concealed chars
2156
+
2157
+ ---
2158
+
2159
+ *Press \`?\` for keybindings*
2160
+ `
2161
+
2162
+ const md = createMarkdownRenderable({
2163
+ id: "markdown",
2164
+ content,
2165
+ syntaxStyle: theme1,
2166
+ conceal: true,
2167
+ })
2168
+
2169
+ renderer.root.add(md)
2170
+ await renderMarkdownRenderable(md)
2171
+
2172
+ const findSpanContaining = (frame: CapturedFrame, text: string) => {
2173
+ for (const line of frame.lines) {
2174
+ const span = line.spans.find((candidate) => candidate.text.includes(text))
2175
+ if (span) return span
2176
+ }
2177
+ return undefined
2178
+ }
2179
+
2180
+ const frame1 = captureSpans()
2181
+ const headingSpan1 = findSpanContaining(frame1, "OpenTUI Markdown Demo")
2182
+ expect(headingSpan1).toBeDefined()
2183
+ expect(headingSpan1!.fg.r).toBe(0)
2184
+ expect(headingSpan1!.fg.g).toBe(1)
2185
+ expect(headingSpan1!.fg.b).toBe(0)
2186
+ expect(headingSpan1!.attributes & TextAttributes.BOLD).toBeTruthy()
2187
+
2188
+ // Switch theme
2189
+ md.syntaxStyle = theme2
2190
+ renderer.requestRender()
2191
+ await renderMarkdownRenderable(md)
2192
+
2193
+ const frame2 = captureSpans()
2194
+ const headingSpan2 = findSpanContaining(frame2, "OpenTUI Markdown Demo")
2195
+ expect(headingSpan2).toBeDefined()
2196
+ expect(headingSpan2!.fg.r).toBe(1)
2197
+ expect(headingSpan2!.fg.g).toBe(1)
2198
+ expect(headingSpan2!.fg.b).toBe(0)
2199
+ expect(headingSpan2!.attributes & TextAttributes.BOLD).toBeTruthy()
2200
+ })
2201
+
2202
+ // Paragraph rendering tests
2203
+
2204
+ test("paragraph links are rendered with markdown conceal behavior", async () => {
2205
+ const md = createMarkdownRenderable({
2206
+ id: "markdown",
2207
+ content: "Check [Google](https://google.com) out",
2208
+ syntaxStyle,
2209
+ conceal: true,
2210
+ })
2211
+
2212
+ renderer.root.add(md)
2213
+ await renderMarkdownRenderable(md)
2214
+
2215
+ const paragraphChildren = md.getChildren()
2216
+ expect(paragraphChildren.length).toBe(1)
2217
+ expect(paragraphChildren[0]).toBeInstanceOf(CodeRenderable)
2218
+ expect(paragraphChildren[0]).not.toBeInstanceOf(TextRenderable)
2219
+
2220
+ const frame = captureFrame()
2221
+ expect(frame).toContain("Google")
2222
+ expect(frame).toContain("https://google.com")
2223
+ expect(frame).not.toContain("[Google](https://google.com)")
2224
+ })
2225
+
2226
+ test("paragraph initial render does not flash raw markdown markers", async () => {
2227
+ const recorder = new TestRecorder(renderer)
2228
+ recorder.rec()
2229
+
2230
+ const md = createMarkdownRenderable({
2231
+ id: "markdown",
2232
+ content: "This has **bold** text.",
2233
+ syntaxStyle,
2234
+ conceal: true,
2235
+ })
2236
+
2237
+ renderer.root.add(md)
2238
+ await renderMarkdownRenderable(md)
2239
+ recorder.stop()
2240
+
2241
+ const paragraphChildren = md.getChildren()
2242
+ expect(paragraphChildren.length).toBe(1)
2243
+ expect(paragraphChildren[0]).toBeInstanceOf(CodeRenderable)
2244
+ expect(paragraphChildren[0]).not.toBeInstanceOf(TextRenderable)
2245
+
2246
+ const rawMarkdownFrames = recorder.recordedFrames.filter((recorded) => recorded.frame.includes("**bold**"))
2247
+ expect(rawMarkdownFrames.length).toBe(0)
2248
+
2249
+ const finalFrame = captureFrame()
2250
+ expect(finalFrame).toContain("This has bold text.")
2251
+ })
2252
+
2253
+ test("paragraph updates do not flash raw markdown markers", async () => {
2254
+ const md = createMarkdownRenderable({
2255
+ id: "markdown",
2256
+ content: "**First** value",
2257
+ syntaxStyle,
2258
+ conceal: true,
2259
+ })
2260
+
2261
+ renderer.root.add(md)
2262
+ await renderMarkdownRenderable(md)
2263
+
2264
+ const paragraphChildrenBefore = md.getChildren()
2265
+ expect(paragraphChildrenBefore.length).toBe(1)
2266
+ expect(paragraphChildrenBefore[0]).toBeInstanceOf(CodeRenderable)
2267
+ expect(paragraphChildrenBefore[0]).not.toBeInstanceOf(TextRenderable)
2268
+
2269
+ const recorder = new TestRecorder(renderer)
2270
+ recorder.rec()
2271
+
2272
+ md.content = "**Second** value"
2273
+ await renderMarkdownRenderable(md)
2274
+ recorder.stop()
2275
+
2276
+ const paragraphChildrenAfter = md.getChildren()
2277
+ expect(paragraphChildrenAfter.length).toBe(1)
2278
+ expect(paragraphChildrenAfter[0]).toBeInstanceOf(CodeRenderable)
2279
+ expect(paragraphChildrenAfter[0]).not.toBeInstanceOf(TextRenderable)
2280
+
2281
+ const rawMarkdownFrames = recorder.recordedFrames.filter((recorded) => recorded.frame.includes("**Second**"))
2282
+ expect(rawMarkdownFrames.length).toBe(0)
2283
+
2284
+ const finalFrame = captureFrame()
2285
+ expect(finalFrame).toContain("Second value")
2286
+ expect(finalFrame).not.toContain("**Second**")
2287
+ })