@deepseek-ai/dsh-client-ui-primitives 0.2.2-rc.1 → 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.
- package/README.i18n.yaml +16 -16
- package/README.md +21 -14
- package/README.zh.md +21 -14
- package/lib/HoverCard.module.css +8 -3
- package/lib/JsonTree.module.css +7 -17
- package/lib/Menu.module.css +157 -37
- package/lib/Modal.module.css +16 -0
- package/lib/QuotePreview.module.css +37 -0
- package/lib/RiskConfirmation.module.css +11 -37
- package/lib/Toast.module.css +2 -3
- package/lib/Tooltip.module.css +2 -1
- package/lib/index.js +1447 -776
- package/lib/index.js.map +1 -0
- package/lib/markdown/CodeBlock.module.css +64 -0
- package/lib/markdown/MarkdownText.module.css +20 -1
- package/lib/types/AnimatedRows.d.ts +3 -0
- package/lib/types/AnimatedRows.d.ts.map +1 -0
- package/lib/types/AnimatedRows.js.map +1 -0
- package/lib/types/BrandWordmark.d.ts.map +1 -0
- package/lib/types/BrandWordmark.js.map +1 -0
- package/lib/types/Button.d.ts.map +1 -0
- package/lib/types/Button.js.map +1 -0
- package/lib/types/Checkbox.d.ts +3 -1
- package/lib/types/Checkbox.d.ts.map +1 -0
- package/lib/types/Checkbox.js.map +1 -0
- package/lib/types/CodeFileIcon.d.ts.map +1 -0
- package/lib/types/CodeFileIcon.js.map +1 -0
- package/lib/types/CodeToolbar.d.ts.map +1 -0
- package/lib/types/CodeToolbar.js.map +1 -0
- package/lib/types/CommandText.d.ts.map +1 -0
- package/lib/types/CommandText.js.map +1 -0
- package/lib/types/ConfigField.d.ts.map +1 -0
- package/lib/types/ConfigField.js.map +1 -0
- package/lib/types/ConnectionIndicator.d.ts.map +1 -0
- package/lib/types/ConnectionIndicator.js.map +1 -0
- package/lib/types/DetailDocument.d.ts.map +1 -0
- package/lib/types/DetailDocument.js.map +1 -0
- package/lib/types/DiffBlock.d.ts.map +1 -0
- package/lib/types/DiffBlock.js.map +1 -0
- package/lib/types/DisclosureRow.d.ts.map +1 -0
- package/lib/types/DisclosureRow.js.map +1 -0
- package/lib/types/DraggableImage.d.ts.map +1 -0
- package/lib/types/DraggableImage.js.map +1 -0
- package/lib/types/FileTypeIcon.d.ts.map +1 -0
- package/lib/types/FileTypeIcon.js.map +1 -0
- package/lib/types/FishLogo.d.ts.map +1 -0
- package/lib/types/FishLogo.js.map +1 -0
- package/lib/types/FoldToggle.d.ts.map +1 -0
- package/lib/types/FoldToggle.js.map +1 -0
- package/lib/types/HoverCard.d.ts.map +1 -0
- package/lib/types/HoverCard.js.map +1 -0
- package/lib/types/HoverCardGroup.d.ts.map +1 -0
- package/lib/types/HoverCardGroup.js.map +1 -0
- package/lib/types/ImageLightbox.d.ts.map +1 -0
- package/lib/types/ImageLightbox.js.map +1 -0
- package/lib/types/ImagePreview.d.ts.map +1 -0
- package/lib/types/ImagePreview.js.map +1 -0
- package/lib/types/InlineEditor.d.ts.map +1 -0
- package/lib/types/InlineEditor.js.map +1 -0
- package/lib/types/Input.d.ts.map +1 -0
- package/lib/types/Input.js.map +1 -0
- package/lib/types/JsonTree.d.ts.map +1 -0
- package/lib/types/JsonTree.js.map +1 -0
- package/lib/types/LinkIcon.d.ts.map +1 -0
- package/lib/types/LinkIcon.js.map +1 -0
- package/lib/types/Menu.d.ts +26 -14
- package/lib/types/Menu.d.ts.map +1 -0
- package/lib/types/Menu.js.map +1 -0
- package/lib/types/MenuGroup.d.ts.map +1 -0
- package/lib/types/MenuGroup.js.map +1 -0
- package/lib/types/MenuRow.d.ts +37 -0
- package/lib/types/MenuRow.d.ts.map +1 -0
- package/lib/types/MenuRow.js.map +1 -0
- package/lib/types/MenuSurface.d.ts.map +1 -0
- package/lib/types/MenuSurface.js.map +1 -0
- package/lib/types/Modal.d.ts +3 -1
- package/lib/types/Modal.d.ts.map +1 -0
- package/lib/types/Modal.js.map +1 -0
- package/lib/types/PathLabel.d.ts.map +1 -0
- package/lib/types/PathLabel.js.map +1 -0
- package/lib/types/PermissionIcon.d.ts.map +1 -0
- package/lib/types/PermissionIcon.js.map +1 -0
- package/lib/types/Pill.d.ts.map +1 -0
- package/lib/types/Pill.js.map +1 -0
- package/lib/types/QuotePreview.d.ts +17 -4
- package/lib/types/QuotePreview.d.ts.map +1 -0
- package/lib/types/QuotePreview.js.map +1 -0
- package/lib/types/ReadBlock.d.ts.map +1 -0
- package/lib/types/ReadBlock.js.map +1 -0
- package/lib/types/ReferenceIcon.d.ts.map +1 -0
- package/lib/types/ReferenceIcon.js.map +1 -0
- package/lib/types/RiskConfirmation.d.ts.map +1 -0
- package/lib/types/RiskConfirmation.js.map +1 -0
- package/lib/types/SearchBlock.d.ts.map +1 -0
- package/lib/types/SearchBlock.js.map +1 -0
- package/lib/types/SegmentedControl.d.ts.map +1 -0
- package/lib/types/SegmentedControl.js.map +1 -0
- package/lib/types/SegmentedTabs.d.ts.map +1 -0
- package/lib/types/SegmentedTabs.js.map +1 -0
- package/lib/types/ShortcutKeys.d.ts.map +1 -0
- package/lib/types/ShortcutKeys.js.map +1 -0
- package/lib/types/SiteGlyph.d.ts.map +1 -0
- package/lib/types/SiteGlyph.js.map +1 -0
- package/lib/types/StateDot.d.ts.map +1 -0
- package/lib/types/StateDot.js.map +1 -0
- package/lib/types/SubmenuPanel.d.ts +19 -0
- package/lib/types/SubmenuPanel.d.ts.map +1 -0
- package/lib/types/SubmenuPanel.js.map +1 -0
- package/lib/types/Switch.d.ts.map +1 -0
- package/lib/types/Switch.js.map +1 -0
- package/lib/types/Tag.d.ts.map +1 -0
- package/lib/types/Tag.js.map +1 -0
- package/lib/types/TerminalBlock.d.ts.map +1 -0
- package/lib/types/TerminalBlock.js.map +1 -0
- package/lib/types/TextShimmer.d.ts.map +1 -0
- package/lib/types/TextShimmer.js.map +1 -0
- package/lib/types/Toast.d.ts.map +1 -0
- package/lib/types/Toast.js.map +1 -0
- package/lib/types/Tooltip.d.ts.map +1 -0
- package/lib/types/Tooltip.js.map +1 -0
- package/lib/types/UserMessageBubble.d.ts.map +1 -0
- package/lib/types/UserMessageBubble.js.map +1 -0
- package/lib/types/WebBlock.d.ts.map +1 -0
- package/lib/types/WebBlock.js.map +1 -0
- package/lib/types/ZoomControls.d.ts.map +1 -0
- package/lib/types/ZoomControls.js.map +1 -0
- package/lib/types/ansi.d.ts.map +1 -0
- package/lib/types/ansi.js.map +1 -0
- package/lib/types/clipboard.d.ts.map +1 -0
- package/lib/types/clipboard.js.map +1 -0
- package/lib/types/code-file-icon-artwork.d.ts.map +1 -0
- package/lib/types/code-file-icon-artwork.js.map +1 -0
- package/lib/types/code-file-types.d.ts.map +1 -0
- package/lib/types/code-file-types.js.map +1 -0
- package/lib/types/code-highlighting.d.ts.map +1 -0
- package/lib/types/code-highlighting.js.map +1 -0
- package/lib/types/darwin-desktop.d.ts.map +1 -0
- package/lib/types/darwin-desktop.js.map +1 -0
- package/lib/types/document-zoom.d.ts.map +1 -0
- package/lib/types/document-zoom.js.map +1 -0
- package/lib/types/file-drag-actions.d.ts.map +1 -0
- package/lib/types/file-drag-actions.js.map +1 -0
- package/lib/types/file-size.d.ts.map +1 -0
- package/lib/types/file-size.js.map +1 -0
- package/lib/types/focus.d.ts.map +1 -0
- package/lib/types/focus.js.map +1 -0
- package/lib/types/guide-artwork.d.ts.map +1 -0
- package/lib/types/guide-artwork.js.map +1 -0
- package/lib/types/head-tail-cap.d.ts.map +1 -0
- package/lib/types/head-tail-cap.js.map +1 -0
- package/lib/types/icons/artwork.d.ts +12 -0
- package/lib/types/icons/artwork.d.ts.map +1 -0
- package/lib/types/icons/artwork.js.map +1 -0
- package/lib/types/icons/index.d.ts +12 -0
- package/lib/types/icons/index.d.ts.map +1 -0
- package/lib/types/icons/index.js.map +1 -0
- package/lib/types/icons/props.d.ts.map +1 -0
- package/lib/types/icons/props.js.map +1 -0
- package/lib/types/icons/shared-artwork.d.ts.map +1 -0
- package/lib/types/icons/shared-artwork.js.map +1 -0
- package/lib/types/image-drag.d.ts.map +1 -0
- package/lib/types/image-drag.js.map +1 -0
- package/lib/types/index.d.ts +5 -4
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/input-modality.d.ts.map +1 -0
- package/lib/types/input-modality.js.map +1 -0
- package/lib/types/keyboard-composition.d.ts.map +1 -0
- package/lib/types/keyboard-composition.js.map +1 -0
- package/lib/types/markdown/CodeBlock.d.ts +8 -1
- package/lib/types/markdown/CodeBlock.d.ts.map +1 -0
- package/lib/types/markdown/CodeBlock.js.map +1 -0
- package/lib/types/markdown/FenceBlocks.d.ts +31 -0
- package/lib/types/markdown/FenceBlocks.d.ts.map +1 -0
- package/lib/types/markdown/FenceBlocks.js.map +1 -0
- package/lib/types/markdown/JsonBlock.d.ts.map +1 -0
- package/lib/types/markdown/JsonBlock.js.map +1 -0
- package/lib/types/markdown/MarkdownDelegate.d.ts +23 -1
- package/lib/types/markdown/MarkdownDelegate.d.ts.map +1 -0
- package/lib/types/markdown/MarkdownDelegate.js.map +1 -0
- package/lib/types/markdown/MarkdownText.d.ts +2 -0
- package/lib/types/markdown/MarkdownText.d.ts.map +1 -0
- package/lib/types/markdown/MarkdownText.js.map +1 -0
- package/lib/types/markdown/cjkFriendlyStrong.d.ts.map +1 -0
- package/lib/types/markdown/cjkFriendlyStrong.js.map +1 -0
- package/lib/types/markdown/copy-tex.d.ts +14 -0
- package/lib/types/markdown/copy-tex.d.ts.map +1 -0
- package/lib/types/markdown/copy-tex.js.map +1 -0
- package/lib/types/markdown/diff-fence.d.ts +58 -0
- package/lib/types/markdown/diff-fence.d.ts.map +1 -0
- package/lib/types/markdown/diff-fence.js.map +1 -0
- package/lib/types/markdown/file-link.d.ts.map +1 -0
- package/lib/types/markdown/file-link.js.map +1 -0
- package/lib/types/markdown/highlight.d.ts +18 -0
- package/lib/types/markdown/highlight.d.ts.map +1 -0
- package/lib/types/markdown/highlight.js.map +1 -0
- package/lib/types/markdown/incremental.d.ts.map +1 -0
- package/lib/types/markdown/incremental.js.map +1 -0
- package/lib/types/markdown/katex.d.ts.map +1 -0
- package/lib/types/markdown/katex.js.map +1 -0
- package/lib/types/markdown/local-image-syntax.d.ts.map +1 -0
- package/lib/types/markdown/local-image-syntax.js.map +1 -0
- package/lib/types/markdown/mathCompatibility.d.ts.map +1 -0
- package/lib/types/markdown/mathCompatibility.js.map +1 -0
- package/lib/types/markdown/parse.d.ts.map +1 -0
- package/lib/types/markdown/parse.js.map +1 -0
- package/lib/types/markdown/plain-text.d.ts.map +1 -0
- package/lib/types/markdown/plain-text.js.map +1 -0
- package/lib/types/markdown/quote-text.d.ts.map +1 -0
- package/lib/types/markdown/quote-text.js.map +1 -0
- package/lib/types/markdown/render.d.ts.map +1 -0
- package/lib/types/markdown/render.js.map +1 -0
- package/lib/types/markdown/useViewportHighlighting.d.ts.map +1 -0
- package/lib/types/markdown/useViewportHighlighting.js.map +1 -0
- package/lib/types/markdown/windows-path-destinations.d.ts.map +1 -0
- package/lib/types/markdown/windows-path-destinations.js.map +1 -0
- package/lib/types/menu-submenu-position.d.ts +15 -0
- package/lib/types/menu-submenu-position.d.ts.map +1 -0
- package/lib/types/menu-submenu-position.js.map +1 -0
- package/lib/types/overlay-top-margin.d.ts.map +1 -0
- package/lib/types/overlay-top-margin.js.map +1 -0
- package/lib/types/plugin-artwork.d.ts.map +1 -0
- package/lib/types/plugin-artwork.js.map +1 -0
- package/lib/types/pointer-grace.d.ts.map +1 -0
- package/lib/types/pointer-grace.js.map +1 -0
- package/lib/types/rank-by-name.d.ts.map +1 -0
- package/lib/types/rank-by-name.js.map +1 -0
- package/lib/types/relative-time.d.ts.map +1 -0
- package/lib/types/relative-time.js.map +1 -0
- package/lib/types/settings-form/SettingsForm.d.ts.map +1 -0
- package/lib/types/settings-form/SettingsForm.js.map +1 -0
- package/lib/types/settings-form/fields.d.ts.map +1 -0
- package/lib/types/settings-form/fields.js.map +1 -0
- package/lib/types/settings-form/form-model.d.ts.map +1 -0
- package/lib/types/settings-form/form-model.js.map +1 -0
- package/lib/types/tooltip-placement.d.ts.map +1 -0
- package/lib/types/tooltip-placement.js.map +1 -0
- package/lib/types/use-clipboard-copy.d.ts.map +1 -0
- package/lib/types/use-clipboard-copy.js.map +1 -0
- package/lib/types/useAnchoredMaxHeight.d.ts.map +1 -0
- package/lib/types/useAnchoredMaxHeight.js.map +1 -0
- package/lib/types/useDismissOnOutsidePointer.d.ts.map +1 -0
- package/lib/types/useDismissOnOutsidePointer.js.map +1 -0
- package/lib/types/useMenuSubmenu.d.ts +43 -0
- package/lib/types/useMenuSubmenu.d.ts.map +1 -0
- package/lib/types/useMenuSubmenu.js.map +1 -0
- package/lib/types/useModalLayer.d.ts.map +1 -0
- package/lib/types/useModalLayer.js.map +1 -0
- package/lib/types/useRowDragPreview.d.ts.map +1 -0
- package/lib/types/useRowDragPreview.js.map +1 -0
- package/lib/types/user-text.d.ts.map +1 -0
- package/lib/types/user-text.js.map +1 -0
- package/package.json +9 -6
- package/src/AnimatedRows.module.css +7 -0
- package/src/AnimatedRows.tsx +195 -0
- package/src/BrandWordmark.tsx +59 -0
- package/src/Button.module.css +84 -0
- package/src/Button.tsx +36 -0
- package/src/Checkbox.module.css +28 -0
- package/src/Checkbox.tsx +34 -0
- package/src/CodeCard.module.css +85 -0
- package/src/CodeFileIcon.tsx +28 -0
- package/src/CodeToolbar.tsx +62 -0
- package/src/CommandText.module.css +12 -0
- package/src/CommandText.tsx +46 -0
- package/src/ConfigField.module.css +12 -0
- package/src/ConfigField.tsx +53 -0
- package/src/ConnectionIndicator.module.css +120 -0
- package/src/ConnectionIndicator.tsx +103 -0
- package/src/DetailDocument.module.css +67 -0
- package/src/DetailDocument.tsx +120 -0
- package/src/DiffBlock.module.css +82 -0
- package/src/DiffBlock.tsx +221 -0
- package/src/DisclosureRow.module.css +115 -0
- package/src/DisclosureRow.tsx +124 -0
- package/src/DraggableImage.tsx +26 -0
- package/src/FileTypeIcon.module.css +50 -0
- package/src/FileTypeIcon.tsx +285 -0
- package/src/FishLogo.tsx +28 -0
- package/src/FoldToggle.tsx +33 -0
- package/src/HoverCard.module.css +108 -0
- package/src/HoverCard.tsx +356 -0
- package/src/HoverCardGroup.tsx +77 -0
- package/src/ImageLightbox.module.css +54 -0
- package/src/ImageLightbox.tsx +72 -0
- package/src/ImagePreview.module.css +24 -0
- package/src/ImagePreview.tsx +36 -0
- package/src/InlineEditor.module.css +23 -0
- package/src/InlineEditor.tsx +69 -0
- package/src/Input.module.css +38 -0
- package/src/Input.tsx +24 -0
- package/src/JsonTree.module.css +371 -0
- package/src/JsonTree.tsx +850 -0
- package/src/LinkIcon.tsx +118 -0
- package/src/Menu.module.css +443 -0
- package/src/Menu.tsx +553 -0
- package/src/MenuGroup.module.css +37 -0
- package/src/MenuGroup.tsx +103 -0
- package/src/MenuRow.tsx +89 -0
- package/src/MenuSurface.module.css +65 -0
- package/src/MenuSurface.tsx +54 -0
- package/src/Modal.module.css +147 -0
- package/src/Modal.tsx +96 -0
- package/src/PathLabel.module.css +28 -0
- package/src/PathLabel.tsx +41 -0
- package/src/PermissionIcon.tsx +119 -0
- package/src/Pill.module.css +30 -0
- package/src/Pill.tsx +36 -0
- package/src/QuotePreview.module.css +138 -0
- package/src/QuotePreview.tsx +133 -0
- package/src/ReadBlock.module.css +45 -0
- package/src/ReadBlock.tsx +151 -0
- package/src/ReferenceIcon.tsx +49 -0
- package/src/RiskConfirmation.module.css +47 -0
- package/src/RiskConfirmation.tsx +79 -0
- package/src/RowDragPreview.module.css +12 -0
- package/src/SearchBlock.module.css +114 -0
- package/src/SearchBlock.tsx +281 -0
- package/src/SegmentedControl.module.css +81 -0
- package/src/SegmentedControl.tsx +140 -0
- package/src/SegmentedTabs.module.css +77 -0
- package/src/SegmentedTabs.tsx +84 -0
- package/src/ShortcutKeys.module.css +33 -0
- package/src/ShortcutKeys.tsx +12 -0
- package/src/SiteGlyph.tsx +149 -0
- package/src/StateDot.module.css +100 -0
- package/src/StateDot.tsx +77 -0
- package/src/SubmenuPanel.tsx +51 -0
- package/src/Switch.module.css +49 -0
- package/src/Switch.tsx +42 -0
- package/src/Tag.module.css +57 -0
- package/src/Tag.tsx +42 -0
- package/src/TerminalBlock.module.css +207 -0
- package/src/TerminalBlock.tsx +294 -0
- package/src/TextShimmer.module.css +88 -0
- package/src/TextShimmer.tsx +58 -0
- package/src/Toast.module.css +102 -0
- package/src/Toast.tsx +101 -0
- package/src/Tooltip.module.css +68 -0
- package/src/Tooltip.tsx +273 -0
- package/src/UserMessageBubble.module.css +11 -0
- package/src/UserMessageBubble.tsx +16 -0
- package/src/WebBlock.module.css +153 -0
- package/src/WebBlock.tsx +196 -0
- package/src/ZoomControls.module.css +54 -0
- package/src/ZoomControls.tsx +92 -0
- package/src/ansi.ts +447 -0
- package/src/clipboard.ts +47 -0
- package/src/code-file-icon-artwork.manifest.json +71 -0
- package/src/code-file-icon-artwork.ts +60 -0
- package/src/code-file-types.ts +340 -0
- package/src/code-highlighting.ts +25 -0
- package/src/css-modules.d.ts +6 -0
- package/src/darwin-desktop.ts +11 -0
- package/src/document-zoom.ts +12 -0
- package/src/file-drag-actions.ts +29 -0
- package/src/file-size.ts +16 -0
- package/src/focus.ts +44 -0
- package/src/guide-artwork.tsx +32 -0
- package/src/head-tail-cap.ts +27 -0
- package/src/icons/artwork.ts +156 -0
- package/src/icons/index.tsx +634 -0
- package/src/icons/props.ts +8 -0
- package/src/icons/shared-artwork.tsx +71 -0
- package/src/image-drag.ts +84 -0
- package/src/index.ts +142 -0
- package/src/input-modality.ts +59 -0
- package/src/keyboard-composition.ts +36 -0
- package/src/markdown/CodeBlock.module.css +209 -0
- package/src/markdown/CodeBlock.tsx +268 -0
- package/src/markdown/FenceBlocks.tsx +51 -0
- package/src/markdown/JsonBlock.module.css +34 -0
- package/src/markdown/JsonBlock.tsx +36 -0
- package/src/markdown/MarkdownDelegate.tsx +102 -0
- package/src/markdown/MarkdownText.module.css +511 -0
- package/src/markdown/MarkdownText.tsx +218 -0
- package/src/markdown/cjkFriendlyStrong.ts +83 -0
- package/src/markdown/copy-tex.ts +53 -0
- package/src/markdown/diff-fence.ts +212 -0
- package/src/markdown/file-link.ts +45 -0
- package/src/markdown/highlight.ts +689 -0
- package/src/markdown/incremental.ts +360 -0
- package/src/markdown/katex.tsx +90 -0
- package/src/markdown/local-image-syntax.ts +29 -0
- package/src/markdown/mathCompatibility.ts +349 -0
- package/src/markdown/parse.ts +49 -0
- package/src/markdown/plain-text.ts +121 -0
- package/src/markdown/quote-text.ts +10 -0
- package/src/markdown/render.tsx +834 -0
- package/src/markdown/useViewportHighlighting.ts +72 -0
- package/src/markdown/windows-path-destinations.ts +34 -0
- package/src/menu-submenu-position.ts +20 -0
- package/src/overlay-top-margin.ts +18 -0
- package/src/plugin-artwork.tsx +110 -0
- package/src/pointer-grace.ts +47 -0
- package/src/rank-by-name.ts +93 -0
- package/src/relative-time.ts +36 -0
- package/src/settings-form/SettingsForm.module.css +53 -0
- package/src/settings-form/SettingsForm.tsx +74 -0
- package/src/settings-form/fields.module.css +153 -0
- package/src/settings-form/fields.tsx +151 -0
- package/src/settings-form/form-model.ts +393 -0
- package/src/tooltip-placement.ts +60 -0
- package/src/use-clipboard-copy.ts +44 -0
- package/src/useAnchoredMaxHeight.ts +45 -0
- package/src/useDismissOnOutsidePointer.ts +34 -0
- package/src/useMenuSubmenu.ts +182 -0
- package/src/useModalLayer.ts +108 -0
- package/src/useRowDragPreview.ts +76 -0
- package/src/user-text.module.css +46 -0
- package/src/user-text.tsx +67 -0
- 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
|
+
}
|