@deepseek-ai/dsh-client-ui-primitives 0.2.2-alpha.3 → 0.2.2-rc.2

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 (412) hide show
  1. package/README.i18n.yaml +16 -16
  2. package/README.md +21 -14
  3. package/README.zh.md +21 -14
  4. package/lib/HoverCard.module.css +8 -3
  5. package/lib/JsonTree.module.css +7 -17
  6. package/lib/Menu.module.css +157 -37
  7. package/lib/Modal.module.css +16 -0
  8. package/lib/QuotePreview.module.css +37 -0
  9. package/lib/RiskConfirmation.module.css +11 -37
  10. package/lib/Toast.module.css +2 -3
  11. package/lib/Tooltip.module.css +2 -1
  12. package/lib/index.js +1447 -776
  13. package/lib/index.js.map +1 -0
  14. package/lib/markdown/CodeBlock.module.css +64 -0
  15. package/lib/markdown/MarkdownText.module.css +20 -1
  16. package/lib/types/AnimatedRows.d.ts +3 -0
  17. package/lib/types/AnimatedRows.d.ts.map +1 -0
  18. package/lib/types/AnimatedRows.js.map +1 -0
  19. package/lib/types/BrandWordmark.d.ts.map +1 -0
  20. package/lib/types/BrandWordmark.js.map +1 -0
  21. package/lib/types/Button.d.ts.map +1 -0
  22. package/lib/types/Button.js.map +1 -0
  23. package/lib/types/Checkbox.d.ts +3 -1
  24. package/lib/types/Checkbox.d.ts.map +1 -0
  25. package/lib/types/Checkbox.js.map +1 -0
  26. package/lib/types/CodeFileIcon.d.ts.map +1 -0
  27. package/lib/types/CodeFileIcon.js.map +1 -0
  28. package/lib/types/CodeToolbar.d.ts.map +1 -0
  29. package/lib/types/CodeToolbar.js.map +1 -0
  30. package/lib/types/CommandText.d.ts.map +1 -0
  31. package/lib/types/CommandText.js.map +1 -0
  32. package/lib/types/ConfigField.d.ts.map +1 -0
  33. package/lib/types/ConfigField.js.map +1 -0
  34. package/lib/types/ConnectionIndicator.d.ts.map +1 -0
  35. package/lib/types/ConnectionIndicator.js.map +1 -0
  36. package/lib/types/DetailDocument.d.ts.map +1 -0
  37. package/lib/types/DetailDocument.js.map +1 -0
  38. package/lib/types/DiffBlock.d.ts.map +1 -0
  39. package/lib/types/DiffBlock.js.map +1 -0
  40. package/lib/types/DisclosureRow.d.ts.map +1 -0
  41. package/lib/types/DisclosureRow.js.map +1 -0
  42. package/lib/types/DraggableImage.d.ts.map +1 -0
  43. package/lib/types/DraggableImage.js.map +1 -0
  44. package/lib/types/FileTypeIcon.d.ts.map +1 -0
  45. package/lib/types/FileTypeIcon.js.map +1 -0
  46. package/lib/types/FishLogo.d.ts.map +1 -0
  47. package/lib/types/FishLogo.js.map +1 -0
  48. package/lib/types/FoldToggle.d.ts.map +1 -0
  49. package/lib/types/FoldToggle.js.map +1 -0
  50. package/lib/types/HoverCard.d.ts.map +1 -0
  51. package/lib/types/HoverCard.js.map +1 -0
  52. package/lib/types/HoverCardGroup.d.ts.map +1 -0
  53. package/lib/types/HoverCardGroup.js.map +1 -0
  54. package/lib/types/ImageLightbox.d.ts.map +1 -0
  55. package/lib/types/ImageLightbox.js.map +1 -0
  56. package/lib/types/ImagePreview.d.ts.map +1 -0
  57. package/lib/types/ImagePreview.js.map +1 -0
  58. package/lib/types/InlineEditor.d.ts.map +1 -0
  59. package/lib/types/InlineEditor.js.map +1 -0
  60. package/lib/types/Input.d.ts.map +1 -0
  61. package/lib/types/Input.js.map +1 -0
  62. package/lib/types/JsonTree.d.ts.map +1 -0
  63. package/lib/types/JsonTree.js.map +1 -0
  64. package/lib/types/LinkIcon.d.ts.map +1 -0
  65. package/lib/types/LinkIcon.js.map +1 -0
  66. package/lib/types/Menu.d.ts +26 -14
  67. package/lib/types/Menu.d.ts.map +1 -0
  68. package/lib/types/Menu.js.map +1 -0
  69. package/lib/types/MenuGroup.d.ts.map +1 -0
  70. package/lib/types/MenuGroup.js.map +1 -0
  71. package/lib/types/MenuRow.d.ts +37 -0
  72. package/lib/types/MenuRow.d.ts.map +1 -0
  73. package/lib/types/MenuRow.js.map +1 -0
  74. package/lib/types/MenuSurface.d.ts.map +1 -0
  75. package/lib/types/MenuSurface.js.map +1 -0
  76. package/lib/types/Modal.d.ts +3 -1
  77. package/lib/types/Modal.d.ts.map +1 -0
  78. package/lib/types/Modal.js.map +1 -0
  79. package/lib/types/PathLabel.d.ts.map +1 -0
  80. package/lib/types/PathLabel.js.map +1 -0
  81. package/lib/types/PermissionIcon.d.ts.map +1 -0
  82. package/lib/types/PermissionIcon.js.map +1 -0
  83. package/lib/types/Pill.d.ts.map +1 -0
  84. package/lib/types/Pill.js.map +1 -0
  85. package/lib/types/QuotePreview.d.ts +17 -4
  86. package/lib/types/QuotePreview.d.ts.map +1 -0
  87. package/lib/types/QuotePreview.js.map +1 -0
  88. package/lib/types/ReadBlock.d.ts.map +1 -0
  89. package/lib/types/ReadBlock.js.map +1 -0
  90. package/lib/types/ReferenceIcon.d.ts.map +1 -0
  91. package/lib/types/ReferenceIcon.js.map +1 -0
  92. package/lib/types/RiskConfirmation.d.ts.map +1 -0
  93. package/lib/types/RiskConfirmation.js.map +1 -0
  94. package/lib/types/SearchBlock.d.ts.map +1 -0
  95. package/lib/types/SearchBlock.js.map +1 -0
  96. package/lib/types/SegmentedControl.d.ts.map +1 -0
  97. package/lib/types/SegmentedControl.js.map +1 -0
  98. package/lib/types/SegmentedTabs.d.ts.map +1 -0
  99. package/lib/types/SegmentedTabs.js.map +1 -0
  100. package/lib/types/ShortcutKeys.d.ts.map +1 -0
  101. package/lib/types/ShortcutKeys.js.map +1 -0
  102. package/lib/types/SiteGlyph.d.ts.map +1 -0
  103. package/lib/types/SiteGlyph.js.map +1 -0
  104. package/lib/types/StateDot.d.ts.map +1 -0
  105. package/lib/types/StateDot.js.map +1 -0
  106. package/lib/types/SubmenuPanel.d.ts +19 -0
  107. package/lib/types/SubmenuPanel.d.ts.map +1 -0
  108. package/lib/types/SubmenuPanel.js.map +1 -0
  109. package/lib/types/Switch.d.ts.map +1 -0
  110. package/lib/types/Switch.js.map +1 -0
  111. package/lib/types/Tag.d.ts.map +1 -0
  112. package/lib/types/Tag.js.map +1 -0
  113. package/lib/types/TerminalBlock.d.ts.map +1 -0
  114. package/lib/types/TerminalBlock.js.map +1 -0
  115. package/lib/types/TextShimmer.d.ts.map +1 -0
  116. package/lib/types/TextShimmer.js.map +1 -0
  117. package/lib/types/Toast.d.ts.map +1 -0
  118. package/lib/types/Toast.js.map +1 -0
  119. package/lib/types/Tooltip.d.ts.map +1 -0
  120. package/lib/types/Tooltip.js.map +1 -0
  121. package/lib/types/UserMessageBubble.d.ts.map +1 -0
  122. package/lib/types/UserMessageBubble.js.map +1 -0
  123. package/lib/types/WebBlock.d.ts.map +1 -0
  124. package/lib/types/WebBlock.js.map +1 -0
  125. package/lib/types/ZoomControls.d.ts.map +1 -0
  126. package/lib/types/ZoomControls.js.map +1 -0
  127. package/lib/types/ansi.d.ts.map +1 -0
  128. package/lib/types/ansi.js.map +1 -0
  129. package/lib/types/clipboard.d.ts.map +1 -0
  130. package/lib/types/clipboard.js.map +1 -0
  131. package/lib/types/code-file-icon-artwork.d.ts.map +1 -0
  132. package/lib/types/code-file-icon-artwork.js.map +1 -0
  133. package/lib/types/code-file-types.d.ts.map +1 -0
  134. package/lib/types/code-file-types.js.map +1 -0
  135. package/lib/types/code-highlighting.d.ts.map +1 -0
  136. package/lib/types/code-highlighting.js.map +1 -0
  137. package/lib/types/darwin-desktop.d.ts.map +1 -0
  138. package/lib/types/darwin-desktop.js.map +1 -0
  139. package/lib/types/document-zoom.d.ts.map +1 -0
  140. package/lib/types/document-zoom.js.map +1 -0
  141. package/lib/types/file-drag-actions.d.ts.map +1 -0
  142. package/lib/types/file-drag-actions.js.map +1 -0
  143. package/lib/types/file-size.d.ts.map +1 -0
  144. package/lib/types/file-size.js.map +1 -0
  145. package/lib/types/focus.d.ts.map +1 -0
  146. package/lib/types/focus.js.map +1 -0
  147. package/lib/types/guide-artwork.d.ts.map +1 -0
  148. package/lib/types/guide-artwork.js.map +1 -0
  149. package/lib/types/head-tail-cap.d.ts.map +1 -0
  150. package/lib/types/head-tail-cap.js.map +1 -0
  151. package/lib/types/icons/artwork.d.ts +12 -0
  152. package/lib/types/icons/artwork.d.ts.map +1 -0
  153. package/lib/types/icons/artwork.js.map +1 -0
  154. package/lib/types/icons/index.d.ts +12 -0
  155. package/lib/types/icons/index.d.ts.map +1 -0
  156. package/lib/types/icons/index.js.map +1 -0
  157. package/lib/types/icons/props.d.ts.map +1 -0
  158. package/lib/types/icons/props.js.map +1 -0
  159. package/lib/types/icons/shared-artwork.d.ts.map +1 -0
  160. package/lib/types/icons/shared-artwork.js.map +1 -0
  161. package/lib/types/image-drag.d.ts.map +1 -0
  162. package/lib/types/image-drag.js.map +1 -0
  163. package/lib/types/index.d.ts +5 -4
  164. package/lib/types/index.d.ts.map +1 -0
  165. package/lib/types/index.js.map +1 -0
  166. package/lib/types/input-modality.d.ts.map +1 -0
  167. package/lib/types/input-modality.js.map +1 -0
  168. package/lib/types/keyboard-composition.d.ts.map +1 -0
  169. package/lib/types/keyboard-composition.js.map +1 -0
  170. package/lib/types/markdown/CodeBlock.d.ts +8 -1
  171. package/lib/types/markdown/CodeBlock.d.ts.map +1 -0
  172. package/lib/types/markdown/CodeBlock.js.map +1 -0
  173. package/lib/types/markdown/FenceBlocks.d.ts +31 -0
  174. package/lib/types/markdown/FenceBlocks.d.ts.map +1 -0
  175. package/lib/types/markdown/FenceBlocks.js.map +1 -0
  176. package/lib/types/markdown/JsonBlock.d.ts.map +1 -0
  177. package/lib/types/markdown/JsonBlock.js.map +1 -0
  178. package/lib/types/markdown/MarkdownDelegate.d.ts +23 -1
  179. package/lib/types/markdown/MarkdownDelegate.d.ts.map +1 -0
  180. package/lib/types/markdown/MarkdownDelegate.js.map +1 -0
  181. package/lib/types/markdown/MarkdownText.d.ts +2 -0
  182. package/lib/types/markdown/MarkdownText.d.ts.map +1 -0
  183. package/lib/types/markdown/MarkdownText.js.map +1 -0
  184. package/lib/types/markdown/cjkFriendlyStrong.d.ts.map +1 -0
  185. package/lib/types/markdown/cjkFriendlyStrong.js.map +1 -0
  186. package/lib/types/markdown/copy-tex.d.ts +14 -0
  187. package/lib/types/markdown/copy-tex.d.ts.map +1 -0
  188. package/lib/types/markdown/copy-tex.js.map +1 -0
  189. package/lib/types/markdown/diff-fence.d.ts +58 -0
  190. package/lib/types/markdown/diff-fence.d.ts.map +1 -0
  191. package/lib/types/markdown/diff-fence.js.map +1 -0
  192. package/lib/types/markdown/file-link.d.ts.map +1 -0
  193. package/lib/types/markdown/file-link.js.map +1 -0
  194. package/lib/types/markdown/highlight.d.ts +18 -0
  195. package/lib/types/markdown/highlight.d.ts.map +1 -0
  196. package/lib/types/markdown/highlight.js.map +1 -0
  197. package/lib/types/markdown/incremental.d.ts.map +1 -0
  198. package/lib/types/markdown/incremental.js.map +1 -0
  199. package/lib/types/markdown/katex.d.ts.map +1 -0
  200. package/lib/types/markdown/katex.js.map +1 -0
  201. package/lib/types/markdown/local-image-syntax.d.ts.map +1 -0
  202. package/lib/types/markdown/local-image-syntax.js.map +1 -0
  203. package/lib/types/markdown/mathCompatibility.d.ts.map +1 -0
  204. package/lib/types/markdown/mathCompatibility.js.map +1 -0
  205. package/lib/types/markdown/parse.d.ts.map +1 -0
  206. package/lib/types/markdown/parse.js.map +1 -0
  207. package/lib/types/markdown/plain-text.d.ts.map +1 -0
  208. package/lib/types/markdown/plain-text.js.map +1 -0
  209. package/lib/types/markdown/quote-text.d.ts.map +1 -0
  210. package/lib/types/markdown/quote-text.js.map +1 -0
  211. package/lib/types/markdown/render.d.ts.map +1 -0
  212. package/lib/types/markdown/render.js.map +1 -0
  213. package/lib/types/markdown/useViewportHighlighting.d.ts.map +1 -0
  214. package/lib/types/markdown/useViewportHighlighting.js.map +1 -0
  215. package/lib/types/markdown/windows-path-destinations.d.ts.map +1 -0
  216. package/lib/types/markdown/windows-path-destinations.js.map +1 -0
  217. package/lib/types/menu-submenu-position.d.ts +15 -0
  218. package/lib/types/menu-submenu-position.d.ts.map +1 -0
  219. package/lib/types/menu-submenu-position.js.map +1 -0
  220. package/lib/types/overlay-top-margin.d.ts.map +1 -0
  221. package/lib/types/overlay-top-margin.js.map +1 -0
  222. package/lib/types/plugin-artwork.d.ts.map +1 -0
  223. package/lib/types/plugin-artwork.js.map +1 -0
  224. package/lib/types/pointer-grace.d.ts.map +1 -0
  225. package/lib/types/pointer-grace.js.map +1 -0
  226. package/lib/types/rank-by-name.d.ts.map +1 -0
  227. package/lib/types/rank-by-name.js.map +1 -0
  228. package/lib/types/relative-time.d.ts.map +1 -0
  229. package/lib/types/relative-time.js.map +1 -0
  230. package/lib/types/settings-form/SettingsForm.d.ts.map +1 -0
  231. package/lib/types/settings-form/SettingsForm.js.map +1 -0
  232. package/lib/types/settings-form/fields.d.ts.map +1 -0
  233. package/lib/types/settings-form/fields.js.map +1 -0
  234. package/lib/types/settings-form/form-model.d.ts.map +1 -0
  235. package/lib/types/settings-form/form-model.js.map +1 -0
  236. package/lib/types/tooltip-placement.d.ts.map +1 -0
  237. package/lib/types/tooltip-placement.js.map +1 -0
  238. package/lib/types/use-clipboard-copy.d.ts.map +1 -0
  239. package/lib/types/use-clipboard-copy.js.map +1 -0
  240. package/lib/types/useAnchoredMaxHeight.d.ts.map +1 -0
  241. package/lib/types/useAnchoredMaxHeight.js.map +1 -0
  242. package/lib/types/useDismissOnOutsidePointer.d.ts.map +1 -0
  243. package/lib/types/useDismissOnOutsidePointer.js.map +1 -0
  244. package/lib/types/useMenuSubmenu.d.ts +43 -0
  245. package/lib/types/useMenuSubmenu.d.ts.map +1 -0
  246. package/lib/types/useMenuSubmenu.js.map +1 -0
  247. package/lib/types/useModalLayer.d.ts.map +1 -0
  248. package/lib/types/useModalLayer.js.map +1 -0
  249. package/lib/types/useRowDragPreview.d.ts.map +1 -0
  250. package/lib/types/useRowDragPreview.js.map +1 -0
  251. package/lib/types/user-text.d.ts.map +1 -0
  252. package/lib/types/user-text.js.map +1 -0
  253. package/package.json +11 -8
  254. package/src/AnimatedRows.module.css +7 -0
  255. package/src/AnimatedRows.tsx +195 -0
  256. package/src/BrandWordmark.tsx +59 -0
  257. package/src/Button.module.css +84 -0
  258. package/src/Button.tsx +36 -0
  259. package/src/Checkbox.module.css +28 -0
  260. package/src/Checkbox.tsx +34 -0
  261. package/src/CodeCard.module.css +85 -0
  262. package/src/CodeFileIcon.tsx +28 -0
  263. package/src/CodeToolbar.tsx +62 -0
  264. package/src/CommandText.module.css +12 -0
  265. package/src/CommandText.tsx +46 -0
  266. package/src/ConfigField.module.css +12 -0
  267. package/src/ConfigField.tsx +53 -0
  268. package/src/ConnectionIndicator.module.css +120 -0
  269. package/src/ConnectionIndicator.tsx +103 -0
  270. package/src/DetailDocument.module.css +67 -0
  271. package/src/DetailDocument.tsx +120 -0
  272. package/src/DiffBlock.module.css +82 -0
  273. package/src/DiffBlock.tsx +221 -0
  274. package/src/DisclosureRow.module.css +115 -0
  275. package/src/DisclosureRow.tsx +124 -0
  276. package/src/DraggableImage.tsx +26 -0
  277. package/src/FileTypeIcon.module.css +50 -0
  278. package/src/FileTypeIcon.tsx +285 -0
  279. package/src/FishLogo.tsx +28 -0
  280. package/src/FoldToggle.tsx +33 -0
  281. package/src/HoverCard.module.css +108 -0
  282. package/src/HoverCard.tsx +356 -0
  283. package/src/HoverCardGroup.tsx +77 -0
  284. package/src/ImageLightbox.module.css +54 -0
  285. package/src/ImageLightbox.tsx +72 -0
  286. package/src/ImagePreview.module.css +24 -0
  287. package/src/ImagePreview.tsx +36 -0
  288. package/src/InlineEditor.module.css +23 -0
  289. package/src/InlineEditor.tsx +69 -0
  290. package/src/Input.module.css +38 -0
  291. package/src/Input.tsx +24 -0
  292. package/src/JsonTree.module.css +371 -0
  293. package/src/JsonTree.tsx +850 -0
  294. package/src/LinkIcon.tsx +118 -0
  295. package/src/Menu.module.css +443 -0
  296. package/src/Menu.tsx +553 -0
  297. package/src/MenuGroup.module.css +37 -0
  298. package/src/MenuGroup.tsx +103 -0
  299. package/src/MenuRow.tsx +89 -0
  300. package/src/MenuSurface.module.css +65 -0
  301. package/src/MenuSurface.tsx +54 -0
  302. package/src/Modal.module.css +147 -0
  303. package/src/Modal.tsx +96 -0
  304. package/src/PathLabel.module.css +28 -0
  305. package/src/PathLabel.tsx +41 -0
  306. package/src/PermissionIcon.tsx +119 -0
  307. package/src/Pill.module.css +30 -0
  308. package/src/Pill.tsx +36 -0
  309. package/src/QuotePreview.module.css +138 -0
  310. package/src/QuotePreview.tsx +133 -0
  311. package/src/ReadBlock.module.css +45 -0
  312. package/src/ReadBlock.tsx +151 -0
  313. package/src/ReferenceIcon.tsx +49 -0
  314. package/src/RiskConfirmation.module.css +47 -0
  315. package/src/RiskConfirmation.tsx +79 -0
  316. package/src/RowDragPreview.module.css +12 -0
  317. package/src/SearchBlock.module.css +114 -0
  318. package/src/SearchBlock.tsx +281 -0
  319. package/src/SegmentedControl.module.css +81 -0
  320. package/src/SegmentedControl.tsx +140 -0
  321. package/src/SegmentedTabs.module.css +77 -0
  322. package/src/SegmentedTabs.tsx +84 -0
  323. package/src/ShortcutKeys.module.css +33 -0
  324. package/src/ShortcutKeys.tsx +12 -0
  325. package/src/SiteGlyph.tsx +149 -0
  326. package/src/StateDot.module.css +100 -0
  327. package/src/StateDot.tsx +77 -0
  328. package/src/SubmenuPanel.tsx +51 -0
  329. package/src/Switch.module.css +49 -0
  330. package/src/Switch.tsx +42 -0
  331. package/src/Tag.module.css +57 -0
  332. package/src/Tag.tsx +42 -0
  333. package/src/TerminalBlock.module.css +207 -0
  334. package/src/TerminalBlock.tsx +294 -0
  335. package/src/TextShimmer.module.css +88 -0
  336. package/src/TextShimmer.tsx +58 -0
  337. package/src/Toast.module.css +102 -0
  338. package/src/Toast.tsx +101 -0
  339. package/src/Tooltip.module.css +68 -0
  340. package/src/Tooltip.tsx +273 -0
  341. package/src/UserMessageBubble.module.css +11 -0
  342. package/src/UserMessageBubble.tsx +16 -0
  343. package/src/WebBlock.module.css +153 -0
  344. package/src/WebBlock.tsx +196 -0
  345. package/src/ZoomControls.module.css +54 -0
  346. package/src/ZoomControls.tsx +92 -0
  347. package/src/ansi.ts +447 -0
  348. package/src/clipboard.ts +47 -0
  349. package/src/code-file-icon-artwork.manifest.json +71 -0
  350. package/src/code-file-icon-artwork.ts +60 -0
  351. package/src/code-file-types.ts +340 -0
  352. package/src/code-highlighting.ts +25 -0
  353. package/src/css-modules.d.ts +6 -0
  354. package/src/darwin-desktop.ts +11 -0
  355. package/src/document-zoom.ts +12 -0
  356. package/src/file-drag-actions.ts +29 -0
  357. package/src/file-size.ts +16 -0
  358. package/src/focus.ts +44 -0
  359. package/src/guide-artwork.tsx +32 -0
  360. package/src/head-tail-cap.ts +27 -0
  361. package/src/icons/artwork.ts +156 -0
  362. package/src/icons/index.tsx +634 -0
  363. package/src/icons/props.ts +8 -0
  364. package/src/icons/shared-artwork.tsx +71 -0
  365. package/src/image-drag.ts +84 -0
  366. package/src/index.ts +142 -0
  367. package/src/input-modality.ts +59 -0
  368. package/src/keyboard-composition.ts +36 -0
  369. package/src/markdown/CodeBlock.module.css +209 -0
  370. package/src/markdown/CodeBlock.tsx +268 -0
  371. package/src/markdown/FenceBlocks.tsx +51 -0
  372. package/src/markdown/JsonBlock.module.css +34 -0
  373. package/src/markdown/JsonBlock.tsx +36 -0
  374. package/src/markdown/MarkdownDelegate.tsx +102 -0
  375. package/src/markdown/MarkdownText.module.css +511 -0
  376. package/src/markdown/MarkdownText.tsx +218 -0
  377. package/src/markdown/cjkFriendlyStrong.ts +83 -0
  378. package/src/markdown/copy-tex.ts +53 -0
  379. package/src/markdown/diff-fence.ts +212 -0
  380. package/src/markdown/file-link.ts +45 -0
  381. package/src/markdown/highlight.ts +689 -0
  382. package/src/markdown/incremental.ts +360 -0
  383. package/src/markdown/katex.tsx +90 -0
  384. package/src/markdown/local-image-syntax.ts +29 -0
  385. package/src/markdown/mathCompatibility.ts +349 -0
  386. package/src/markdown/parse.ts +49 -0
  387. package/src/markdown/plain-text.ts +121 -0
  388. package/src/markdown/quote-text.ts +10 -0
  389. package/src/markdown/render.tsx +834 -0
  390. package/src/markdown/useViewportHighlighting.ts +72 -0
  391. package/src/markdown/windows-path-destinations.ts +34 -0
  392. package/src/menu-submenu-position.ts +20 -0
  393. package/src/overlay-top-margin.ts +18 -0
  394. package/src/plugin-artwork.tsx +110 -0
  395. package/src/pointer-grace.ts +47 -0
  396. package/src/rank-by-name.ts +93 -0
  397. package/src/relative-time.ts +36 -0
  398. package/src/settings-form/SettingsForm.module.css +53 -0
  399. package/src/settings-form/SettingsForm.tsx +74 -0
  400. package/src/settings-form/fields.module.css +153 -0
  401. package/src/settings-form/fields.tsx +151 -0
  402. package/src/settings-form/form-model.ts +393 -0
  403. package/src/tooltip-placement.ts +60 -0
  404. package/src/use-clipboard-copy.ts +44 -0
  405. package/src/useAnchoredMaxHeight.ts +45 -0
  406. package/src/useDismissOnOutsidePointer.ts +34 -0
  407. package/src/useMenuSubmenu.ts +182 -0
  408. package/src/useModalLayer.ts +108 -0
  409. package/src/useRowDragPreview.ts +76 -0
  410. package/src/user-text.module.css +46 -0
  411. package/src/user-text.tsx +67 -0
  412. package/lib/types/useAnchoredPosition.d.ts +0 -36
