@deepseek-ai/dsh-client-ui-primitives 0.2.2-alpha.3 → 0.2.2-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +11 -8
- 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,360 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Incremental block-level markdown parsing for an append-only text stream.
|
|
3
|
+
*
|
|
4
|
+
* Re-parsing the whole accumulated document on every streaming chunk is
|
|
5
|
+
* quadratic in the final reply length. CommonMark block parsing is line-based
|
|
6
|
+
* and appended text can only reshape the parse frontier — the last top-level
|
|
7
|
+
* block (a paragraph becoming a setext heading or a table, or a list
|
|
8
|
+
* continuing after a blank line) — so earlier blocks are final. This parser
|
|
9
|
+
* therefore freezes all but the trailing {@link UNSTABLE_TAIL_BLOCKS} blocks
|
|
10
|
+
* and re-parses only the source tail behind them. A final unclosed top-level
|
|
11
|
+
* fence cannot freeze as a block, so its completed content lines use a second
|
|
12
|
+
* frontier: only the last completed line and current partial line return
|
|
13
|
+
* through the caller's grammar. Each source region is therefore parsed a
|
|
14
|
+
* bounded number of times over the stream instead of once per chunk.
|
|
15
|
+
*
|
|
16
|
+
* The block freeze boundary comes from the parser's own `position` offsets.
|
|
17
|
+
* The cut sits at the *end offset* of the last frozen block (not the next
|
|
18
|
+
* block's start): a following block's start offset excludes up to three spaces
|
|
19
|
+
* of insignificant leading indentation, which is harmless to drop, but
|
|
20
|
+
* cutting at the previous end also keeps the inter-block blank lines in the
|
|
21
|
+
* tail so the sliced source stays verbatim. Fence scanning only recognizes a
|
|
22
|
+
* parser-confirmed code node and closing delimiter; ambiguous input returns to
|
|
23
|
+
* the normal tail parse.
|
|
24
|
+
*
|
|
25
|
+
* Known deviation, shared with any prefix-freeze scheme: micromark resolves
|
|
26
|
+
* reference-style links and footnotes document-wide at parse time, so a
|
|
27
|
+
* reference whose definition lands on the other side of the freeze boundary
|
|
28
|
+
* renders literally until the settled full parse self-heals it.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import type { Code, Root, RootContent } from 'mdast'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Trailing blocks kept unstable. Appended text reshapes at most the last
|
|
35
|
+
* block; the second-to-last is retained as safety margin so a freeze decision
|
|
36
|
+
* never has to reason about the parse frontier.
|
|
37
|
+
*/
|
|
38
|
+
const UNSTABLE_TAIL_BLOCKS = 2
|
|
39
|
+
|
|
40
|
+
/** A top-level mdast block plus a render key that is stable across chunks. */
|
|
41
|
+
export interface PositionedBlock {
|
|
42
|
+
/** The parsed block. Positions inside it are relative to its parse slice. */
|
|
43
|
+
readonly node: RootContent
|
|
44
|
+
/**
|
|
45
|
+
* The block's start offset in the full source text. Stable from the frame
|
|
46
|
+
* a block first appears through freezing, so React reconciles rather than
|
|
47
|
+
* remounts when a block crosses the freeze boundary.
|
|
48
|
+
*/
|
|
49
|
+
readonly key: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One {@link IncrementalMarkdownParser.update} result. */
|
|
53
|
+
export interface IncrementalBlocks {
|
|
54
|
+
/** Blocks that can no longer change; grows monotonically per generation. */
|
|
55
|
+
readonly frozen: readonly PositionedBlock[]
|
|
56
|
+
/** The re-parsed unstable tail (at most {@link UNSTABLE_TAIL_BLOCKS} blocks plus growth). */
|
|
57
|
+
readonly tail: readonly PositionedBlock[]
|
|
58
|
+
/** Bumped whenever non-append input discards the frozen prefix; callers drop caches keyed on it. */
|
|
59
|
+
readonly generation: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A block's render key: its absolute source start offset. A position-less
|
|
64
|
+
* node (a grammar is free to omit positions) falls back to a negative
|
|
65
|
+
* list-index key — unique within one update's tail, which is the only place
|
|
66
|
+
* the fallback can occur: freezing requires the cut block's position, so a
|
|
67
|
+
* position-less parse keeps every block in the tail (real grammars always
|
|
68
|
+
* stamp positions and never take this path).
|
|
69
|
+
*/
|
|
70
|
+
function blockKey(node: RootContent, base: number, index: number): number {
|
|
71
|
+
const offset = node.position?.start.offset
|
|
72
|
+
return offset === undefined ? -(index + 1) : base + offset
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface OpenFenceState {
|
|
76
|
+
readonly marker: '`' | '~'
|
|
77
|
+
readonly markerLength: number
|
|
78
|
+
readonly syntheticPrefix: string
|
|
79
|
+
readonly codeIndex: number
|
|
80
|
+
readonly frozen: readonly PositionedBlock[]
|
|
81
|
+
readonly tail: readonly PositionedBlock[]
|
|
82
|
+
readonly pendingStart: number
|
|
83
|
+
readonly valuePrefix: string
|
|
84
|
+
readonly end: { readonly line: number; readonly column: number; readonly offset: number }
|
|
85
|
+
readonly endedWithCarriageReturn: boolean
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Return the first line terminator at or after `start`, including a CRLF pair. */
|
|
89
|
+
function lineTerminatorEnd(text: string, start: number): number | undefined {
|
|
90
|
+
for (let index = start; index < text.length; index += 1) {
|
|
91
|
+
const char = text[index]
|
|
92
|
+
if (char === '\n') return index + 1
|
|
93
|
+
if (char === '\r') return text[index + 1] === '\n' ? index + 2 : index + 1
|
|
94
|
+
}
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Source prefix before the last completed line. Keeping that line beside the
|
|
100
|
+
* current partial line lets the grammar retain its trailing-newline semantics.
|
|
101
|
+
*/
|
|
102
|
+
function committableLinePrefixLength(text: string): number {
|
|
103
|
+
let previousEnd = 0
|
|
104
|
+
let end = 0
|
|
105
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
106
|
+
const char = text[index]
|
|
107
|
+
if (char === '\n') {
|
|
108
|
+
previousEnd = end
|
|
109
|
+
end = index + 1
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
if (char !== '\r' || index + 1 >= text.length) continue
|
|
113
|
+
if (text[index + 1] === '\n') index += 1
|
|
114
|
+
previousEnd = end
|
|
115
|
+
end = index + 1
|
|
116
|
+
}
|
|
117
|
+
return previousEnd
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Exact source terminator ending a non-empty committable prefix. */
|
|
121
|
+
function trailingLineTerminator(text: string): '\n' | '\r' | '\r\n' {
|
|
122
|
+
return text.endsWith('\r\n') ? '\r\n' : text.endsWith('\r') ? '\r' : '\n'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Whether `text` contains a CommonMark closing fence on one of its logical lines. */
|
|
126
|
+
function containsClosingFence(text: string, marker: '`' | '~', markerLength: number): boolean {
|
|
127
|
+
let start = 0
|
|
128
|
+
while (start <= text.length) {
|
|
129
|
+
let end = start
|
|
130
|
+
while (end < text.length && text[end] !== '\n' && text[end] !== '\r') end += 1
|
|
131
|
+
const line = text.slice(start, end)
|
|
132
|
+
let indent = 0
|
|
133
|
+
while (indent < 3 && line[indent] === ' ') indent += 1
|
|
134
|
+
let run = indent
|
|
135
|
+
while (line[run] === marker) run += 1
|
|
136
|
+
if (run - indent >= markerLength && /^[ \t]*$/.test(line.slice(run))) return true
|
|
137
|
+
if (end === text.length) return false
|
|
138
|
+
start = text[end] === '\r' && text[end + 1] === '\n' ? end + 2 : end + 1
|
|
139
|
+
}
|
|
140
|
+
/* v8 ignore next -- each loop iteration returns at EOF or advances past a line terminator. */
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Advance an mdast point across one append while treating a split CRLF as one line ending. */
|
|
145
|
+
function advancePoint(
|
|
146
|
+
point: OpenFenceState['end'],
|
|
147
|
+
appended: string,
|
|
148
|
+
precededByCarriageReturn: boolean,
|
|
149
|
+
): OpenFenceState['end'] {
|
|
150
|
+
let line = point.line
|
|
151
|
+
let column = point.column
|
|
152
|
+
let afterCarriageReturn = precededByCarriageReturn
|
|
153
|
+
for (const char of appended) {
|
|
154
|
+
if (char === '\n') {
|
|
155
|
+
if (!afterCarriageReturn) line += 1
|
|
156
|
+
column = 1
|
|
157
|
+
afterCarriageReturn = false
|
|
158
|
+
continue
|
|
159
|
+
}
|
|
160
|
+
if (char === '\r') {
|
|
161
|
+
line += 1
|
|
162
|
+
column = 1
|
|
163
|
+
afterCarriageReturn = true
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
column += 1
|
|
167
|
+
afterCarriageReturn = false
|
|
168
|
+
}
|
|
169
|
+
return { line, column, offset: point.offset + appended.length }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Append-only incremental parser over a caller-supplied grammar. One instance
|
|
174
|
+
* accumulates one streaming document; non-append input resets it.
|
|
175
|
+
*/
|
|
176
|
+
export class IncrementalMarkdownParser {
|
|
177
|
+
private prevText = ''
|
|
178
|
+
private tailStart = 0
|
|
179
|
+
private frozen: PositionedBlock[] = []
|
|
180
|
+
private generation = 0
|
|
181
|
+
private cached: IncrementalBlocks | null = null
|
|
182
|
+
private openFence: OpenFenceState | null = null
|
|
183
|
+
|
|
184
|
+
/** @param parse - Grammar shared with whatever renders the blocks, so boundaries agree. */
|
|
185
|
+
constructor(private readonly parse: (text: string) => Root) {}
|
|
186
|
+
|
|
187
|
+
/** Parse one unclosed-fence content slice through the caller's grammar. */
|
|
188
|
+
private fenceValue(state: Pick<OpenFenceState, 'syntheticPrefix'>, text: string): string | undefined {
|
|
189
|
+
const root = this.parse(`${state.syntheticPrefix}${text}`)
|
|
190
|
+
if (root.children.length !== 1) return undefined
|
|
191
|
+
const node = root.children[0] as RootContent
|
|
192
|
+
return node.type === 'code' ? node.value : undefined
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Recognize the parsed tail's final unclosed fence and prepare its incremental content frontier. */
|
|
196
|
+
private openFenceState(
|
|
197
|
+
text: string,
|
|
198
|
+
base: number,
|
|
199
|
+
tail: readonly PositionedBlock[],
|
|
200
|
+
frozen: readonly PositionedBlock[],
|
|
201
|
+
): OpenFenceState | null {
|
|
202
|
+
const codeIndex = tail.length - 1
|
|
203
|
+
const block = tail[codeIndex]
|
|
204
|
+
if (block?.node.type !== 'code') return null
|
|
205
|
+
const node = block.node
|
|
206
|
+
const startOffset = node.position?.start.offset
|
|
207
|
+
const end = node.position?.end
|
|
208
|
+
if (startOffset === undefined || end?.offset === undefined) return null
|
|
209
|
+
/* v8 ignore next -- the caller's parse slice ends at text.length, so its final node ends there. */
|
|
210
|
+
if (base + end.offset !== text.length) return null
|
|
211
|
+
const source = text.slice(base)
|
|
212
|
+
const previousLf = source.lastIndexOf('\n', startOffset - 1)
|
|
213
|
+
const previousCr = source.lastIndexOf('\r', startOffset - 1)
|
|
214
|
+
const lineStart = Math.max(previousLf, previousCr) + 1
|
|
215
|
+
const terminatorEnd = lineTerminatorEnd(source, startOffset)
|
|
216
|
+
/* v8 ignore next -- a parser-confirmed fenced code node requires its opening line terminator. */
|
|
217
|
+
if (terminatorEnd === undefined) return null
|
|
218
|
+
if (terminatorEnd === source.length && source.endsWith('\r')) return null
|
|
219
|
+
const openingLine = source.slice(lineStart, terminatorEnd).replace(/[\r\n]+$/, '')
|
|
220
|
+
const opening = /^( {0,3})(`{3,}|~{3,})/.exec(openingLine)
|
|
221
|
+
if (opening === null) return null
|
|
222
|
+
const indent = opening[1] as string
|
|
223
|
+
const run = opening[2] as string
|
|
224
|
+
/* v8 ignore next -- mdast positions a fenced code node at the matched delimiter after indentation. */
|
|
225
|
+
if (lineStart + indent.length !== startOffset) return null
|
|
226
|
+
const marker = run[0] as '`' | '~'
|
|
227
|
+
const contentStart = base + terminatorEnd
|
|
228
|
+
const content = text.slice(contentStart)
|
|
229
|
+
if (containsClosingFence(content, marker, run.length)) return null
|
|
230
|
+
const syntheticPrefix = `${indent}${run}\n`
|
|
231
|
+
const stableLength = committableLinePrefixLength(content)
|
|
232
|
+
const stableValue = stableLength === 0
|
|
233
|
+
? ''
|
|
234
|
+
: this.fenceValue({ syntheticPrefix }, content.slice(0, stableLength))
|
|
235
|
+
if (stableValue === undefined) return null
|
|
236
|
+
const pendingStart = contentStart + stableLength
|
|
237
|
+
const stableSource = content.slice(0, stableLength)
|
|
238
|
+
const valuePrefix = stableLength === 0 ? '' : `${stableValue}${trailingLineTerminator(stableSource)}`
|
|
239
|
+
const pendingValue = this.fenceValue({ syntheticPrefix }, text.slice(pendingStart))
|
|
240
|
+
if (pendingValue === undefined || `${valuePrefix}${pendingValue}` !== node.value) return null
|
|
241
|
+
return {
|
|
242
|
+
marker,
|
|
243
|
+
markerLength: run.length,
|
|
244
|
+
syntheticPrefix,
|
|
245
|
+
codeIndex,
|
|
246
|
+
frozen,
|
|
247
|
+
tail,
|
|
248
|
+
pendingStart,
|
|
249
|
+
valuePrefix,
|
|
250
|
+
end: { line: end.line, column: end.column, offset: end.offset },
|
|
251
|
+
endedWithCarriageReturn: text.endsWith('\r'),
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Extend a recognized unclosed fence without parsing its completed content prefix again. */
|
|
256
|
+
private updateOpenFence(
|
|
257
|
+
state: OpenFenceState,
|
|
258
|
+
text: string,
|
|
259
|
+
previousText: string,
|
|
260
|
+
): IncrementalBlocks | undefined {
|
|
261
|
+
const pending = text.slice(state.pendingStart)
|
|
262
|
+
if (containsClosingFence(pending, state.marker, state.markerLength)) return undefined
|
|
263
|
+
const pendingValue = this.fenceValue(state, pending)
|
|
264
|
+
if (pendingValue === undefined) return undefined
|
|
265
|
+
const stableLength = committableLinePrefixLength(pending)
|
|
266
|
+
const stableValue = stableLength === 0
|
|
267
|
+
? ''
|
|
268
|
+
: this.fenceValue(state, pending.slice(0, stableLength))
|
|
269
|
+
if (stableValue === undefined) return undefined
|
|
270
|
+
// OpenFenceState is private and is installed only from this exact retained
|
|
271
|
+
// code entry; updates replace that entry with another positioned Code.
|
|
272
|
+
const block = state.tail[state.codeIndex] as PositionedBlock
|
|
273
|
+
const previousNode = block.node as Code & { position: NonNullable<Code['position']> }
|
|
274
|
+
const end = advancePoint(
|
|
275
|
+
state.end,
|
|
276
|
+
text.slice(previousText.length),
|
|
277
|
+
state.endedWithCarriageReturn,
|
|
278
|
+
)
|
|
279
|
+
const node: Code = {
|
|
280
|
+
...previousNode,
|
|
281
|
+
value: `${state.valuePrefix}${pendingValue}`,
|
|
282
|
+
position: { start: previousNode.position.start, end },
|
|
283
|
+
}
|
|
284
|
+
const tail = state.tail.map((entry, index) => index === state.codeIndex ? { ...entry, node } : entry)
|
|
285
|
+
const cached = {
|
|
286
|
+
frozen: state.frozen,
|
|
287
|
+
tail,
|
|
288
|
+
generation: this.generation,
|
|
289
|
+
}
|
|
290
|
+
this.openFence = {
|
|
291
|
+
...state,
|
|
292
|
+
tail,
|
|
293
|
+
pendingStart: state.pendingStart + stableLength,
|
|
294
|
+
valuePrefix: stableLength === 0
|
|
295
|
+
? state.valuePrefix
|
|
296
|
+
: `${state.valuePrefix}${stableValue}${trailingLineTerminator(pending.slice(0, stableLength))}`,
|
|
297
|
+
end,
|
|
298
|
+
endedWithCarriageReturn: text.endsWith('\r'),
|
|
299
|
+
}
|
|
300
|
+
return cached
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Fold the current accumulated text and return the frozen/tail split.
|
|
305
|
+
* Idempotent for identical input (the previous result is returned as-is),
|
|
306
|
+
* so callers may invoke it from render paths that re-execute.
|
|
307
|
+
* @param text - The full accumulated markdown source.
|
|
308
|
+
* @returns Frozen and tail blocks with stream-stable render keys.
|
|
309
|
+
*/
|
|
310
|
+
update(text: string): IncrementalBlocks {
|
|
311
|
+
if (this.cached !== null && text === this.prevText) return this.cached
|
|
312
|
+
// Deliberate O(prefix) memcmp per update: sound divergence detection has
|
|
313
|
+
// to verify the whole retained prefix, and startsWith compares bytes two
|
|
314
|
+
// orders of magnitude faster than parsing them — the cost this class
|
|
315
|
+
// exists to remove. Passing append/reset deltas instead would push
|
|
316
|
+
// append bookkeeping across the session-projection update boundary for a check
|
|
317
|
+
// that stays sub-millisecond at realistic reply sizes.
|
|
318
|
+
if (!text.startsWith(this.prevText)) {
|
|
319
|
+
this.prevText = ''
|
|
320
|
+
this.tailStart = 0
|
|
321
|
+
this.frozen = []
|
|
322
|
+
this.openFence = null
|
|
323
|
+
this.generation += 1
|
|
324
|
+
}
|
|
325
|
+
const previousText = this.prevText
|
|
326
|
+
if (previousText !== '' && this.openFence !== null) {
|
|
327
|
+
const incremental = this.updateOpenFence(this.openFence, text, previousText)
|
|
328
|
+
if (incremental !== undefined) {
|
|
329
|
+
this.prevText = text
|
|
330
|
+
this.cached = incremental
|
|
331
|
+
return incremental
|
|
332
|
+
}
|
|
333
|
+
this.openFence = null
|
|
334
|
+
}
|
|
335
|
+
this.prevText = text
|
|
336
|
+
const base = this.tailStart
|
|
337
|
+
const blocks = this.parse(text.slice(base)).children
|
|
338
|
+
let firstUnstable = Math.max(0, blocks.length - UNSTABLE_TAIL_BLOCKS)
|
|
339
|
+
if (firstUnstable > 0) {
|
|
340
|
+
const cutEnd = blocks[firstUnstable - 1]?.position?.end.offset
|
|
341
|
+
if (cutEnd === undefined) {
|
|
342
|
+
// A grammar that omits positions leaves nothing to cut at; keep the
|
|
343
|
+
// whole parse in the tail rather than guessing a boundary.
|
|
344
|
+
firstUnstable = 0
|
|
345
|
+
} else {
|
|
346
|
+
for (const node of blocks.slice(0, firstUnstable)) {
|
|
347
|
+
this.frozen.push({ node, key: blockKey(node, base, this.frozen.length) })
|
|
348
|
+
}
|
|
349
|
+
this.tailStart = base + cutEnd
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const tail = blocks.slice(firstUnstable).map((node, index) => ({
|
|
353
|
+
node,
|
|
354
|
+
key: blockKey(node, base, index),
|
|
355
|
+
}))
|
|
356
|
+
this.cached = { frozen: [...this.frozen], tail, generation: this.generation }
|
|
357
|
+
this.openFence = this.openFenceState(text, base, tail, this.cached.frozen)
|
|
358
|
+
return this.cached
|
|
359
|
+
}
|
|
360
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TeX-to-React via KaTeX, replicating the rehype-katex pipeline this renderer
|
|
3
|
+
* replaced: the same three-arm error chain (strict render, `strict: 'ignore'`
|
|
4
|
+
* retry, error span) and a DOM-identical element tree, so settled math keeps
|
|
5
|
+
* its exact markup. KaTeX emits an HTML string; the browser's own HTML parser
|
|
6
|
+
* (`DOMParser`, applying the spec's SVG/MathML foreign-content attribute
|
|
7
|
+
* adjustments KaTeX output relies on) turns it into a tree this module maps
|
|
8
|
+
* onto React elements — KaTeX output is a static span/MathML/SVG vocabulary
|
|
9
|
+
* with no raw user HTML, the same trust shiki's tree gets in CodeBlock.
|
|
10
|
+
*
|
|
11
|
+
* React 18 has no MathML support, so the `.katex-mathml` subtree's elements
|
|
12
|
+
* land in the HTML namespace — exactly as they did under the replaced
|
|
13
|
+
* hast-util-to-jsx-runtime pipeline. The visual arm is the `.katex-html`
|
|
14
|
+
* span tree; the MathML arm serves assistive technology, which reads it by
|
|
15
|
+
* tag name regardless of namespace.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { createElement } from 'react'
|
|
19
|
+
import type { CSSProperties, ReactNode } from 'react'
|
|
20
|
+
import katex from 'katex'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Convert one inline `style` attribute string into React's style object.
|
|
24
|
+
* KaTeX emits only plain kebab-case declarations (no custom properties and no
|
|
25
|
+
* nameless declarations), so camel-casing the property is the whole mapping.
|
|
26
|
+
*/
|
|
27
|
+
function styleObject(css: string): CSSProperties {
|
|
28
|
+
const style: Record<string, string> = {}
|
|
29
|
+
for (const declaration of css.split(';')) {
|
|
30
|
+
const colon = declaration.indexOf(':')
|
|
31
|
+
if (colon === -1) continue
|
|
32
|
+
const name = declaration.slice(0, colon).trim()
|
|
33
|
+
const key = name.replace(/-([a-z])/g, (_, letter: string) => letter.toUpperCase())
|
|
34
|
+
style[key] = declaration.slice(colon + 1).trim()
|
|
35
|
+
}
|
|
36
|
+
return style
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Map one parsed DOM node onto a React element (text nodes pass through). */
|
|
40
|
+
function domToReact(node: ChildNode, key: number): ReactNode {
|
|
41
|
+
if (node.nodeType === Node.TEXT_NODE) return node.textContent
|
|
42
|
+
/* v8 ignore next 2 -- KaTeX output holds only elements and text; other
|
|
43
|
+
node kinds cannot appear in its serialized vocabulary. */
|
|
44
|
+
if (node.nodeType !== Node.ELEMENT_NODE) return null
|
|
45
|
+
const element = node as Element
|
|
46
|
+
const props: Record<string, unknown> = { key }
|
|
47
|
+
for (const attribute of element.attributes) {
|
|
48
|
+
if (attribute.name === 'class') props['className'] = attribute.value
|
|
49
|
+
else if (attribute.name === 'style') props['style'] = styleObject(attribute.value)
|
|
50
|
+
else props[attribute.name] = attribute.value
|
|
51
|
+
}
|
|
52
|
+
const children = [...element.childNodes].map(domToReact)
|
|
53
|
+
return children.length === 0
|
|
54
|
+
? createElement(element.localName, props)
|
|
55
|
+
: createElement(element.localName, props, ...children)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Render TeX source to React elements through KaTeX.
|
|
60
|
+
* @param value - The TeX source (math node value; fenced `math` blocks append
|
|
61
|
+
* their trailing newline to match the replaced pipeline's text extraction).
|
|
62
|
+
* @param displayMode - Display (block) versus inline rendering.
|
|
63
|
+
* @returns KaTeX's element tree, or the error span when the source does not
|
|
64
|
+
* parse (colored with KaTeX's stock `errorColor`, matching rehype-katex).
|
|
65
|
+
*/
|
|
66
|
+
export function renderTexToReact(value: string, displayMode: boolean): ReactNode {
|
|
67
|
+
let html: string
|
|
68
|
+
try {
|
|
69
|
+
html = katex.renderToString(value, { displayMode, throwOnError: true })
|
|
70
|
+
} catch (error) {
|
|
71
|
+
try {
|
|
72
|
+
html = katex.renderToString(value, { displayMode, strict: 'ignore', throwOnError: false })
|
|
73
|
+
} catch {
|
|
74
|
+
// KaTeX renders ParseErrors itself under throwOnError: false; only its
|
|
75
|
+
// internal errors reach here, so mirror rehype-katex's manual span.
|
|
76
|
+
/* v8 ignore next 8 */
|
|
77
|
+
return (
|
|
78
|
+
<span
|
|
79
|
+
className="katex-error"
|
|
80
|
+
style={{ color: '#cc0000' }}
|
|
81
|
+
title={String(error)}
|
|
82
|
+
>
|
|
83
|
+
{value}
|
|
84
|
+
</span>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html')
|
|
89
|
+
return [...parsed.body.childNodes].map(domToReact)
|
|
90
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Recover standalone local image references with bare spaces without changing code or source offsets. */
|
|
2
|
+
import type { Root, RootContent, PhrasingContent } from 'mdast'
|
|
3
|
+
import { classifyFileType } from '../FileTypeIcon.tsx'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Recover only unambiguous, unescaped image-only paragraphs containing a local path with spaces.
|
|
7
|
+
* @param root - Parsed Markdown tree, modified in place.
|
|
8
|
+
* @param source - Original source used to distinguish authored syntax from escaped examples.
|
|
9
|
+
* @returns The same root with recovered image nodes.
|
|
10
|
+
*/
|
|
11
|
+
export function recoverLocalImages(root: Root, source: string): Root {
|
|
12
|
+
const visit = (node: Root | RootContent): void => {
|
|
13
|
+
if (node.type === 'paragraph' && node.children.length === 1) {
|
|
14
|
+
const [child] = node.children as [PhrasingContent]
|
|
15
|
+
if (child.type === 'text' && child.position !== undefined
|
|
16
|
+
&& source.slice(child.position.start.offset, child.position.end.offset) === child.value) {
|
|
17
|
+
const pattern = /^!\[([^\]\n]*)\]\(((?:\/(?!\/)|\.{1,2}\/|[a-z]:[\\/])[^\n<>()[\]"']+\.[a-z\d]+)\)$/iu
|
|
18
|
+
const match = pattern.exec(child.value) as [string, string, string] | null
|
|
19
|
+
if (match !== null && match[2].includes(' ') && classifyFileType(match[2]) === 'image') {
|
|
20
|
+
node.children = [{ type: 'image', alt: match[1], url: match[2], position: child.position }]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
} else if ('children' in node) {
|
|
24
|
+
for (const child of node.children) visit(child)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
visit(root)
|
|
28
|
+
return root
|
|
29
|
+
}
|