@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,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Untrusted assistant-Markdown renderer over the direct mdast pipeline:
|
|
3
|
+
* `parse.ts` grammars, the incremental streaming parser, and `render.tsx`.
|
|
4
|
+
* While a message streams, all but the trailing two blocks freeze as cached
|
|
5
|
+
* React elements and only the source tail behind them re-parses per chunk,
|
|
6
|
+
* so per-chunk work tracks the tail size instead of the whole reply. Frozen
|
|
7
|
+
* blocks keep their source-offset keys when they cross the freeze boundary,
|
|
8
|
+
* so React reconciles instead of remounting. Known deviation while
|
|
9
|
+
* streaming: a reference-style link or footnote whose definition sits on the
|
|
10
|
+
* other side of the freeze boundary renders literally until the settled
|
|
11
|
+
* full parse self-heals it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { memo, useMemo, useRef } from 'react'
|
|
15
|
+
import type { ReactNode } from 'react'
|
|
16
|
+
import clsx from 'clsx'
|
|
17
|
+
import { copyKatexAsTex } from './copy-tex.ts'
|
|
18
|
+
import { IncrementalMarkdownParser } from './incremental.ts'
|
|
19
|
+
import { parseGfm, parseGfmWithMath } from './parse.ts'
|
|
20
|
+
import {
|
|
21
|
+
collectReferenceTargets, createReferenceTargets, renderBlocks, renderFootnoteSection,
|
|
22
|
+
wrapBlockChildren,
|
|
23
|
+
} from './render.tsx'
|
|
24
|
+
import type { MarkdownFileMentions, MarkdownLabels, MarkdownPathImages, MarkdownRenderContext, ReferenceTargets } from './render.tsx'
|
|
25
|
+
import 'katex/dist/katex.min.css'
|
|
26
|
+
import css from './MarkdownText.module.css'
|
|
27
|
+
|
|
28
|
+
export type { MarkdownCodeLabels, MarkdownFileMentions, MarkdownLabels, MarkdownPathImages } from './render.tsx'
|
|
29
|
+
|
|
30
|
+
/** One settled full render: parse with math, resolve references, append the footnote section. */
|
|
31
|
+
function renderSettled(
|
|
32
|
+
text: string,
|
|
33
|
+
labels: MarkdownLabels,
|
|
34
|
+
fileMentions: MarkdownFileMentions | undefined,
|
|
35
|
+
pathImages: MarkdownPathImages | undefined,
|
|
36
|
+
omitHtml: boolean,
|
|
37
|
+
): ReactNode[] {
|
|
38
|
+
const root = parseGfmWithMath(text)
|
|
39
|
+
const targets = createReferenceTargets()
|
|
40
|
+
collectReferenceTargets(root.children, targets)
|
|
41
|
+
const context: MarkdownRenderContext = {
|
|
42
|
+
streaming: false,
|
|
43
|
+
labels,
|
|
44
|
+
fileMentions,
|
|
45
|
+
pathImages,
|
|
46
|
+
omitHtml,
|
|
47
|
+
targets,
|
|
48
|
+
footnoteOrder: [],
|
|
49
|
+
footnoteCounts: new Map(),
|
|
50
|
+
}
|
|
51
|
+
const blocks = wrapBlockChildren(
|
|
52
|
+
renderBlocks(root.children.map((node, index) => ({
|
|
53
|
+
node,
|
|
54
|
+
/* v8 ignore next -- parseFull uses parseGfm, which stamps every top-level node. */
|
|
55
|
+
key: node.position?.start.offset ?? -(index + 1),
|
|
56
|
+
})), context),
|
|
57
|
+
false,
|
|
58
|
+
)
|
|
59
|
+
const section = renderFootnoteSection(context)
|
|
60
|
+
return section === null ? blocks : [...blocks, '\n', section]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Streaming render state for one growing message: the incremental parser,
|
|
65
|
+
* the frozen blocks' cached elements, and the reference/footnote state their
|
|
66
|
+
* rendering consumed (footnote numbering assigned to frozen references is
|
|
67
|
+
* final, so the tail continues from a copy of it each frame).
|
|
68
|
+
*/
|
|
69
|
+
class StreamingRenderer {
|
|
70
|
+
private readonly parser = new IncrementalMarkdownParser(parseGfm)
|
|
71
|
+
private generation = -1
|
|
72
|
+
private frozenCount = 0
|
|
73
|
+
private frozenElements: ReactNode[] = []
|
|
74
|
+
private frozenTargets: ReferenceTargets = createReferenceTargets()
|
|
75
|
+
private frozenFootnoteOrder: string[] = []
|
|
76
|
+
private frozenFootnoteCounts = new Map<string, number>()
|
|
77
|
+
private lastText: string | null = null
|
|
78
|
+
private lastRendered: ReactNode[] = []
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param labels - Localized Markdown chrome baked into cached elements; the owner replaces the renderer when it changes.
|
|
82
|
+
* @param omitHtml - Whether raw HTML renders nothing; the owner replaces the renderer when it changes.
|
|
83
|
+
*/
|
|
84
|
+
constructor(private readonly labels: MarkdownLabels, readonly omitHtml: boolean) {}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Render the current accumulated text. Idempotent per text value, so React
|
|
88
|
+
* may re-execute the calling render freely.
|
|
89
|
+
* @param text - The full accumulated markdown source.
|
|
90
|
+
* @returns Frozen elements, re-rendered tail, and the footnote section.
|
|
91
|
+
*/
|
|
92
|
+
render(text: string): ReactNode[] {
|
|
93
|
+
if (text === this.lastText) return this.lastRendered
|
|
94
|
+
const { frozen, tail, generation } = this.parser.update(text)
|
|
95
|
+
if (generation !== this.generation) {
|
|
96
|
+
this.generation = generation
|
|
97
|
+
this.frozenCount = 0
|
|
98
|
+
this.frozenElements = []
|
|
99
|
+
this.frozenTargets = createReferenceTargets()
|
|
100
|
+
this.frozenFootnoteOrder = []
|
|
101
|
+
this.frozenFootnoteCounts = new Map()
|
|
102
|
+
}
|
|
103
|
+
const newlyFrozen = frozen.slice(this.frozenCount)
|
|
104
|
+
collectReferenceTargets(newlyFrozen.map(block => block.node), this.frozenTargets)
|
|
105
|
+
// Targets visible this frame: everything frozen so far plus the current
|
|
106
|
+
// tail parse — a newly frozen block's references resolved against the
|
|
107
|
+
// same parse tree its definitions came from.
|
|
108
|
+
const frameTargets: ReferenceTargets = {
|
|
109
|
+
definitions: new Map(this.frozenTargets.definitions),
|
|
110
|
+
footnotes: new Map(this.frozenTargets.footnotes),
|
|
111
|
+
}
|
|
112
|
+
collectReferenceTargets(tail.map(block => block.node), frameTargets)
|
|
113
|
+
if (newlyFrozen.length > 0) {
|
|
114
|
+
const frozenContext: MarkdownRenderContext = {
|
|
115
|
+
streaming: true,
|
|
116
|
+
labels: this.labels,
|
|
117
|
+
fileMentions: undefined,
|
|
118
|
+
pathImages: undefined,
|
|
119
|
+
omitHtml: this.omitHtml,
|
|
120
|
+
targets: frameTargets,
|
|
121
|
+
footnoteOrder: this.frozenFootnoteOrder,
|
|
122
|
+
footnoteCounts: this.frozenFootnoteCounts,
|
|
123
|
+
}
|
|
124
|
+
// Separator newlines are cached alongside the elements so the
|
|
125
|
+
// assembled children match the settled pipeline's block wrapping.
|
|
126
|
+
const batch = [...this.frozenElements]
|
|
127
|
+
for (const element of renderBlocks(newlyFrozen, frozenContext)) {
|
|
128
|
+
if (batch.length > 0) batch.push('\n')
|
|
129
|
+
batch.push(element)
|
|
130
|
+
}
|
|
131
|
+
this.frozenElements = batch
|
|
132
|
+
this.frozenCount = frozen.length
|
|
133
|
+
}
|
|
134
|
+
const tailContext: MarkdownRenderContext = {
|
|
135
|
+
streaming: true,
|
|
136
|
+
labels: this.labels,
|
|
137
|
+
fileMentions: undefined,
|
|
138
|
+
pathImages: undefined,
|
|
139
|
+
omitHtml: this.omitHtml,
|
|
140
|
+
targets: frameTargets,
|
|
141
|
+
footnoteOrder: [...this.frozenFootnoteOrder],
|
|
142
|
+
footnoteCounts: new Map(this.frozenFootnoteCounts),
|
|
143
|
+
}
|
|
144
|
+
const children = [...this.frozenElements]
|
|
145
|
+
for (const element of renderBlocks(tail, tailContext)) {
|
|
146
|
+
if (children.length > 0) children.push('\n')
|
|
147
|
+
children.push(element)
|
|
148
|
+
}
|
|
149
|
+
const section = renderFootnoteSection(tailContext)
|
|
150
|
+
if (section !== null) children.push('\n', section)
|
|
151
|
+
this.lastText = text
|
|
152
|
+
this.lastRendered = children
|
|
153
|
+
return this.lastRendered
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Render untrusted assistant-authored Markdown as semantic React elements.
|
|
159
|
+
* @param props - Markdown source text preserved by the session projection;
|
|
160
|
+
* `streaming` parses incrementally across chunks and highlights fences as
|
|
161
|
+
* they grow (each fence re-tokenizes only appended text; TeX stays literal
|
|
162
|
+
* until the finalize swap so incomplete formulae never flash errors);
|
|
163
|
+
* `labels` forwards localized fence and footnote chrome — pass a
|
|
164
|
+
* reference-stable object (memoized per locale revision), because a new
|
|
165
|
+
* identity discards the streaming render cache mid-message. `fileMentions`
|
|
166
|
+
* links inline-code tokens its resolver recognizes as real files, and
|
|
167
|
+
* `pathImages` rewrites image destinations that are local file paths into
|
|
168
|
+
* displayable URLs its resolver vouches for. Those two vocabularies are the
|
|
169
|
+
* single streaming gate — they apply to settled renders only, because a
|
|
170
|
+
* streaming message's vocabulary is not final and frozen cached elements
|
|
171
|
+
* must not bake in handlers that could go stale. A surrounding
|
|
172
|
+
* `MarkdownDelegateProvider` can delegate ordinary HTTP(S) activation while
|
|
173
|
+
* modified clicks retain native behavior. `variant="compact"` uses secondary
|
|
174
|
+
* text sizing, uniform bold headings, and tight block spacing; the default
|
|
175
|
+
* `body` variant uses the full document typography. `omitHtml` renders raw
|
|
176
|
+
* HTML as nothing instead of literal text, for documents written for HTML
|
|
177
|
+
* renderers, such as package READMEs. `textInteractionSource` marks the
|
|
178
|
+
* existing root as a source for text selection interactions.
|
|
179
|
+
* The provider's `openFile` enables local Markdown links in settled messages,
|
|
180
|
+
* including `#L24` / `#L24-L30` fragments and `:24` / `:24-30` / `:24:5` suffixes
|
|
181
|
+
* (ranges open at their first line, columns are ignored, and invalid locations
|
|
182
|
+
* open only the file; see the README for URL/path classification).
|
|
183
|
+
* Copying a selection inside the document writes selected formulas as
|
|
184
|
+
* `$…$` / `$$…$$` TeX source.
|
|
185
|
+
* @returns A GFM document with TeX math rendered through KaTeX; raw HTML never
|
|
186
|
+
* renders as markup, and unsafe protocols are disabled. Local links without an opener remain text;
|
|
187
|
+
* absolute HTTP(S) images render directly.
|
|
188
|
+
*/
|
|
189
|
+
export const MarkdownText = memo(function MarkdownText({
|
|
190
|
+
text, streaming = false, labels, fileMentions, pathImages, variant = 'body', omitHtml = false, textInteractionSource = false,
|
|
191
|
+
}: {
|
|
192
|
+
text: string
|
|
193
|
+
streaming?: boolean
|
|
194
|
+
labels: MarkdownLabels
|
|
195
|
+
fileMentions?: MarkdownFileMentions | undefined
|
|
196
|
+
pathImages?: MarkdownPathImages | undefined
|
|
197
|
+
variant?: 'body' | 'compact'
|
|
198
|
+
omitHtml?: boolean
|
|
199
|
+
textInteractionSource?: boolean
|
|
200
|
+
}) {
|
|
201
|
+
const streamRef = useRef<StreamingRenderer | null>(null)
|
|
202
|
+
const streamLabelsRef = useRef<MarkdownLabels>(labels)
|
|
203
|
+
const children = useMemo(() => {
|
|
204
|
+
if (!streaming) {
|
|
205
|
+
streamRef.current = null
|
|
206
|
+
return renderSettled(text, labels, fileMentions, pathImages, omitHtml)
|
|
207
|
+
}
|
|
208
|
+
if (streamRef.current === null || streamLabelsRef.current !== labels || streamRef.current.omitHtml !== omitHtml) {
|
|
209
|
+
streamRef.current = new StreamingRenderer(labels, omitHtml)
|
|
210
|
+
streamLabelsRef.current = labels
|
|
211
|
+
}
|
|
212
|
+
return streamRef.current.render(text)
|
|
213
|
+
}, [text, streaming, labels, fileMentions, pathImages, omitHtml])
|
|
214
|
+
return <div className={clsx(css.markdown, variant === 'compact' && css.compact)}
|
|
215
|
+
data-text-interaction-source={textInteractionSource ? '' : undefined}
|
|
216
|
+
data-markdown-variant={variant === 'compact' ? variant : undefined}
|
|
217
|
+
onCopy={(event) => { copyKatexAsTex(event.nativeEvent, event.currentTarget) }}>{children}</div>
|
|
218
|
+
})
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** Let asterisk strong emphasis close after punctuation when CJK prose continues without whitespace. */
|
|
2
|
+
|
|
3
|
+
import { attention } from 'micromark-core-commonmark'
|
|
4
|
+
import { unicodePunctuation } from 'micromark-util-character'
|
|
5
|
+
import { classifyCharacter } from 'micromark-util-classify-character'
|
|
6
|
+
import { codes, constants } from 'micromark-util-symbol'
|
|
7
|
+
import type { Construct, Extension, State, Tokenizer } from 'micromark-util-types'
|
|
8
|
+
|
|
9
|
+
const cjkCharacter = new RegExp([
|
|
10
|
+
'\\p{Script_Extensions=Han}',
|
|
11
|
+
'\\p{Script_Extensions=Hiragana}',
|
|
12
|
+
'\\p{Script_Extensions=Katakana}',
|
|
13
|
+
'\\p{Script_Extensions=Hangul}',
|
|
14
|
+
'\\p{Script_Extensions=Bopomofo}',
|
|
15
|
+
].join('|'), 'u')
|
|
16
|
+
|
|
17
|
+
function isCjkCharacter(code: number | null): boolean {
|
|
18
|
+
return code !== null && code >= 0 && cjkCharacter.test(String.fromCodePoint(code))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const tokenizeCjkFriendlyAttention: Tokenizer = function (effects, ok, nok) {
|
|
22
|
+
const configuredAttentionMarkers = this.parser.constructs.attentionMarkers.null
|
|
23
|
+
if (configuredAttentionMarkers === undefined) {
|
|
24
|
+
throw new Error('micromark CommonMark attention markers are unavailable')
|
|
25
|
+
}
|
|
26
|
+
const attentionMarkers = configuredAttentionMarkers
|
|
27
|
+
const previous = this.previous
|
|
28
|
+
const before = classifyCharacter(previous)
|
|
29
|
+
let marker: number | null = codes.eof
|
|
30
|
+
|
|
31
|
+
return start
|
|
32
|
+
|
|
33
|
+
function start(code: number | null): State | undefined {
|
|
34
|
+
/* v8 ignore next -- this text construct is dispatched only for an asterisk. */
|
|
35
|
+
if (code !== codes.asterisk) return nok(code)
|
|
36
|
+
marker = code
|
|
37
|
+
effects.enter('attentionSequence')
|
|
38
|
+
return inside(code)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function inside(code: number | null): State | undefined {
|
|
42
|
+
if (code === marker) {
|
|
43
|
+
effects.consume(code)
|
|
44
|
+
return inside
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const token = effects.exit('attentionSequence')
|
|
48
|
+
const after = classifyCharacter(code)
|
|
49
|
+
const open = !after || (after === constants.characterGroupPunctuation && Boolean(before))
|
|
50
|
+
|| attentionMarkers.includes(code)
|
|
51
|
+
const commonMarkClose = !before
|
|
52
|
+
|| (before === constants.characterGroupPunctuation && Boolean(after))
|
|
53
|
+
|| attentionMarkers.includes(previous)
|
|
54
|
+
const markerCount = token.end.offset - token.start.offset
|
|
55
|
+
const cjkStrongClose = markerCount >= 2
|
|
56
|
+
&& unicodePunctuation(previous)
|
|
57
|
+
&& isCjkCharacter(code)
|
|
58
|
+
const close = commonMarkClose || cjkStrongClose
|
|
59
|
+
|
|
60
|
+
token._open = open
|
|
61
|
+
token._close = close
|
|
62
|
+
return ok(code)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const cjkFriendlyAttention: Construct = {
|
|
67
|
+
name: 'cjkFriendlyAttention',
|
|
68
|
+
resolveAll: attention.resolveAll,
|
|
69
|
+
tokenize: tokenizeCjkFriendlyAttention,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const cjkFriendlyStrongExtension: Extension = {
|
|
73
|
+
text: { [codes.asterisk]: cjkFriendlyAttention },
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Extend CommonMark asterisk strong emphasis for punctuation-delimited CJK
|
|
78
|
+
* prose, as a micromark syntax extension for `fromMarkdown`.
|
|
79
|
+
* @returns The micromark syntax extension.
|
|
80
|
+
*/
|
|
81
|
+
export function cjkFriendlyStrong(): Extension {
|
|
82
|
+
return cjkFriendlyStrongExtension
|
|
83
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Clipboard projection that copies rendered KaTeX formulas as their TeX source. */
|
|
2
|
+
|
|
3
|
+
/** The whole formula around a node, including the display wrapper that marks display math. */
|
|
4
|
+
function closestFormula(node: Node): Element | null {
|
|
5
|
+
const element = node instanceof Element ? node : node.parentElement
|
|
6
|
+
return element?.closest('.katex-display') ?? element?.closest('.katex') ?? null
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Rewrite a copy whose selection lies inside one rendered Markdown document and
|
|
11
|
+
* includes rendered math, so each formula becomes its trimmed TeX source,
|
|
12
|
+
* delimited as `$…$` inline and `$$…$$` for display math, in both the
|
|
13
|
+
* plain-text and HTML clipboard data. A selection endpoint inside a formula
|
|
14
|
+
* extends to the whole formula in the clipboard only; the visible selection is
|
|
15
|
+
* unchanged. A copy without math, a selection reaching outside `root`, and a
|
|
16
|
+
* copy whose default an inner listener already prevented keep their data.
|
|
17
|
+
* @param event - Native copy event.
|
|
18
|
+
* @param root - The rendered Markdown document root.
|
|
19
|
+
*/
|
|
20
|
+
export function copyKatexAsTex(event: ClipboardEvent, root: HTMLElement): void {
|
|
21
|
+
const selection = root.ownerDocument.getSelection()
|
|
22
|
+
if (event.defaultPrevented || event.clipboardData === null || selection === null
|
|
23
|
+
|| selection.rangeCount !== 1 || selection.isCollapsed) return
|
|
24
|
+
const range = selection.getRangeAt(0).cloneRange()
|
|
25
|
+
if (!root.contains(range.commonAncestorContainer)) return
|
|
26
|
+
const startFormula = closestFormula(range.startContainer)
|
|
27
|
+
if (startFormula !== null) range.setStartBefore(startFormula)
|
|
28
|
+
const endFormula = closestFormula(range.endContainer)
|
|
29
|
+
if (endFormula !== null) range.setEndAfter(endFormula)
|
|
30
|
+
const fragment = range.cloneContents()
|
|
31
|
+
const formulas = [...fragment.querySelectorAll('.katex')]
|
|
32
|
+
if (formulas.length === 0) return
|
|
33
|
+
for (const formula of formulas) {
|
|
34
|
+
const tex = formula.querySelector('annotation[encoding="application/x-tex"]')?.textContent.trim()
|
|
35
|
+
if (tex === undefined) continue
|
|
36
|
+
const delimiter = formula.closest('.katex-display') === null ? '$' : '$$'
|
|
37
|
+
formula.replaceWith(`${delimiter}${tex}${delimiter}`)
|
|
38
|
+
}
|
|
39
|
+
// innerText applies the browser's own block and line-break serialization,
|
|
40
|
+
// which needs a rendered element; an off-screen child of the root keeps the
|
|
41
|
+
// Markdown styles that decide block layout while staying invisible.
|
|
42
|
+
const container = root.ownerDocument.createElement('div')
|
|
43
|
+
container.style.cssText = 'position:fixed;top:0;left:-100000px;pointer-events:none'
|
|
44
|
+
container.append(fragment)
|
|
45
|
+
root.append(container)
|
|
46
|
+
try {
|
|
47
|
+
event.clipboardData.setData('text/plain', container.innerText)
|
|
48
|
+
event.clipboardData.setData('text/html', container.innerHTML)
|
|
49
|
+
} finally {
|
|
50
|
+
container.remove()
|
|
51
|
+
}
|
|
52
|
+
event.preventDefault()
|
|
53
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/** Unified-diff row parsing and per-file body tokenization for markdown fences. @module dsh-client-ui-primitives/markdown/diff-fence */
|
|
2
|
+
|
|
3
|
+
import { languageForPath } from '../code-highlighting.ts'
|
|
4
|
+
import { highlightLines } from './highlight.ts'
|
|
5
|
+
import type { HighlightSpan } from './highlight.ts'
|
|
6
|
+
|
|
7
|
+
/** Fence info strings rendered as a unified diff instead of grammar-highlighted code. */
|
|
8
|
+
export const DIFF_FENCE_LANGUAGES = new Set(['diff', 'patch'])
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether a fence info string selects diff row rendering.
|
|
12
|
+
* @param lang - the fence info string, case-insensitively compared.
|
|
13
|
+
* @returns whether the fence is a unified diff.
|
|
14
|
+
*/
|
|
15
|
+
export function isDiffFenceLanguage(lang: string | undefined): boolean {
|
|
16
|
+
return lang !== undefined && DIFF_FENCE_LANGUAGES.has(lang.toLowerCase())
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** One unified-diff line, split so the marker column stays out of the tokenized body. */
|
|
20
|
+
export interface DiffRow {
|
|
21
|
+
/** Row role, which selects the line's diff class. */
|
|
22
|
+
kind: 'add' | 'del' | 'context' | 'hunk' | 'meta'
|
|
23
|
+
/** The marker column, kept verbatim so copy reproduces the source. */
|
|
24
|
+
marker: string
|
|
25
|
+
/** The code after the marker column, or the whole line for chrome rows. */
|
|
26
|
+
body: string
|
|
27
|
+
/** The full source line, rendered as-is for hunk and meta rows. */
|
|
28
|
+
raw: string
|
|
29
|
+
/** Index of the file section this row belongs to; -1 before any file header. */
|
|
30
|
+
file: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Parsed fence: rows plus the language each file section names. */
|
|
34
|
+
export interface DiffFence {
|
|
35
|
+
/** The fence rows in source order. */
|
|
36
|
+
rows: DiffRow[]
|
|
37
|
+
/** Inner language per file section, when the header names a known one. */
|
|
38
|
+
languages: ReadonlyMap<number, string>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** File-level chrome a unified diff carries above and between hunks. */
|
|
42
|
+
const DIFF_META_PREFIXES = [
|
|
43
|
+
'diff --git ', 'index ', 'old mode ', 'new mode ', 'new file mode ', 'deleted file mode ',
|
|
44
|
+
'similarity index ', 'dissimilarity index ', 'rename from ', 'rename to ', 'copy from ', 'copy to ',
|
|
45
|
+
'Binary files ', 'GIT binary patch', '\\ No newline',
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
/** Hunk header with both line counts; a missing count means one line. */
|
|
49
|
+
const HUNK_HEADER = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/u
|
|
50
|
+
|
|
51
|
+
/** Lines the hunk header says remain on each side. */
|
|
52
|
+
interface HunkRemainder {
|
|
53
|
+
/** Unconsumed old-side lines. */
|
|
54
|
+
old: number
|
|
55
|
+
/** Unconsumed new-side lines. */
|
|
56
|
+
new: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Parse a hunk header's line budget.
|
|
61
|
+
* @param raw - the `@@ -a,b +c,d @@` line.
|
|
62
|
+
* @returns the remaining line counts, or `undefined` for a malformed header.
|
|
63
|
+
*/
|
|
64
|
+
function hunkRemainder(raw: string): HunkRemainder | undefined {
|
|
65
|
+
const match = HUNK_HEADER.exec(raw)
|
|
66
|
+
if (match === null) return undefined
|
|
67
|
+
return {
|
|
68
|
+
old: match[2] === undefined ? 1 : Number(match[2]),
|
|
69
|
+
new: match[4] === undefined ? 1 : Number(match[4]),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Whether a raw line is file-level chrome rather than hunk content. */
|
|
74
|
+
function isDiffMeta(raw: string): boolean {
|
|
75
|
+
return DIFF_META_PREFIXES.some(prefix => raw.startsWith(prefix))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Resolve the inner language a `---`/`+++` header names.
|
|
80
|
+
* @param raw - the header line.
|
|
81
|
+
* @returns the language id, or `undefined` for `/dev/null` or an unknown path.
|
|
82
|
+
*/
|
|
83
|
+
function headerLanguage(raw: string): string | undefined {
|
|
84
|
+
const path = raw.slice(4).replace(/\t.*$/u, '').trim()
|
|
85
|
+
const unquoted = path.startsWith('"') && path.endsWith('"')
|
|
86
|
+
? path.slice(1, -1).replace(/\\(["\\])/gu, '$1')
|
|
87
|
+
: path
|
|
88
|
+
if (unquoted === '' || unquoted === '/dev/null') return undefined
|
|
89
|
+
return languageForPath(unquoted.replace(/^[ab]\//u, ''))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Split a unified diff into rows without losing a byte (copy reads the DOM).
|
|
94
|
+
*
|
|
95
|
+
* The hunk header's line budget ends a hunk, so a following `---` / `+++` file
|
|
96
|
+
* header is chrome again — both for a concatenated `git diff` and for `diff -u`
|
|
97
|
+
* output without `diff --git` lines. A malformed header keeps the row until the
|
|
98
|
+
* next `diff --git`, matching how authored fences are rendered.
|
|
99
|
+
* @param code - the fence body, display-trimmed.
|
|
100
|
+
* @returns the rows and each file section's language.
|
|
101
|
+
*/
|
|
102
|
+
export function parseDiffFence(code: string): DiffFence {
|
|
103
|
+
const rows: DiffRow[] = []
|
|
104
|
+
const languages = new Map<number, string>()
|
|
105
|
+
let inHunk = false
|
|
106
|
+
let remainder: HunkRemainder | undefined
|
|
107
|
+
let file = -1
|
|
108
|
+
|
|
109
|
+
const push = (row: Omit<DiffRow, 'file'>): void => {
|
|
110
|
+
rows.push({ ...row, file })
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
for (const raw of code.split('\n')) {
|
|
114
|
+
if (raw.startsWith('diff --git ')) {
|
|
115
|
+
inHunk = false
|
|
116
|
+
remainder = undefined
|
|
117
|
+
push({ kind: 'meta', marker: '', body: '', raw })
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
if (raw.startsWith('@@')) {
|
|
121
|
+
inHunk = true
|
|
122
|
+
remainder = hunkRemainder(raw)
|
|
123
|
+
push({ kind: 'hunk', marker: '', body: '', raw })
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
// A file header carries a marker column but names the file, not a line.
|
|
127
|
+
if (!inHunk && (raw.startsWith('--- ') || raw.startsWith('+++ '))) {
|
|
128
|
+
if (raw.startsWith('--- ')) file += 1
|
|
129
|
+
const language = headerLanguage(raw)
|
|
130
|
+
if (language !== undefined) languages.set(file, language)
|
|
131
|
+
push({ kind: 'meta', marker: '', body: '', raw })
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
if (isDiffMeta(raw)) {
|
|
135
|
+
push({ kind: 'meta', marker: '', body: '', raw })
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
if (raw.startsWith('+')) {
|
|
139
|
+
if (remainder !== undefined) remainder.new -= 1
|
|
140
|
+
push({ kind: 'add', marker: '+', body: raw.slice(1), raw })
|
|
141
|
+
} else if (raw.startsWith('-')) {
|
|
142
|
+
if (remainder !== undefined) remainder.old -= 1
|
|
143
|
+
push({ kind: 'del', marker: '-', body: raw.slice(1), raw })
|
|
144
|
+
} else if (raw.startsWith(' ')) {
|
|
145
|
+
if (remainder !== undefined) {
|
|
146
|
+
remainder.old -= 1
|
|
147
|
+
remainder.new -= 1
|
|
148
|
+
}
|
|
149
|
+
push({ kind: 'context', marker: ' ', body: raw.slice(1), raw })
|
|
150
|
+
} else {
|
|
151
|
+
// Authored diffs may omit the context marker column; the text is the body.
|
|
152
|
+
push({ kind: 'context', marker: '', body: raw, raw })
|
|
153
|
+
}
|
|
154
|
+
if (inHunk && remainder !== undefined && remainder.old <= 0 && remainder.new <= 0) inHunk = false
|
|
155
|
+
}
|
|
156
|
+
return { rows, languages }
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Whether a row carries code the body highlighter tokenizes.
|
|
161
|
+
* @param row - the parsed row.
|
|
162
|
+
* @returns whether the row has tokenizable body text.
|
|
163
|
+
*/
|
|
164
|
+
export function isDiffContent(row: DiffRow): boolean {
|
|
165
|
+
return row.kind === 'add' || row.kind === 'del' || row.kind === 'context'
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Tokenize each file's old side (context and deletions) and new side (context
|
|
170
|
+
* and additions) separately, so an unterminated comment or string in a removed
|
|
171
|
+
* line cannot color the added lines and one file's grammar never applies to the
|
|
172
|
+
* next. Context rows keep the old-side runs.
|
|
173
|
+
* @param diff - parsed fence rows and per-file languages.
|
|
174
|
+
* @returns token runs per content-row index, or `undefined` when no body has a
|
|
175
|
+
* loaded grammar and the caller should render plain text.
|
|
176
|
+
*/
|
|
177
|
+
export function diffFenceTokens(diff: DiffFence): Map<number, readonly HighlightSpan[]> | undefined {
|
|
178
|
+
const groups = new Map<string, Array<{ row: number; body: string; kind: DiffRow['kind'] }>>()
|
|
179
|
+
for (const [index, row] of diff.rows.entries()) {
|
|
180
|
+
if (!isDiffContent(row)) continue
|
|
181
|
+
const sides: Array<'old' | 'new'> = row.kind === 'del'
|
|
182
|
+
? ['old']
|
|
183
|
+
: row.kind === 'add' ? ['new'] : ['old', 'new']
|
|
184
|
+
for (const side of sides) {
|
|
185
|
+
const key = `${row.file}:${side}`
|
|
186
|
+
const entry = { row: index, body: row.body, kind: row.kind }
|
|
187
|
+
const list = groups.get(key)
|
|
188
|
+
if (list === undefined) groups.set(key, [entry])
|
|
189
|
+
else list.push(entry)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (groups.size === 0) return undefined
|
|
193
|
+
|
|
194
|
+
const tokens = new Map<number, readonly HighlightSpan[]>()
|
|
195
|
+
let highlighted = false
|
|
196
|
+
for (const [key, entries] of groups) {
|
|
197
|
+
const separator = key.lastIndexOf(':')
|
|
198
|
+
const language = diff.languages.get(Number(key.slice(0, separator)))
|
|
199
|
+
const side = key.slice(separator + 1)
|
|
200
|
+
const lines = highlightLines(entries.map(entry => entry.body).join('\n'), language)
|
|
201
|
+
if (lines === undefined) continue
|
|
202
|
+
highlighted = true
|
|
203
|
+
entries.forEach((entry, lineIndex) => {
|
|
204
|
+
// A group ending in an empty body line loses shiki's terminal empty row;
|
|
205
|
+
// that row stays plain while its siblings keep their tokens.
|
|
206
|
+
const line = lines[lineIndex] ?? []
|
|
207
|
+
// Context runs come from the old side; added rows only exist on the new side.
|
|
208
|
+
if (side === 'old' || entry.kind === 'add') tokens.set(entry.row, line)
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
return highlighted ? tokens : undefined
|
|
212
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Local Markdown destinations accepted by the file-preview callback. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Recognize a relative, rooted, or absolute Windows path before URL parsing.
|
|
5
|
+
* @param value - A decoded or URI-normalized destination.
|
|
6
|
+
* @returns Whether a filesystem prefix makes the destination unambiguous.
|
|
7
|
+
*/
|
|
8
|
+
export function isExplicitFilePath(value: string): boolean {
|
|
9
|
+
return /^(?:[\\/]|\.{1,2}[\\/]|[a-z]:[\\/])/i.test(value)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Decode a file destination with a fragment, line range, or line/column suffix.
|
|
14
|
+
* Explicit relative, slash/backslash-rooted, and Windows drive paths take
|
|
15
|
+
* precedence over URLs. Other parseable absolute URLs are not file links.
|
|
16
|
+
* Fragments take precedence over colon suffixes; invalid locations open the
|
|
17
|
+
* file without selecting a line. Columns are consumed but not selected.
|
|
18
|
+
* Percent-encode literal `?`, `#`, and numeric colon suffixes in filenames.
|
|
19
|
+
* @param value - Parsed Markdown link destination.
|
|
20
|
+
* @returns A local path and optional first line, or undefined for URLs,
|
|
21
|
+
* fragment-only links, queries, malformed escapes, or control characters.
|
|
22
|
+
*/
|
|
23
|
+
export function parseFileLink(value: string): { path: string; line?: number } | undefined {
|
|
24
|
+
const hash = value.indexOf('#')
|
|
25
|
+
const destination = hash < 0 ? value : value.slice(0, hash)
|
|
26
|
+
if (destination.includes('?')) return undefined
|
|
27
|
+
let path: string
|
|
28
|
+
try {
|
|
29
|
+
path = decodeURIComponent(destination)
|
|
30
|
+
} catch (_error) {
|
|
31
|
+
// Malformed percent escapes cannot identify a file unambiguously.
|
|
32
|
+
return undefined
|
|
33
|
+
}
|
|
34
|
+
if (path.length === 0 || /[\u0000-\u001f\u007f]/.test(path)) return undefined
|
|
35
|
+
if (!isExplicitFilePath(path) && URL.canParse(path)) return undefined
|
|
36
|
+
const suffix = hash < 0 ? /:(\d+)(?::\d+|-(\d+))?$/.exec(destination) : null
|
|
37
|
+
if (suffix !== null) path = path.slice(0, -suffix[0].length)
|
|
38
|
+
if (path.length === 0) return undefined
|
|
39
|
+
const match = suffix ?? (hash < 0 ? null : /^L(\d+)(?:-L(\d+))?$/.exec(value.slice(hash + 1)))
|
|
40
|
+
if (match === null) return { path }
|
|
41
|
+
const line = Number(match[1])
|
|
42
|
+
const end = match[2] === undefined ? line : Number(match[2])
|
|
43
|
+
if (!Number.isSafeInteger(line) || !Number.isSafeInteger(end) || line < 1 || end < line) return { path }
|
|
44
|
+
return { path, line }
|
|
45
|
+
}
|