@@ -0,0 +1,689 @@
1
+ /**
2
+ * The client's ONE syntax highlighter: a synchronous fine-grained shiki core
3
+ * (JavaScript regex engine — no oniguruma WASM, bundle-friendly) with an
4
+ * explicit grammar allowlist and a CSS-variables theme. Colors live in the
5
+ * theme package's token sheets as `--shiki-*` custom properties (light and
6
+ * dark blocks), never here — the repo's tokens-only styling rule.
7
+ *
8
+ * Only the three markdown-fence and `run_code` grammars (TypeScript, shell,
9
+ * JSON) load into the singleton at boot — the set every session renders. Every
10
+ * other language in the shared extension table
11
+ * (`@deepseek-ai/dsh-util-code-language`: python, rust, yaml, markup, …) is
12
+ * imported lazily and registered the first time such a language is requested,
13
+ * so a session that never opens a code surface in one of those languages pays
14
+ * neither the grammar modules nor their synchronous init. The first render of a
15
+ * lazy language falls back to plain text while its grammar loads, then
16
+ * {@link onGrammarLoaded} notifies subscribers to re-render with highlighting.
17
+ * An unknown or absent language falls back to plain text (no highlighting, still
18
+ * monospace) — never an error.
19
+ */
20
+
21
+ import { createHighlighterCoreSync, createCssVariablesTheme } from 'shiki/core'
22
+ import { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor } from 'shiki/engine/javascript'
23
+ import langTs from '@shikijs/langs/typescript'
24
+ import langBash from '@shikijs/langs/shellscript'
25
+ import langJson from '@shikijs/langs/json'
26
+ import type { GrammarState, HighlighterCore, ThemedToken } from 'shiki/core'
27
+ import type { CSSProperties } from 'react'
28
+
29
+ /** A shiki grammar module's default export (a `LanguageRegistration[]`), taken
30
+ * from a boot grammar so no direct `@shikijs/types` dependency is needed. */
31
+ type LangModule = { default: typeof langTs }
32
+
33
+ /**
34
+ * Grammars the singleton loads at boot; each entry's own `name` is the id
35
+ * `codeToTokens`/`codeToHtml` resolve. The JS-family aliases (js/jsx/ts/tsx)
36
+ * resolve to the TypeScript grammar rather than a separate one: it tokenizes
37
+ * plain TS/JS exactly, and JSX/TSX approximately (shiki's TS grammar is not the
38
+ * dedicated TSX grammar, so JSX elements tokenize imperfectly) — an accepted
39
+ * trade to keep the boot set to one JS-family grammar. Every other language in
40
+ * the shared extension table loads lazily through {@link LAZY_GRAMMARS}.
41
+ */
42
+ const LANGS = [langTs, langBash, langJson]
43
+
44
+ /**
45
+ * Replace the Go grammar's repeated one-line struct-field rule with the rule
46
+ * worlpaker/go-syntax merged in 4a9b120a52c (https://github.com/worlpaker/go-syntax/pull/25):
47
+ * the repeated rule backtracks exponentially on lines such as a brace-bearing
48
+ * comment after `struct {`. Delete this patch once `@shikijs/langs` ships that
49
+ * commit; 4.5.0 does not. The rule is located as the only rule in
50
+ * `struct_variables_types_fields` containing the field-token literal below,
51
+ * taken from `@shikijs/langs` 4.4.3.
52
+ * @param grammars - the `@shikijs/langs/go` default export, left unmodified.
53
+ * @returns copies of `grammars` with that rule's match replaced in the `go` grammar.
54
+ * @throws When the module has no `go` grammar, the grammar lacks
55
+ * `struct_variables_types_fields`, or that entry has no or several such rules.
56
+ * The lazy load then rejects unhandled and Go code renders as plain text.
57
+ */
58
+ export function patchGoGrammar(grammars: LangModule['default']): LangModule['default'] {
59
+ const go = grammars.find(grammar => grammar.name === 'go')
60
+ const fields = go?.repository.struct_variables_types_fields
61
+ const patterns = fields?.patterns ?? []
62
+ const rules = patterns.filter(rule => typeof rule.match === 'string' && rule.match.includes('[^"/`\\s]+;?'))
63
+ if (go === undefined || fields === undefined || rules.length !== 1) {
64
+ throw new Error(`ui-primitives: expected exactly one Go struct-field rule to patch for worlpaker/go-syntax#25, found ${
65
+ rules.length}; update or delete the patch`)
66
+ }
67
+ const repository = { ...go.repository }
68
+ repository.struct_variables_types_fields = {
69
+ ...fields,
70
+ patterns: patterns.map(rule => rule === rules[0]
71
+ ? { ...rule, match: '(?:(?<=\\{)([^/\\`"]*[^\\s/\\`"])\\s*(?=\\}))' }
72
+ : rule),
73
+ }
74
+ return grammars.map(grammar => grammar === go ? { ...go, repository } : grammar)
75
+ }
76
+
77
+ /** The Perl grammar's bare `/…/` rule begins a match only after these characters or at line start. */
78
+ const PERL_FIND_BEGIN = '(?<=[\\&({|~]|if|unless|^)\\s*((/))'
79
+
80
+ /**
81
+ * Words after which a Perl `/` starts a match rather than a division. A word
82
+ * counts only when no identifier character, sigil, `>`, `:` or `-` precedes it,
83
+ * so `$split / 2` and `$obj->return / 2` stay divisions. Each word is a
84
+ * separate fixed-length lookbehind alternative because the JavaScript engine's
85
+ * Oniguruma translator rejects nested lookbehinds.
86
+ */
87
+ const PERL_FIND_WORDS = ['if', 'unless', 'split', 'grep', 'and', 'or', 'not', 'return', 'while', 'until']
88
+
89
+ /**
90
+ * Widen the vendored Perl grammar's bare `/…/` match rule, which begins only
91
+ * after `&`, `(`, `{`, `|`, `~`, `if`, `unless` or at line start. Elsewhere,
92
+ * such as `split /^---\s*$/m, $text`, the pattern body tokenizes as code: its
93
+ * `m,` opens an `m` match delimited by commas that runs to the end of the
94
+ * source. The patched rule also begins after `,`, `!`, `=` and `=>`, and after
95
+ * the list operators `split` and `grep`, the low-precedence operators `and`,
96
+ * `or` and `not`, and `return`, `while`, `until`, `if` and `unless` when no
97
+ * identifier character, sigil, `>`, `:` or `-` precedes the word. The upstream
98
+ * textmate/perl.tmbundle grammar, which `@shikijs/langs` 4.4.3 ships through
99
+ * VS Code, has the same rule.
100
+ * @param grammars - the `@shikijs/langs/perl` default export, left unmodified.
101
+ * @returns copies of `grammars` with that rule's begin replaced in the `perl` grammar.
102
+ * @throws When the module has no `perl` grammar or that grammar has no or several
103
+ * top-level rules with the vendored begin pattern. The lazy load then rejects
104
+ * unhandled and Perl code renders as plain text.
105
+ */
106
+ export function patchPerlGrammar(grammars: LangModule['default']): LangModule['default'] {
107
+ const perl = grammars.find(grammar => grammar.name === 'perl')
108
+ const rules = perl?.patterns.filter(rule => rule.begin === PERL_FIND_BEGIN) ?? []
109
+ if (perl === undefined || rules.length !== 1) {
110
+ throw new Error(`ui-primitives: expected exactly one Perl match rule to patch, found ${rules.length}; update or delete the patch`)
111
+ }
112
+ const words = PERL_FIND_WORDS.flatMap(word => [`[^\\w$@%&>:-]${word}`, `^${word}`]).join('|')
113
+ const begin = `(?<=[\\&({|~,!=]|=>|^|${words})\\s*((/))`
114
+ const patterns = perl.patterns.map(rule => rule === rules[0] ? { ...rule, begin } : rule)
115
+ return grammars.map(grammar => grammar === perl ? { ...perl, patterns } : grammar)
116
+ }
117
+
118
+ /**
119
+ * The non-boot extension grammars, each behind a dynamic import so its module
120
+ * stays out of the boot chunk until a code surface renders that language.
121
+ * Keyed by the grammar id (`LanguageRegistration.name`) the aliases resolve to.
122
+ * `@shikijs/langs`' default export is a `LanguageRegistration[]`; the loader
123
+ * hands the whole array to `loadLanguageSync`, which registers each entry
124
+ * (including embedded sub-grammars). The three boot grammars are absent —
125
+ * already loaded, so no alias value ever points at a missing entry here.
126
+ */
127
+ const LAZY_GRAMMARS = new Map<string, () => Promise<LangModule>>([
128
+ ['python', () => import('@shikijs/langs/python')],
129
+ ['ruby', () => import('@shikijs/langs/ruby')],
130
+ ['go', async () => ({ default: patchGoGrammar((await import('@shikijs/langs/go')).default) })],
131
+ ['rust', () => import('@shikijs/langs/rust')],
132
+ ['java', () => import('@shikijs/langs/java')],
133
+ ['c', () => import('@shikijs/langs/c')],
134
+ ['cpp', () => import('@shikijs/langs/cpp')],
135
+ ['csharp', () => import('@shikijs/langs/csharp')],
136
+ ['kotlin', () => import('@shikijs/langs/kotlin')],
137
+ ['swift', () => import('@shikijs/langs/swift')],
138
+ ['php', () => import('@shikijs/langs/php')],
139
+ ['yaml', () => import('@shikijs/langs/yaml')],
140
+ ['toml', () => import('@shikijs/langs/toml')],
141
+ ['ini', () => import('@shikijs/langs/ini')],
142
+ ['markdown', () => import('@shikijs/langs/markdown')],
143
+ ['mdx', () => import('@shikijs/langs/mdx')],
144
+ ['html', () => import('@shikijs/langs/html')],
145
+ ['css', () => import('@shikijs/langs/css')],
146
+ ['scss', () => import('@shikijs/langs/scss')],
147
+ ['less', () => import('@shikijs/langs/less')],
148
+ ['sql', () => import('@shikijs/langs/sql')],
149
+ ['xml', () => import('@shikijs/langs/xml')],
150
+ ['lua', () => import('@shikijs/langs/lua')],
151
+ ['bat', () => import('@shikijs/langs/bat')],
152
+ ['powershell', () => import('@shikijs/langs/powershell')],
153
+ ['fish', () => import('@shikijs/langs/fish')],
154
+ ['dotenv', () => import('@shikijs/langs/dotenv')],
155
+ ['log', () => import('@shikijs/langs/log')],
156
+ ['csv', () => import('@shikijs/langs/csv')],
157
+ ['diff', () => import('@shikijs/langs/diff')],
158
+ ['http', () => import('@shikijs/langs/http')],
159
+ ['rst', () => import('@shikijs/langs/rst')],
160
+ ['latex', () => import('@shikijs/langs/latex')],
161
+ ['bibtex', () => import('@shikijs/langs/bibtex')],
162
+ ['asciidoc', () => import('@shikijs/langs/asciidoc')],
163
+ ['r', () => import('@shikijs/langs/r')],
164
+ ['julia', () => import('@shikijs/langs/julia')],
165
+ ['dart', () => import('@shikijs/langs/dart')],
166
+ ['scala', () => import('@shikijs/langs/scala')],
167
+ ['clojure', () => import('@shikijs/langs/clojure')],
168
+ ['erlang', () => import('@shikijs/langs/erlang')],
169
+ ['elixir', () => import('@shikijs/langs/elixir')],
170
+ ['haskell', () => import('@shikijs/langs/haskell')],
171
+ ['fsharp', () => import('@shikijs/langs/fsharp')],
172
+ ['vb', () => import('@shikijs/langs/vb')],
173
+ ['perl', async () => ({ default: patchPerlGrammar((await import('@shikijs/langs/perl')).default) })],
174
+ ['verilog', () => import('@shikijs/langs/verilog')],
175
+ ['system-verilog', () => import('@shikijs/langs/system-verilog')],
176
+ ['graphql', () => import('@shikijs/langs/graphql')],
177
+ ['proto', () => import('@shikijs/langs/proto')],
178
+ ['hcl', () => import('@shikijs/langs/hcl')],
179
+ ['nix', () => import('@shikijs/langs/nix')],
180
+ ['vue', () => import('@shikijs/langs/vue')],
181
+ ['svelte', () => import('@shikijs/langs/svelte')],
182
+ ['make', () => import('@shikijs/langs/make')],
183
+ ['cmake', () => import('@shikijs/langs/cmake')],
184
+ ['groovy', () => import('@shikijs/langs/groovy')],
185
+ ])
186
+
187
+ /**
188
+ * Language ids (and aliases) the highlighter accepts; everything else renders
189
+ * plain. A Map, not an object: fence info strings are assistant-authored, so
190
+ * a label like `constructor` or `__proto__` must miss instead of resolving an
191
+ * inherited property and crashing the renderer inside shiki. Keys cover both
192
+ * the markdown-fence aliases `CodeBlock` uses, the file-extension language ids
193
+ * `@deepseek-ai/dsh-util-code-language` resolves, and the short ids
194
+ * `readLangHintForPath` persists, so every caller resolves the same grammars.
195
+ * A new short name in the shared table must be aliased here too. The JS family maps to the TypeScript grammar (see {@link LANGS} for
196
+ * the JSX/TSX approximation). A value not in {@link LANGS} names a
197
+ * {@link LAZY_GRAMMARS} entry loaded on first use.
198
+ */
199
+ const LANG_ALIASES = new Map<string, string>([
200
+ ['typescript', 'typescript'],
201
+ ['ts', 'typescript'],
202
+ ['tsx', 'typescript'],
203
+ ['javascript', 'typescript'],
204
+ ['js', 'typescript'],
205
+ ['jsx', 'typescript'],
206
+ ['shellscript', 'shellscript'],
207
+ ['bash', 'shellscript'],
208
+ ['sh', 'shellscript'],
209
+ ['shell', 'shellscript'],
210
+ ['zsh', 'shellscript'],
211
+ ['json', 'json'],
212
+ ['jsonc', 'json'],
213
+ ['py', 'python'],
214
+ ['python', 'python'],
215
+ ['rb', 'ruby'],
216
+ ['ruby', 'ruby'],
217
+ ['go', 'go'],
218
+ ['rs', 'rust'],
219
+ ['rust', 'rust'],
220
+ ['java', 'java'],
221
+ ['c', 'c'],
222
+ ['cpp', 'cpp'],
223
+ ['cs', 'csharp'],
224
+ ['csharp', 'csharp'],
225
+ ['kotlin', 'kotlin'],
226
+ ['swift', 'swift'],
227
+ ['php', 'php'],
228
+ ['yaml', 'yaml'],
229
+ ['yml', 'yaml'],
230
+ ['toml', 'toml'],
231
+ ['ini', 'ini'],
232
+ ['md', 'markdown'],
233
+ ['markdown', 'markdown'],
234
+ ['mdx', 'mdx'],
235
+ ['html', 'html'],
236
+ ['css', 'css'],
237
+ ['scss', 'scss'],
238
+ ['less', 'less'],
239
+ ['sql', 'sql'],
240
+ ['xml', 'xml'],
241
+ ['lua', 'lua'],
242
+ ['bat', 'bat'],
243
+ ['batch', 'bat'],
244
+ ['powershell', 'powershell'],
245
+ ['ps1', 'powershell'],
246
+ ['ps', 'powershell'],
247
+ ['fish', 'fish'],
248
+ ['properties', 'ini'],
249
+ ['dotenv', 'dotenv'],
250
+ ['env', 'dotenv'],
251
+ ['log', 'log'],
252
+ ['csv', 'csv'],
253
+ ['diff', 'diff'],
254
+ ['patch', 'diff'],
255
+ ['http', 'http'],
256
+ ['rst', 'rst'],
257
+ ['latex', 'latex'],
258
+ ['tex', 'latex'],
259
+ ['bibtex', 'bibtex'],
260
+ ['bib', 'bibtex'],
261
+ ['asciidoc', 'asciidoc'],
262
+ ['adoc', 'asciidoc'],
263
+ ['r', 'r'],
264
+ ['julia', 'julia'],
265
+ ['jl', 'julia'],
266
+ ['dart', 'dart'],
267
+ ['scala', 'scala'],
268
+ ['clojure', 'clojure'],
269
+ ['clj', 'clojure'],
270
+ ['erlang', 'erlang'],
271
+ ['erl', 'erlang'],
272
+ ['elixir', 'elixir'],
273
+ ['ex', 'elixir'],
274
+ ['exs', 'elixir'],
275
+ ['haskell', 'haskell'],
276
+ ['hs', 'haskell'],
277
+ ['fsharp', 'fsharp'],
278
+ ['fs', 'fsharp'],
279
+ ['fsi', 'fsharp'],
280
+ ['fsx', 'fsharp'],
281
+ ['vb', 'vb'],
282
+ ['vbnet', 'vb'],
283
+ ['perl', 'perl'],
284
+ ['pl', 'perl'],
285
+ ['pm', 'perl'],
286
+ ['verilog', 'verilog'],
287
+ // Shiki's own `v` grammar is the V language; the fence label and the `.v`
288
+ // extension both name Verilog here, so a future V registration needs a
289
+ // different alias.
290
+ ['v', 'verilog'],
291
+ ['system-verilog', 'system-verilog'],
292
+ ['systemverilog', 'system-verilog'],
293
+ ['sv', 'system-verilog'],
294
+ ['svh', 'system-verilog'],
295
+ ['graphql', 'graphql'],
296
+ ['gql', 'graphql'],
297
+ ['proto', 'proto'],
298
+ ['protobuf', 'proto'],
299
+ ['hcl', 'hcl'],
300
+ ['tf', 'hcl'],
301
+ ['tfvars', 'hcl'],
302
+ ['nix', 'nix'],
303
+ ['vue', 'vue'],
304
+ ['svelte', 'svelte'],
305
+ ['make', 'make'],
306
+ ['makefile', 'make'],
307
+ ['mk', 'make'],
308
+ ['cmake', 'cmake'],
309
+ ['groovy', 'groovy'],
310
+ ['gradle', 'groovy'],
311
+ ])
312
+
313
+ /**
314
+ * Resolve a language hint to the grammar id {@link LANG_ALIASES} selects.
315
+ * @param lang - Language hint from a code surface: a canonical grammar id or the read card's persisted short id.
316
+ * @returns The resolved grammar id, or `undefined` when the table aliases no grammar.
317
+ */
318
+ export function grammarForHint(lang: string | undefined): string | undefined {
319
+ return lang === undefined ? undefined : LANG_ALIASES.get(lang.toLowerCase())
320
+ }
321
+
322
+ /**
323
+ * Whether a language hint can use the shared syntax highlighter.
324
+ * @param lang - Language hint from a code surface.
325
+ * @returns Whether the hint resolves to a supported grammar.
326
+ */
327
+ export function supportsHighlighting(lang: string | undefined): boolean {
328
+ return grammarForHint(lang) !== undefined
329
+ }
330
+
331
+ /** All token colors resolve through `--shiki-*` custom properties (theme package sheets). */
332
+ const cssVariablesTheme = createCssVariablesTheme({
333
+ name: 'css-variables',
334
+ variablePrefix: '--shiki-',
335
+ fontStyle: true,
336
+ })
337
+
338
+ /**
339
+ * The client regex engine compiles each TextMate pattern when its scanner is
340
+ * created. Shiki otherwise defers patterns longer than 3,000 characters until
341
+ * their first match; that compilation counts against Shiki's 500 ms per-line
342
+ * budget and can return a partial token stream under host contention. Eager
343
+ * compilation leaves the same budget in place for scanning user content.
344
+ */
345
+ const regexEngine = createJavaScriptRegexEngine({
346
+ forgiving: true,
347
+ regexConstructor: pattern => defaultJavaScriptRegexConstructor(pattern, {
348
+ lazyCompileLength: Number.POSITIVE_INFINITY,
349
+ }),
350
+ })
351
+
352
+ /**
353
+ * Lines at least this many UTF-16 units long render as one uncolored token.
354
+ * With the JavaScript regex engine, scanning one JavaScript string-literal
355
+ * line costs about 70 ms at 1,000 units, 300 ms at 2,000, 600 ms at 5,000 and
356
+ * 2.5 s at 10,000, so this cap keeps one line within a few frames. A skipped
357
+ * line leaves the grammar state unchanged: a multi-line construct that opens
358
+ * or closes on it colors the following lines as if the line were absent.
359
+ */
360
+ const TOKENIZE_MAX_LINE_LENGTH = 1000
361
+
362
+ let singleton: HighlighterCore | undefined
363
+
364
+ /** Representative paths through every boot grammar, compiled before user content is timed. */
365
+ const BOOT_GRAMMAR_WARMUPS = [
366
+ { lang: 'typescript', code: 'const answer: number = 42' },
367
+ { lang: 'shellscript', code: 'printf \'%s\\n\' "$HOME"' },
368
+ { lang: 'json', code: '{"ready":true}' },
369
+ ] as const
370
+
371
+ /** Construct and pre-tokenize the boot grammars outside the user-content scan budget. */
372
+ function createHighlighter(): HighlighterCore {
373
+ const instance = createHighlighterCoreSync({
374
+ themes: [cssVariablesTheme],
375
+ langs: LANGS,
376
+ engine: regexEngine,
377
+ })
378
+ for (const sample of BOOT_GRAMMAR_WARMUPS) {
379
+ instance.codeToTokens(sample.code, {
380
+ lang: sample.lang,
381
+ theme: 'css-variables',
382
+ tokenizeTimeLimit: 0,
383
+ })
384
+ }
385
+ return instance
386
+ }
387
+
388
+ /** The synchronous highlighter (one instance per document); pre-warmed below, lazy as the fallback. */
389
+ function highlighter(): HighlighterCore {
390
+ singleton ??= createHighlighter()
391
+ return singleton
392
+ }
393
+
394
+ /** Grammar ids whose lazy import is in flight or done, so it is requested once. */
395
+ const requested = new Set<string>()
396
+ /** Subscribers re-rendered after a lazy grammar registers (React callers). */
397
+ const listeners = new Set<() => void>()
398
+ /** Bumped on each lazy-grammar load; the `useSyncExternalStore` snapshot. */
399
+ let loadCount = 0
400
+
401
+ /**
402
+ * Subscribe to lazy-grammar load completions; `listener` fires after a
403
+ * {@link LAZY_GRAMMARS} grammar finishes registering on the singleton, so a
404
+ * caller that rendered its plain fallback while the grammar loaded can
405
+ * re-highlight. Uses the `useSyncExternalStore` subscribe signature; pair it with
406
+ * {@link grammarLoadCount} as the snapshot. Returns an unsubscribe function.
407
+ * @param listener - invoked (no args) on each grammar-load completion.
408
+ * @returns a disposer that removes the listener.
409
+ */
410
+ export function subscribeGrammarLoaded(listener: () => void): () => void {
411
+ listeners.add(listener)
412
+ return () => { listeners.delete(listener) }
413
+ }
414
+
415
+ /**
416
+ * The lazy-grammar load counter — a value that changes on every load, so a
417
+ * `useSyncExternalStore` snapshot re-renders the subscriber when a grammar
418
+ * registers. Opaque: only its identity across renders matters.
419
+ * @returns the current load count.
420
+ */
421
+ export function grammarLoadCount(): number {
422
+ return loadCount
423
+ }
424
+
425
+ /**
426
+ * Ensure the grammar `resolved` names is registered. A boot grammar (not in
427
+ * {@link LAZY_GRAMMARS}) and an already-loaded lazy grammar report ready
428
+ * synchronously; a lazy grammar not yet loaded starts its import (once) and
429
+ * reports not-ready, so the caller renders plain until a
430
+ * {@link subscribeGrammarLoaded} listener fires.
431
+ * @param resolved - the grammar id an alias resolved to.
432
+ * @returns whether the grammar is registered and ready to tokenize now.
433
+ */
434
+ function ensureGrammar(resolved: string): boolean {
435
+ const load = LAZY_GRAMMARS.get(resolved)
436
+ // A boot grammar (already registered) has no lazy loader; it is always ready.
437
+ if (load === undefined) return true
438
+ if (highlighter().getLoadedLanguages().includes(resolved)) return true
439
+ if (!requested.has(resolved)) {
440
+ requested.add(resolved)
441
+ void load().then((mod) => {
442
+ highlighter().loadLanguageSync(mod.default)
443
+ loadCount += 1
444
+ for (const listener of listeners) listener()
445
+ })
446
+ }
447
+ return false
448
+ }
449
+
450
+ // Engine + grammar construction costs a long task (~120-175ms); building it
451
+ // during the first finalized fence's render would jank exactly when a stream
452
+ // completes. Warm the singleton in a deferred task at module load (= plugin
453
+ // boot) instead; the lazy path above stays as the correctness fallback for a
454
+ // fence that renders before the timer fires. `unref` (Node-only) keeps a
455
+ // non-browser import from pinning the event loop.
456
+ const warmupTimer = setTimeout(() => { highlighter() }, 0)
457
+ ;(warmupTimer as { unref?: () => void }).unref?.()
458
+
459
+ /**
460
+ * Highlight `code` into shiki's HTML (a single `<pre class="shiki">` tree)
461
+ * when `lang` maps to a registered grammar; `undefined` means the caller
462
+ * renders its plain fallback. A lazy grammar not yet loaded returns `undefined`
463
+ * for this call and loads in the background; subscribe with
464
+ * {@link onGrammarLoaded} to re-highlight once it registers.
465
+ * @param code - the source text.
466
+ * @param lang - the language hint (a markdown fence info string or a fixed caller id).
467
+ * @returns the highlighted HTML, or `undefined` for unknown or not-yet-loaded languages.
468
+ */
469
+ export function highlightToHtml(code: string, lang: string | undefined): string | undefined {
470
+ const resolved = grammarForHint(lang)
471
+ if (resolved === undefined) return undefined
472
+ if (!ensureGrammar(resolved)) return undefined
473
+ return highlighter().codeToHtml(code, {
474
+ lang: resolved, theme: 'css-variables', tokenizeMaxLineLength: TOKENIZE_MAX_LINE_LENGTH,
475
+ })
476
+ }
477
+
478
+ /**
479
+ * One highlighted run of a line: the text and the inline style shiki assigned
480
+ * it. The css-variables theme colors every run through a `--shiki-*` custom
481
+ * property, so `style.color` is always present; it is held as a style object
482
+ * rather than a bare color so a run spreads onto a `<span style>` uniformly.
483
+ */
484
+ export interface HighlightSpan {
485
+ text: string
486
+ style: CSSProperties
487
+ }
488
+
489
+ /** vscode-textmate FontStyle bits shiki folds into `text-decoration` values. */
490
+ const DECORATION_BITS: readonly (readonly [number, string])[] = [[4, 'underline'], [8, 'line-through']]
491
+
492
+ /**
493
+ * The inline style shiki's HTML arm assigns one token (`getTokenStyleObject`
494
+ * mirrored onto React style keys): the css-variables color plus the
495
+ * vscode-textmate font-style bits the theme lets through — italic (1), bold
496
+ * (2), and the {@link DECORATION_BITS} decorations (the theme injects bold,
497
+ * italic, and underline rules for markup scopes, so markdown fences carry
498
+ * them). The theme has no per-scope backgrounds, so `background-color` never
499
+ * occurs; the arm-parity tests fail loud if a shiki upgrade changes that.
500
+ */
501
+ function spanStyle(token: ThemedToken): CSSProperties {
502
+ const style: CSSProperties = { color: token.color }
503
+ /* v8 ignore next -- fontStyle is optional in ThemedToken's type; tokenizeWithTheme always stamps it. */
504
+ const bits = token.fontStyle ?? 0
505
+ if ((bits & 1) !== 0) style.fontStyle = 'italic'
506
+ if ((bits & 2) !== 0) style.fontWeight = 'bold'
507
+ const decorations = DECORATION_BITS.filter(([bit]) => (bits & bit) !== 0)
508
+ if (decorations.length > 0) style.textDecoration = decorations.map(([, value]) => value).join(' ')
509
+ return style
510
+ }
511
+
512
+ /**
513
+ * Narrow one tokenized line to the runs a `<span style>` renders, folding a
514
+ * whitespace-only run into the token that follows it — shiki's default
515
+ * `mergeWhitespaces` HTML behavior — with each run styled through
516
+ * {@link spanStyle}, so the streaming spans and the settled `codeToHtml`
517
+ * swap render one identical span tree. shiki exempts underlined/struck
518
+ * whitespace from the fold; under the css-variables theme that case cannot
519
+ * occur — its only underline rule styles inline-link scopes, whose spaced
520
+ * text tokenizes as one run, and it injects no strikethrough rule — so the
521
+ * unconditional fold here stays equivalent (the markdown arm-parity test
522
+ * pins it). A line-trailing whitespace-only run has no follower and keeps
523
+ * its own span, as in shiki.
524
+ */
525
+ function lineSpans(line: ThemedToken[]): HighlightSpan[] {
526
+ const spans: HighlightSpan[] = []
527
+ let pendingWhitespace = ''
528
+ for (const [index, token] of line.entries()) {
529
+ if (/^\s+$/.test(token.content) && index + 1 < line.length) {
530
+ pendingWhitespace += token.content
531
+ continue
532
+ }
533
+ spans.push({ text: pendingWhitespace + token.content, style: spanStyle(token) })
534
+ pendingWhitespace = ''
535
+ }
536
+ return spans
537
+ }
538
+
539
+ /**
540
+ * Incremental highlighter for one growing streaming fence. TextMate
541
+ * tokenization is line-based and forward-only — a line's tokens depend only on
542
+ * its own text and the grammar state entering it — so appended text never
543
+ * changes a completed line's tokens. The session caches the spans of every
544
+ * completed line together with the grammar state after them;
545
+ * {@link updateFrame} reports only newly completed lines plus the still-growing
546
+ * last line, while {@link update} materializes the complete compatibility
547
+ * result. Per-call tokenization cost therefore excludes the completed prefix,
548
+ * and the result equals a from-scratch tokenization of the same code.
549
+ * Non-append input and a change of resolved grammar reset the cache and
550
+ * re-tokenize fully, so any input stays correct.
551
+ */
552
+ export class StreamingHighlightSession {
553
+ /** Grammar id the cache was built with; a different resolution resets it. */
554
+ private resolved: string | undefined
555
+ /** Newline-terminated source prefix covered by {@link spans}. */
556
+ private prefix = ''
557
+ /** Cached spans, one entry per completed line of {@link prefix}. */
558
+ private spans: HighlightSpan[][] = []
559
+ /** Grammar state after {@link prefix}; undefined = the grammar's initial state. */
560
+ private state: GrammarState | undefined
561
+ private lastCode: string | undefined
562
+ private lastLang: string | undefined
563
+ private lastResult: HighlightSpan[][] | undefined
564
+ private generation = 0
565
+ private lastFrame: StreamingHighlightFrame | undefined
566
+
567
+ private reset(resolved: string | undefined): void {
568
+ this.resolved = resolved
569
+ this.prefix = ''
570
+ this.spans = []
571
+ this.state = undefined
572
+ this.generation += 1
573
+ this.lastFrame = undefined
574
+ }
575
+
576
+ /** Tokenize `text` with `resolved`, resuming from the cached grammar state when one exists. */
577
+ private tokenize(resolved: string, text: string): ThemedToken[][] {
578
+ return highlighter().codeToTokensBase(text, {
579
+ lang: resolved,
580
+ theme: 'css-variables',
581
+ tokenizeMaxLineLength: TOKENIZE_MAX_LINE_LENGTH,
582
+ ...(this.state === undefined ? {} : { grammarState: this.state }),
583
+ })
584
+ }
585
+
586
+ /**
587
+ * Tokenize one update as a delta for a retained renderer.
588
+ * @param code - the fence text accumulated so far.
589
+ * @param lang - the language hint.
590
+ * @returns Newly completed lines plus the current tail, or `undefined` for the plain arm.
591
+ */
592
+ updateFrame(code: string, lang: string | undefined): StreamingHighlightFrame | undefined {
593
+ if (code === this.lastCode && lang === this.lastLang && this.lastFrame !== undefined) {
594
+ return this.lastFrame
595
+ }
596
+ this.lastCode = code
597
+ this.lastLang = lang
598
+ this.lastResult = undefined
599
+ const resolved = grammarForHint(lang)
600
+ if (resolved === undefined || !ensureGrammar(resolved)) {
601
+ this.reset(undefined)
602
+ return undefined
603
+ }
604
+ if (resolved !== this.resolved || !code.startsWith(this.prefix)) this.reset(resolved)
605
+ const firstNewLine = this.spans.length
606
+ const rest = code.slice(this.prefix.length)
607
+ const lastNewline = rest.lastIndexOf('\n')
608
+ if (lastNewline >= 0) {
609
+ const grownEnd = rest[lastNewline - 1] === '\r' ? lastNewline - 1 : lastNewline
610
+ const tokens = this.tokenize(resolved, rest.slice(0, grownEnd))
611
+ for (const line of tokens) this.spans.push(lineSpans(line))
612
+ this.state = highlighter().getLastGrammarState(tokens)
613
+ this.prefix = code.slice(0, this.prefix.length + lastNewline + 1)
614
+ }
615
+ this.lastFrame = {
616
+ generation: this.generation,
617
+ appended: this.spans.slice(firstNewLine),
618
+ tail: this.tokenize(resolved, rest.slice(lastNewline + 1)).map(lineSpans),
619
+ }
620
+ return this.lastFrame
621
+ }
622
+
623
+ /**
624
+ * Tokenize the fence's current text into per-line highlighted runs;
625
+ * `undefined` means the caller renders its plain fallback. Idempotent per
626
+ * (`code`, `lang`) input — repeated calls return the identical result array —
627
+ * and a retained line keeps its span-array identity across growing calls, so
628
+ * a React caller can reuse cached line elements. A lazy grammar not yet
629
+ * loaded returns `undefined` and loads in the background exactly as
630
+ * {@link highlightToHtml} does; the next call after it registers highlights.
631
+ * @param code - the fence text accumulated so far (display-trimmed, no synthetic trailing newline).
632
+ * @param lang - the language hint (a markdown fence info string).
633
+ * @returns one entry per line of `code` (each an array of runs), or `undefined` for unknown or not-yet-loaded languages.
634
+ */
635
+ update(code: string, lang: string | undefined): readonly HighlightSpan[][] | undefined {
636
+ if (code === this.lastCode && lang === this.lastLang && this.lastResult !== undefined) {
637
+ return this.lastResult
638
+ }
639
+ const frame = this.updateFrame(code, lang)
640
+ if (frame === undefined) return undefined
641
+ this.lastResult = [...this.spans, ...frame.tail]
642
+ return this.lastResult
643
+ }
644
+ }
645
+
646
+ /** One retained-renderer update from {@link StreamingHighlightSession.updateFrame}. */
647
+ export interface StreamingHighlightFrame {
648
+ /** Changes whenever prior completed lines must be discarded. */
649
+ readonly generation: number
650
+ /** Completed lines added since the preceding frame in this generation. */
651
+ readonly appended: readonly HighlightSpan[][]
652
+ /** The still-growing final line or lines, replaced by the next frame. */
653
+ readonly tail: readonly HighlightSpan[][]
654
+ }
655
+
656
+ /**
657
+ * Tokenize `code` into per-line highlighted runs when `lang` maps to a
658
+ * registered grammar; `undefined` means the caller renders its plain fallback.
659
+ * A line-numbered view needs the token runs split per line (one gutter number
660
+ * per line), which the single-`<pre>` {@link highlightToHtml} does not expose,
661
+ * so this returns shiki's own 2D line/token structure narrowed to what a run
662
+ * renders. Each run's color is a `--shiki-*` custom property, keeping token
663
+ * colors on the theme package's sheets exactly as the HTML path does; the
664
+ * markup font-style bits the theme lets through (bold/italic/underline in
665
+ * markdown scopes) are dropped — the line-numbered file view renders
666
+ * color-only runs. The trailing newline shiki appends as a final empty line
667
+ * is dropped so the run count matches the caller's own line array.
668
+ * @param code - the source text.
669
+ * @param lang - the language hint (a file-extension-derived language id).
670
+ * @returns one entry per source line (each an array of runs), or `undefined` for unknown or not-yet-loaded languages.
671
+ */
672
+ export function highlightLines(code: string, lang: string | undefined): HighlightSpan[][] | undefined {
673
+ const resolved = grammarForHint(lang)
674
+ if (resolved === undefined) return undefined
675
+ if (!ensureGrammar(resolved)) return undefined
676
+ const { tokens } = highlighter().codeToTokens(code, {
677
+ lang: resolved, theme: 'css-variables', tokenizeMaxLineLength: TOKENIZE_MAX_LINE_LENGTH,
678
+ })
679
+ // shiki tokenizes `a\nb` into two lines; a trailing newline (`a\n`) adds a
680
+ // third, empty line the caller's own line array does not carry. Drop that
681
+ // one terminator line so the two structures stay in step. The explicit
682
+ // `last !== undefined` (over `tokens[...]?.length`) keeps a single branch for
683
+ // per-file coverage, matching TerminalBlock's terminator check.
684
+ const last = tokens[tokens.length - 1]
685
+ const lines = tokens.length > 1 && last !== undefined && last.length === 0
686
+ ? tokens.slice(0, -1)
687
+ : tokens
688
+ return lines.map(line => line.map(token => ({ text: token.content, style: { color: token.color } })))
689
+ }