@diegopluna/narvi 0.0.2 → 0.2.0
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.md +116 -44
- package/dist/base.css +96 -0
- package/dist/color.d.ts +1 -1
- package/dist/color.js +1 -1
- package/dist/components/activity/ActivityContent.svelte +3 -15
- package/dist/components/activity/ActivityStep.svelte +12 -12
- package/dist/components/activity/ActivityThought.svelte +4 -4
- package/dist/components/activity/ActivityTrigger.svelte +5 -6
- package/dist/components/app-shell/AppShellBar.svelte +89 -0
- package/dist/components/app-shell/AppShellBar.svelte.d.ts +20 -0
- package/dist/components/app-shell/AppShellHeader.svelte +17 -17
- package/dist/components/app-shell/AppShellHeader.svelte.d.ts +2 -2
- package/dist/components/app-shell/AppShellMain.svelte +4 -20
- package/dist/components/app-shell/AppShellPanel.svelte +21 -19
- package/dist/components/app-shell/AppShellPanes.svelte +58 -0
- package/dist/components/app-shell/AppShellPanes.svelte.d.ts +11 -0
- package/dist/components/app-shell/AppShellRail.svelte +85 -0
- package/dist/components/app-shell/AppShellRail.svelte.d.ts +11 -0
- package/dist/components/app-shell/AppShellRoot.svelte +50 -24
- package/dist/components/app-shell/AppShellRoot.svelte.d.ts +8 -6
- package/dist/components/app-shell/AppShellSheet.svelte +95 -0
- package/dist/components/app-shell/AppShellSheet.svelte.d.ts +17 -0
- package/dist/components/app-shell/AppShellSidebar.svelte +41 -65
- package/dist/components/app-shell/AppShellSidebar.svelte.d.ts +1 -5
- package/dist/components/app-shell/AppShellToggle.svelte +62 -23
- package/dist/components/app-shell/AppShellToggle.svelte.d.ts +2 -0
- package/dist/components/app-shell/context.d.ts +6 -0
- package/dist/components/app-shell/index.d.ts +9 -1
- package/dist/components/app-shell/index.js +5 -1
- package/dist/components/approval/Approval.svelte +99 -23
- package/dist/components/approval/Approval.svelte.d.ts +7 -0
- package/dist/components/avatar/AvatarFallback.svelte +1 -2
- package/dist/components/avatar/AvatarRoot.svelte +2 -2
- package/dist/components/badge/Badge.svelte +5 -5
- package/dist/components/button/Button.svelte +227 -51
- package/dist/components/button/Button.svelte.d.ts +38 -2
- package/dist/components/card/Card.svelte +19 -33
- package/dist/components/change-stats/ChangeStats.svelte +1 -2
- package/dist/components/changes-summary/ChangesSummary.svelte +14 -15
- package/dist/components/checkbox/Checkbox.svelte +31 -25
- package/dist/components/checkbox/Checkbox.svelte.d.ts +5 -0
- package/dist/components/code/Code.svelte +1 -2
- package/dist/components/collapsible/CollapsibleContent.svelte +38 -0
- package/dist/components/collapsible/CollapsibleContent.svelte.d.ts +11 -0
- package/dist/components/collapsible/CollapsibleRoot.svelte +44 -0
- package/dist/components/collapsible/CollapsibleRoot.svelte.d.ts +11 -0
- package/dist/components/collapsible/CollapsibleTrigger.svelte +83 -0
- package/dist/components/collapsible/CollapsibleTrigger.svelte.d.ts +13 -0
- package/dist/components/collapsible/context.d.ts +5 -0
- package/dist/components/collapsible/context.js +11 -0
- package/dist/components/collapsible/index.d.ts +7 -0
- package/dist/components/collapsible/index.js +4 -0
- package/dist/components/command/CommandItem.svelte +1 -1
- package/dist/components/command/styles.d.ts +3 -2
- package/dist/components/command/styles.js +15 -12
- package/dist/components/composer/ComposerAttachButton.svelte +1 -1
- package/dist/components/composer/ComposerAttachment.svelte +13 -18
- package/dist/components/composer/ComposerModelPicker.svelte +24 -28
- package/dist/components/composer/ComposerRoot.svelte +15 -13
- package/dist/components/composer/ComposerSubmit.svelte +12 -20
- package/dist/components/composer/ComposerTextarea.svelte +1 -1
- package/dist/components/composer/ComposerToggle.svelte +16 -18
- package/dist/components/composer/ComposerTray.svelte +78 -0
- package/dist/components/composer/ComposerTray.svelte.d.ts +15 -0
- package/dist/components/composer/index.d.ts +3 -1
- package/dist/components/composer/index.js +3 -2
- package/dist/components/conversation/ConversationContent.svelte +3 -2
- package/dist/components/conversation/ConversationEmpty.svelte +1 -1
- package/dist/components/conversation/ConversationScrollButton.svelte +9 -11
- package/dist/components/dialog/ConfirmDialog.svelte +1 -1
- package/dist/components/dialog/DialogContent.svelte +5 -1
- package/dist/components/dialog/styles.d.ts +2 -26
- package/dist/components/dialog/styles.js +12 -49
- package/dist/components/divider/Divider.svelte +3 -3
- package/dist/components/empty-state/EmptyState.svelte +4 -5
- package/dist/components/field/Field.svelte +4 -4
- package/dist/components/frame/Frame.svelte +6 -7
- package/dist/components/frame/Frame.svelte.d.ts +1 -1
- package/dist/components/grid/Grid.svelte +111 -0
- package/dist/components/grid/Grid.svelte.d.ts +27 -0
- package/dist/components/heading/Heading.svelte +2 -2
- package/dist/components/hover-card/HoverCardContent.svelte +50 -0
- package/dist/components/hover-card/HoverCardContent.svelte.d.ts +10 -0
- package/dist/components/hover-card/HoverCardRoot.svelte +22 -0
- package/dist/components/hover-card/HoverCardRoot.svelte.d.ts +5 -0
- package/dist/components/hover-card/index.d.ts +7 -0
- package/dist/components/hover-card/index.js +7 -0
- package/dist/components/kbd/Kbd.svelte +12 -7
- package/dist/components/link/Link.svelte +9 -7
- package/dist/components/link/Link.svelte.d.ts +4 -1
- package/dist/components/markdown/Markdown.svelte +714 -0
- package/dist/components/markdown/Markdown.svelte.d.ts +80 -0
- package/dist/components/markdown/MarkdownBlock.svelte +231 -0
- package/dist/components/markdown/MarkdownBlock.svelte.d.ts +11 -0
- package/dist/components/markdown/MarkdownBlocks.svelte +115 -0
- package/dist/components/markdown/MarkdownBlocks.svelte.d.ts +15 -0
- package/dist/components/markdown/MarkdownCode.svelte +583 -0
- package/dist/components/markdown/MarkdownCode.svelte.d.ts +23 -0
- package/dist/components/markdown/MarkdownDiagram.svelte +310 -0
- package/dist/components/markdown/MarkdownDiagram.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownFile.svelte +134 -0
- package/dist/components/markdown/MarkdownFile.svelte.d.ts +19 -0
- package/dist/components/markdown/MarkdownFootnotes.svelte +61 -0
- package/dist/components/markdown/MarkdownFootnotes.svelte.d.ts +8 -0
- package/dist/components/markdown/MarkdownFresh.svelte +69 -0
- package/dist/components/markdown/MarkdownFresh.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownHtml.svelte +72 -0
- package/dist/components/markdown/MarkdownHtml.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownImage.svelte +150 -0
- package/dist/components/markdown/MarkdownImage.svelte.d.ts +8 -0
- package/dist/components/markdown/MarkdownInlines.svelte +225 -0
- package/dist/components/markdown/MarkdownInlines.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownLink.svelte +120 -0
- package/dist/components/markdown/MarkdownLink.svelte.d.ts +25 -0
- package/dist/components/markdown/MarkdownMath.svelte +138 -0
- package/dist/components/markdown/MarkdownMath.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownTable.svelte +265 -0
- package/dist/components/markdown/MarkdownTable.svelte.d.ts +17 -0
- package/dist/components/markdown/caret.d.ts +14 -0
- package/dist/components/markdown/caret.js +67 -0
- package/dist/components/markdown/context.d.ts +93 -0
- package/dist/components/markdown/context.js +128 -0
- package/dist/components/markdown/copy.d.ts +13 -0
- package/dist/components/markdown/copy.js +74 -0
- package/dist/components/markdown/hidden.d.ts +12 -0
- package/dist/components/markdown/hidden.js +31 -0
- package/dist/components/markdown/kept.d.ts +32 -0
- package/dist/components/markdown/kept.js +71 -0
- package/dist/components/markdown/labels.d.ts +46 -0
- package/dist/components/markdown/labels.js +35 -0
- package/dist/components/markdown/save.d.ts +11 -0
- package/dist/components/markdown/save.js +58 -0
- package/dist/components/markdown/scrollable.d.ts +24 -0
- package/dist/components/markdown/scrollable.js +152 -0
- package/dist/components/markdown/static.d.ts +37 -0
- package/dist/components/markdown/static.js +233 -0
- package/dist/components/markdown/styles.d.ts +328 -0
- package/dist/components/markdown/styles.js +302 -0
- package/dist/components/menu/MenuRadioItem.svelte +3 -3
- package/dist/components/menu/index.d.ts +23 -0
- package/dist/components/menu/index.js +3 -1
- package/dist/components/message/MessageAction.svelte +24 -5
- package/dist/components/message/MessageAction.svelte.d.ts +9 -0
- package/dist/components/message/MessageActions.svelte +2 -3
- package/dist/components/message/MessageContent.svelte +2 -3
- package/dist/components/message/MessageCopy.svelte +66 -8
- package/dist/components/message/MessageCopy.svelte.d.ts +17 -1
- package/dist/components/message/MessageHeader.svelte +3 -3
- package/dist/components/message/MessagePending.svelte +1 -1
- package/dist/components/message/MessageRoot.svelte +3 -4
- package/dist/components/message/MessageTime.svelte +3 -4
- package/dist/components/notice/Notice.svelte +14 -10
- package/dist/components/plan/Plan.svelte +25 -21
- package/dist/components/progress/Progress.svelte +34 -38
- package/dist/components/questionnaire/Questionnaire.svelte +19 -22
- package/dist/components/queued-message/QueuedMessage.svelte +14 -7
- package/dist/components/rail/RailItem.svelte +43 -40
- package/dist/components/rail/RailItem.svelte.d.ts +3 -1
- package/dist/components/rail/RailRoot.svelte +18 -4
- package/dist/components/rail/RailRoot.svelte.d.ts +1 -1
- package/dist/components/rail/RailSeparator.svelte +17 -0
- package/dist/components/rail/RailSeparator.svelte.d.ts +18 -0
- package/dist/components/rail/index.d.ts +2 -1
- package/dist/components/rail/index.js +2 -1
- package/dist/components/reasoning/ReasoningContent.svelte +4 -8
- package/dist/components/reasoning/ReasoningTrigger.svelte +4 -5
- package/dist/components/resize-handle/ResizeHandle.svelte +4 -7
- package/dist/components/reveal/Reveal.svelte +89 -0
- package/dist/components/reveal/Reveal.svelte.d.ts +17 -0
- package/dist/components/reveal/revealed.d.ts +38 -0
- package/dist/components/reveal/revealed.js +79 -0
- package/dist/components/segmented/SegmentedControl.svelte +11 -16
- package/dist/components/select/Select.svelte +1 -1
- package/dist/components/separator/Separator.svelte +1 -1
- package/dist/components/settings-row/SettingsRow.svelte +5 -5
- package/dist/components/sheet/SheetContent.svelte +63 -0
- package/dist/components/sheet/SheetContent.svelte.d.ts +18 -0
- package/dist/components/sheet/index.d.ts +12 -0
- package/dist/components/sheet/index.js +12 -0
- package/dist/components/sheet/styles.d.ts +51 -0
- package/dist/components/sheet/styles.js +65 -0
- package/dist/components/shimmer/Shimmer.svelte +41 -26
- package/dist/components/sidebar/SidebarContent.svelte +1 -1
- package/dist/components/sidebar/SidebarFolder.svelte +142 -0
- package/dist/components/sidebar/SidebarFolder.svelte.d.ts +19 -0
- package/dist/components/sidebar/SidebarFooter.svelte +1 -1
- package/dist/components/sidebar/SidebarGroup.svelte +41 -35
- package/dist/components/sidebar/SidebarGroup.svelte.d.ts +3 -3
- package/dist/components/sidebar/SidebarHeader.svelte +12 -6
- package/dist/components/sidebar/SidebarHeader.svelte.d.ts +4 -1
- package/dist/components/sidebar/SidebarItem.svelte +34 -38
- package/dist/components/sidebar/SidebarRoot.svelte +62 -0
- package/dist/components/sidebar/SidebarRoot.svelte.d.ts +14 -0
- package/dist/components/sidebar/index.d.ts +5 -1
- package/dist/components/sidebar/index.js +3 -1
- package/dist/components/skeleton/Skeleton.svelte +26 -15
- package/dist/components/slider/Slider.svelte +200 -0
- package/dist/components/slider/Slider.svelte.d.ts +55 -0
- package/dist/components/spinner/Spinner.svelte +4 -11
- package/dist/components/stack/Stack.svelte +37 -44
- package/dist/components/stack/Stack.svelte.d.ts +12 -6
- package/dist/components/status-indicator/StatusIndicator.svelte +1 -1
- package/dist/components/suggestions/SuggestionsItem.svelte +11 -13
- package/dist/components/switch/Switch.svelte +16 -25
- package/dist/components/tab-strip/TabStrip.svelte +340 -0
- package/dist/components/tab-strip/TabStrip.svelte.d.ts +31 -0
- package/dist/components/tabs/Tabs.svelte +15 -14
- package/dist/components/tabs/Tabs.svelte.d.ts +4 -0
- package/dist/components/text/Text.svelte +64 -14
- package/dist/components/text/Text.svelte.d.ts +13 -3
- package/dist/components/toast/Toaster.svelte +12 -10
- package/dist/components/toggle/Toggle.svelte +40 -0
- package/dist/components/toggle/Toggle.svelte.d.ts +15 -0
- package/dist/components/toggle-group/ToggleGroupItem.svelte +37 -0
- package/dist/components/toggle-group/ToggleGroupItem.svelte.d.ts +12 -0
- package/dist/components/toggle-group/ToggleGroupRoot.svelte +95 -0
- package/dist/components/toggle-group/ToggleGroupRoot.svelte.d.ts +33 -0
- package/dist/components/toggle-group/context.d.ts +6 -0
- package/dist/components/toggle-group/context.js +11 -0
- package/dist/components/toggle-group/index.d.ts +5 -0
- package/dist/components/toggle-group/index.js +3 -0
- package/dist/components/tool/ToolContent.svelte +3 -8
- package/dist/components/tool/ToolSection.svelte +4 -5
- package/dist/components/tool/ToolTrigger.svelte +14 -15
- package/dist/components/tooltip/TooltipContent.svelte +9 -6
- package/dist/components/work-fold/WorkFold.svelte +17 -34
- package/dist/components/work-fold/WorkFold.svelte.d.ts +1 -1
- package/dist/index.d.ts +38 -10
- package/dist/index.js +38 -12
- package/dist/internal/Chevron.svelte +2 -3
- package/dist/internal/Code.svelte +3 -4
- package/dist/internal/Collapse.svelte +6 -5
- package/dist/internal/Collapse.svelte.d.ts +2 -1
- package/dist/internal/Glyph.svelte +16 -0
- package/dist/internal/Glyph.svelte.d.ts +6 -0
- package/dist/internal/Tile.svelte +17 -37
- package/dist/internal/control.d.ts +4 -4
- package/dist/internal/control.js +21 -18
- package/dist/internal/layer.d.ts +3 -5
- package/dist/internal/layer.js +14 -18
- package/dist/internal/layout.d.ts +13 -0
- package/dist/internal/layout.js +453 -0
- package/dist/internal/markers.stylex.d.ts +8 -0
- package/dist/internal/markers.stylex.js +4 -0
- package/dist/internal/plugin-order.d.ts +8 -0
- package/dist/internal/plugin-order.js +16 -0
- package/dist/internal/responsive.d.ts +45 -0
- package/dist/internal/responsive.js +42 -0
- package/dist/internal/roving.js +5 -2
- package/dist/internal/shared.d.ts +8 -1
- package/dist/internal/shared.js +21 -5
- package/dist/internal/sx.d.ts +31 -4
- package/dist/internal/sx.js +17 -0
- package/dist/internal/toggle.d.ts +73 -0
- package/dist/internal/toggle.js +83 -0
- package/dist/internal/visible.d.ts +32 -0
- package/dist/internal/visible.js +44 -0
- package/dist/markdown/ast.d.ts +209 -0
- package/dist/markdown/ast.js +10 -0
- package/dist/markdown/blocks.d.ts +177 -0
- package/dist/markdown/blocks.js +1489 -0
- package/dist/markdown/code-info.d.ts +10 -0
- package/dist/markdown/code-info.js +16 -0
- package/dist/markdown/common.d.ts +25 -0
- package/dist/markdown/common.js +107 -0
- package/dist/markdown/diagrams.d.ts +2 -0
- package/dist/markdown/diagrams.js +18 -0
- package/dist/markdown/file-paths.d.ts +29 -0
- package/dist/markdown/file-paths.js +80 -0
- package/dist/markdown/files.d.ts +16 -0
- package/dist/markdown/files.js +133 -0
- package/dist/markdown/highlight.d.ts +30 -0
- package/dist/markdown/highlight.js +127 -0
- package/dist/markdown/html.d.ts +23 -0
- package/dist/markdown/html.js +289 -0
- package/dist/markdown/images.d.ts +15 -0
- package/dist/markdown/images.js +45 -0
- package/dist/markdown/index.d.ts +12 -0
- package/dist/markdown/index.js +5 -0
- package/dist/markdown/inlines.d.ts +48 -0
- package/dist/markdown/inlines.js +1714 -0
- package/dist/markdown/math.d.ts +2 -0
- package/dist/markdown/math.js +22 -0
- package/dist/markdown/parse.d.ts +81 -0
- package/dist/markdown/parse.js +204 -0
- package/dist/markdown/plugin.d.ts +178 -0
- package/dist/markdown/plugin.js +160 -0
- package/dist/markdown/sanitize.d.ts +78 -0
- package/dist/markdown/sanitize.js +522 -0
- package/dist/markdown/share.d.ts +12 -0
- package/dist/markdown/share.js +59 -0
- package/dist/markdown/table.d.ts +13 -0
- package/dist/markdown/table.js +60 -0
- package/dist/media.stylex.d.ts +52 -0
- package/dist/media.stylex.js +53 -0
- package/dist/preset.d.ts +40 -16
- package/dist/preset.js +169 -51
- package/dist/tokens.stylex.d.ts +70 -37
- package/dist/tokens.stylex.js +127 -62
- package/dist/typography.d.ts +48 -0
- package/dist/typography.js +55 -0
- package/dist/vite.d.ts +9 -1
- package/dist/vite.js +37 -4
- package/package.json +58 -6
- package/skills/building-ui/SKILL.md +242 -0
- package/skills/building-ui/references/components.md +207 -0
- package/skills/markdown/SKILL.md +452 -0
- package/skills/markdown/references/plugins.md +103 -0
- package/skills/setup/SKILL.md +191 -0
- package/skills/styling/SKILL.md +269 -0
- package/skills/theming/SKILL.md +184 -0
- package/dist/internal/loop.d.ts +0 -35
- package/dist/internal/loop.js +0 -72
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
A fenced or indented code block: its language and a copy button above the code, and the
|
|
3
|
+
code coloured by a highlighter plugin when there is one. A block with no language is
|
|
4
|
+
usually prose or a command, so its lines wrap instead of scrolling.
|
|
5
|
+
|
|
6
|
+
Coloured code is written as markup, a line at a time, not as an element per token made
|
|
7
|
+
one by one: a forty-line block has some four hundred tokens, and keeping track of each
|
|
8
|
+
would cost more than the rest of the reply together. The markup is Narvi's own: a
|
|
9
|
+
`<span>` with one of its own classes, around text that has been escaped (`tokensHtml`).
|
|
10
|
+
Nothing of the code's text is ever read as markup.
|
|
11
|
+
-->
|
|
12
|
+
<script lang="ts" module>
|
|
13
|
+
import * as stylex from '@stylexjs/stylex';
|
|
14
|
+
import Glyph, { glyphHtml } from '../../internal/Glyph.svelte';
|
|
15
|
+
import type { CodeBlock } from '../../markdown/ast.js';
|
|
16
|
+
import { codeTitle } from '../../markdown/code-info.js';
|
|
17
|
+
import { scrollable } from './scrollable.js';
|
|
18
|
+
import type { Highlighter, HighlightStream, HighlightToken } from '../../markdown/plugin.js';
|
|
19
|
+
import { attrs, classes } from '../../internal/sx.js';
|
|
20
|
+
import { escapeHtml } from '../../markdown/html.js';
|
|
21
|
+
import { colors, font, radius } from '../../tokens.stylex.js';
|
|
22
|
+
import { actionHtml } from '../message/MessageAction.svelte';
|
|
23
|
+
import { copyHtml } from '../message/MessageCopy.svelte';
|
|
24
|
+
import { getMarkdownContext } from './context.js';
|
|
25
|
+
import { showHidden } from './hidden.js';
|
|
26
|
+
import { prose } from './styles.js';
|
|
27
|
+
|
|
28
|
+
export type MarkdownCodeProps = { node: CodeBlock; enter?: boolean };
|
|
29
|
+
|
|
30
|
+
const styles = stylex.create({
|
|
31
|
+
frame: {
|
|
32
|
+
position: 'relative',
|
|
33
|
+
minWidth: 0,
|
|
34
|
+
borderRadius: radius.control,
|
|
35
|
+
backgroundColor: colors.fillSunken,
|
|
36
|
+
outlineStyle: 'solid',
|
|
37
|
+
outlineWidth: '1px',
|
|
38
|
+
outlineColor: 'transparent',
|
|
39
|
+
outlineOffset: '-1px'
|
|
40
|
+
},
|
|
41
|
+
header: {
|
|
42
|
+
display: 'flex',
|
|
43
|
+
alignItems: 'center',
|
|
44
|
+
justifyContent: 'space-between',
|
|
45
|
+
gap: '0.5rem',
|
|
46
|
+
minHeight: '2rem',
|
|
47
|
+
paddingInlineStart: '0.75rem',
|
|
48
|
+
paddingInlineEnd: '0.25rem',
|
|
49
|
+
color: colors.textSubtle,
|
|
50
|
+
fontSize: font.sizeCaption,
|
|
51
|
+
lineHeight: 1,
|
|
52
|
+
// The label and the button are not part of the code: a selection skips them
|
|
53
|
+
userSelect: 'none',
|
|
54
|
+
WebkitUserSelect: 'none'
|
|
55
|
+
},
|
|
56
|
+
label: {
|
|
57
|
+
display: 'inline-flex',
|
|
58
|
+
alignItems: 'center',
|
|
59
|
+
gap: '0.375rem',
|
|
60
|
+
minWidth: 0,
|
|
61
|
+
overflow: 'hidden',
|
|
62
|
+
textOverflow: 'ellipsis',
|
|
63
|
+
whiteSpace: 'nowrap'
|
|
64
|
+
},
|
|
65
|
+
pre: {
|
|
66
|
+
margin: 0,
|
|
67
|
+
// No padding above or below: the room under the code is the code's own. A box that
|
|
68
|
+
// scrolls, has padding, and whose content is left out for now (below) is not as tall
|
|
69
|
+
// in every browser, and without the padding there is nothing to disagree about.
|
|
70
|
+
paddingBlockStart: 0,
|
|
71
|
+
paddingBlockEnd: 0,
|
|
72
|
+
paddingInline: '0.75rem',
|
|
73
|
+
overflowX: 'auto',
|
|
74
|
+
// A sideways scroller must not also scroll up and down by a pixel
|
|
75
|
+
overflowY: 'hidden',
|
|
76
|
+
color: colors.text,
|
|
77
|
+
fontFamily: font.mono,
|
|
78
|
+
fontSize: '0.8125em',
|
|
79
|
+
lineHeight: 1.6,
|
|
80
|
+
tabSize: 2,
|
|
81
|
+
whiteSpace: 'pre',
|
|
82
|
+
scrollbarWidth: 'thin',
|
|
83
|
+
// Code that is off the screen is not laid out until it comes near. Code is most of
|
|
84
|
+
// the elements of a reply that has any, and a long reply is mostly off the screen.
|
|
85
|
+
// Until then the block is as tall as its lines make it, which is known without
|
|
86
|
+
// laying them out: a line of code does not wrap. Once it has been drawn, the
|
|
87
|
+
// browser remembers what it measured (`auto`).
|
|
88
|
+
contentVisibility: 'auto',
|
|
89
|
+
containIntrinsicBlockSize: 'auto calc(var(--narvi-code-lines, 1) * 1lh + 0.75rem)'
|
|
90
|
+
},
|
|
91
|
+
// A block still being written is on the screen and changes every frame, and text with
|
|
92
|
+
// no language wraps, so how tall it is depends on how wide: both are laid out as ever
|
|
93
|
+
laidOut: { contentVisibility: 'visible', containIntrinsicBlockSize: 'none' },
|
|
94
|
+
wrap: { whiteSpace: 'pre-wrap', overflowWrap: 'anywhere' },
|
|
95
|
+
code: {
|
|
96
|
+
display: 'block',
|
|
97
|
+
paddingBlockEnd: '0.75rem',
|
|
98
|
+
// The block's own face and size, not the ones a browser gives `<code>`: left to the
|
|
99
|
+
// browser, the code is in its default monospace, and smaller than the block asks for
|
|
100
|
+
fontFamily: 'inherit',
|
|
101
|
+
fontSize: 'inherit'
|
|
102
|
+
},
|
|
103
|
+
// With line numbers, they sit beside the code and stay in view as it scrolls sideways
|
|
104
|
+
numbered: { display: 'flex', paddingInlineStart: 0 },
|
|
105
|
+
numbers: {
|
|
106
|
+
position: 'sticky',
|
|
107
|
+
insetInlineStart: 0,
|
|
108
|
+
flexShrink: 0,
|
|
109
|
+
paddingInlineStart: '0.75rem',
|
|
110
|
+
paddingInlineEnd: '0.875rem',
|
|
111
|
+
backgroundColor: colors.fillSunken,
|
|
112
|
+
color: colors.textSubtle,
|
|
113
|
+
textAlign: 'end',
|
|
114
|
+
fontVariantNumeric: 'tabular-nums',
|
|
115
|
+
userSelect: 'none',
|
|
116
|
+
WebkitUserSelect: 'none'
|
|
117
|
+
},
|
|
118
|
+
numberedCode: { flexShrink: 0, paddingInlineEnd: '0.75rem' },
|
|
119
|
+
// A group of lines of a block that streams: a block of its own, laid out on its own
|
|
120
|
+
lines: { display: 'block' },
|
|
121
|
+
// A block with no language and no file name has nothing to put in a header: its copy
|
|
122
|
+
// button sits in the corner instead, and the code keeps clear of it
|
|
123
|
+
bare: { position: 'relative' },
|
|
124
|
+
bareHeader: {
|
|
125
|
+
position: 'absolute',
|
|
126
|
+
insetBlockStart: 0,
|
|
127
|
+
insetInlineEnd: 0,
|
|
128
|
+
paddingInlineStart: 0
|
|
129
|
+
},
|
|
130
|
+
barePre: { paddingBlockStart: '0.75rem', paddingInlineEnd: '2.5rem' },
|
|
131
|
+
actions: { display: 'inline-flex', flexShrink: 0, gap: '0.125rem' },
|
|
132
|
+
copy: { flexShrink: 0 }
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
/** Token kinds a highlighter names, and the colours Narvi gives them. */
|
|
136
|
+
const kinds = stylex.create({
|
|
137
|
+
keyword: { color: colors.codeLiteral },
|
|
138
|
+
string: { color: colors.codeString },
|
|
139
|
+
number: { color: colors.codeNumber },
|
|
140
|
+
constant: { color: colors.codeNumber },
|
|
141
|
+
comment: { color: colors.textSubtle, fontStyle: 'italic' },
|
|
142
|
+
function: { color: colors.codeKey },
|
|
143
|
+
type: { color: colors.codeKey },
|
|
144
|
+
property: { color: colors.codeKey },
|
|
145
|
+
tag: { color: colors.codeLiteral },
|
|
146
|
+
attribute: { color: colors.codeKey },
|
|
147
|
+
punctuation: { color: colors.codePunct },
|
|
148
|
+
operator: { color: colors.codePunct },
|
|
149
|
+
inserted: { color: colors.success },
|
|
150
|
+
deleted: { color: colors.danger }
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
/** The class of each kind of token, worked out once. */
|
|
154
|
+
const kindClasses = new Map(
|
|
155
|
+
Object.entries(kinds).map(([kind, style]) => [kind, classes(style as never)])
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
// A colour a highlighter gives is written into a `style`. Only what reads as a colour and
|
|
159
|
+
// nothing else is: no `;` to start another property, no quote to end the attribute
|
|
160
|
+
const reColor =
|
|
161
|
+
/^(?:#[0-9a-fA-F]{3,8}|[a-zA-Z]{3,24}|(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|light-dark|var)\([-\w\s.,%/#()]*\))$/;
|
|
162
|
+
|
|
163
|
+
const markup = new WeakMap<HighlightToken[], string>();
|
|
164
|
+
|
|
165
|
+
/** How many lines code has: what a block that is not laid out yet is as tall as. */
|
|
166
|
+
function countLines(code: string): number {
|
|
167
|
+
let count = 1;
|
|
168
|
+
for (let at = code.indexOf(NEWLINE); at !== -1; at = code.indexOf(NEWLINE, at + 1)) count++;
|
|
169
|
+
return count;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const reBlank = /^\s*$/;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A line's tokens as markup: its text escaped, in spans of Narvi's own. Kept with the line.
|
|
176
|
+
*
|
|
177
|
+
* Tokens that follow one another and look the same share a span, and white space, which
|
|
178
|
+
* has no look, goes into whatever is open: `export async function ` is one span and not
|
|
179
|
+
* five. A reply's code is most of the elements on its page, and each is one the browser
|
|
180
|
+
* has to read, style and lay out.
|
|
181
|
+
*/
|
|
182
|
+
function tokensHtml(tokens: HighlightToken[]): string {
|
|
183
|
+
let html = markup.get(tokens);
|
|
184
|
+
if (html !== undefined) return html;
|
|
185
|
+
html = '';
|
|
186
|
+
// The span being written: its attributes ('' for text with no look) and its text so far
|
|
187
|
+
let open = '';
|
|
188
|
+
let run = '';
|
|
189
|
+
for (const token of tokens) {
|
|
190
|
+
const name = token.kind === undefined ? undefined : kindClasses.get(token.kind);
|
|
191
|
+
let style = '';
|
|
192
|
+
if (token.color && reColor.test(token.color)) style += `color:${token.color};`;
|
|
193
|
+
if (token.italic) style += 'font-style:italic;';
|
|
194
|
+
if (token.bold) style += 'font-weight:600;';
|
|
195
|
+
const look = `${name ? ` class="${name}"` : ''}${style ? ` style="${escapeHtml(style)}"` : ''}`;
|
|
196
|
+
if (look !== open && !reBlank.test(token.text)) {
|
|
197
|
+
if (run) html += open ? `<span${open}>${run}</span>` : run;
|
|
198
|
+
open = look;
|
|
199
|
+
run = '';
|
|
200
|
+
}
|
|
201
|
+
run += escapeHtml(showHidden(token.text));
|
|
202
|
+
}
|
|
203
|
+
if (run) html += open ? `<span${open}>${run}</span>` : run;
|
|
204
|
+
markup.set(tokens, html);
|
|
205
|
+
return html;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The lines of a block that streams, as markup. They are kept in groups of 64. A line is
|
|
210
|
+
* drawn once and never again: a new token changes the line being written and nothing else,
|
|
211
|
+
* and a new line is one more in the last group, however long the block has grown. Each
|
|
212
|
+
* group is a block of its own, so the browser lays out that group and not the thousand
|
|
213
|
+
* lines above it. (The groups are an idea from Pierre's Words.)
|
|
214
|
+
*/
|
|
215
|
+
const GROUP = 64;
|
|
216
|
+
const NEWLINE = '\n';
|
|
217
|
+
|
|
218
|
+
/** The numbers of each full group of lines, made once: `1` to `64`, `65` to `128`… */
|
|
219
|
+
const fullNumbers: string[] = [];
|
|
220
|
+
|
|
221
|
+
/** Line numbers for `count` lines, in the same groups as the lines they number. */
|
|
222
|
+
function numbersFor(count: number): string[] {
|
|
223
|
+
const groups: string[] = [];
|
|
224
|
+
for (let first = 1; first <= count; first += GROUP) {
|
|
225
|
+
const last = Math.min(count, first + GROUP - 1);
|
|
226
|
+
const full = last === first + GROUP - 1;
|
|
227
|
+
const index = (first - 1) / GROUP;
|
|
228
|
+
let text = full ? fullNumbers[index] : undefined;
|
|
229
|
+
if (text === undefined) {
|
|
230
|
+
text = String(first);
|
|
231
|
+
for (let n = first + 1; n <= last; n++) text += NEWLINE + n;
|
|
232
|
+
if (full) fullNumbers[index] = text;
|
|
233
|
+
}
|
|
234
|
+
// A line ending closes every group but the last, as it does the lines beside it
|
|
235
|
+
groups.push(last < count ? text + NEWLINE : text);
|
|
236
|
+
}
|
|
237
|
+
return groups;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
type Group = { lines: string[] };
|
|
241
|
+
|
|
242
|
+
class Lines {
|
|
243
|
+
private done: Group[] = [];
|
|
244
|
+
private open: string[] = [];
|
|
245
|
+
|
|
246
|
+
add(line: string) {
|
|
247
|
+
if (this.open.length === GROUP) {
|
|
248
|
+
this.done.push({ lines: this.open });
|
|
249
|
+
this.open = [];
|
|
250
|
+
}
|
|
251
|
+
this.open.push(line);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* What to draw, with the line still being written after the whole ones. The last group
|
|
256
|
+
* is handed over as a new object each time, which is how the page knows to look at it
|
|
257
|
+
* again; the groups before it stay the objects they were, and are left alone.
|
|
258
|
+
*/
|
|
259
|
+
view(tail: string): View {
|
|
260
|
+
return {
|
|
261
|
+
kind: 'lines',
|
|
262
|
+
groups: [...this.done, { lines: this.open }],
|
|
263
|
+
tail: tail === '' ? '' : (this.open.length > 0 ? NEWLINE : '') + tail
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// `text` and `whole` are a block that was whole when it was drawn: it goes in as one piece.
|
|
269
|
+
// One that streams gets `lines`, and keeps them when it ends, so that no line already on
|
|
270
|
+
// the page is drawn again.
|
|
271
|
+
type View =
|
|
272
|
+
| { kind: 'text'; text: string }
|
|
273
|
+
| { kind: 'whole'; html: string }
|
|
274
|
+
| { kind: 'lines'; groups: Group[]; tail: string };
|
|
275
|
+
|
|
276
|
+
const whole = new WeakMap<HighlightToken[][], string>();
|
|
277
|
+
|
|
278
|
+
/** All of a finished block's lines as markup, kept with them: a block drawn again costs nothing. */
|
|
279
|
+
function linesHtml(lines: HighlightToken[][]): string {
|
|
280
|
+
let html = whole.get(lines);
|
|
281
|
+
if (html === undefined) whole.set(lines, (html = lines.map(tokensHtml).join('\n')));
|
|
282
|
+
return html;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Finished blocks, highlighted: by highlighter, then by language and code. A long thread
|
|
287
|
+
* that draws its rows only while they are on screen mounts the same blocks again and
|
|
288
|
+
* again; they are tokenized once.
|
|
289
|
+
*/
|
|
290
|
+
const finished = new WeakMap<Highlighter, Map<string, HighlightToken[][] | null>>();
|
|
291
|
+
const CACHE_ENTRIES = 300;
|
|
292
|
+
const CACHE_MAX_CODE = 200_000;
|
|
293
|
+
|
|
294
|
+
function highlightWhole(
|
|
295
|
+
highlight: Highlighter,
|
|
296
|
+
code: string,
|
|
297
|
+
lang: string | null
|
|
298
|
+
): HighlightToken[][] | null {
|
|
299
|
+
if (code.length > CACHE_MAX_CODE) return highlight(code, lang);
|
|
300
|
+
let cache = finished.get(highlight);
|
|
301
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- a cache the page never reads
|
|
302
|
+
if (!cache) finished.set(highlight, (cache = new Map()));
|
|
303
|
+
const key = `${lang ?? ''}\0${code}`;
|
|
304
|
+
if (cache.has(key)) {
|
|
305
|
+
const hit = cache.get(key)!;
|
|
306
|
+
// Most recently used goes last, so the oldest is first to go
|
|
307
|
+
cache.delete(key);
|
|
308
|
+
cache.set(key, hit);
|
|
309
|
+
return hit;
|
|
310
|
+
}
|
|
311
|
+
const lines = highlight(code, lang);
|
|
312
|
+
if (cache.size >= CACHE_ENTRIES) cache.delete(cache.keys().next().value!);
|
|
313
|
+
cache.set(key, lines);
|
|
314
|
+
return lines;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* The buttons of a code block, as markup: each a button and its icon. They have no
|
|
319
|
+
* handlers of their own. The document's root does what they are for (`partClicked` in
|
|
320
|
+
* `Markdown.svelte`), for a block drawn by this component as for one that `static` wrote,
|
|
321
|
+
* so a reply with thirty code blocks does not make thirty copy buttons out of components.
|
|
322
|
+
*/
|
|
323
|
+
function actionsHtml(
|
|
324
|
+
title: string | null,
|
|
325
|
+
labels: { copyCode: string; downloadCode: string },
|
|
326
|
+
download: boolean
|
|
327
|
+
): string {
|
|
328
|
+
const label = (words: string) => escapeHtml(title ? `${words}: ${title}` : words);
|
|
329
|
+
return (
|
|
330
|
+
(download
|
|
331
|
+
? actionHtml({
|
|
332
|
+
label: label(labels.downloadCode),
|
|
333
|
+
attributes: ' data-markdown-action="download"',
|
|
334
|
+
bare: true,
|
|
335
|
+
inner: glyphHtml('download')
|
|
336
|
+
})
|
|
337
|
+
: '') +
|
|
338
|
+
copyHtml({
|
|
339
|
+
label: label(labels.copyCode),
|
|
340
|
+
attributes: ' data-markdown-action="copy"',
|
|
341
|
+
sx: styles.copy
|
|
342
|
+
})
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* A finished code block as markup, as this component draws one that was whole from the
|
|
348
|
+
* start: for `static`. Its buttons carry `data-markdown-action`, and the block its id, so
|
|
349
|
+
* that the document's root can do for them what their own handlers would.
|
|
350
|
+
*/
|
|
351
|
+
export function codeHtml(
|
|
352
|
+
node: CodeBlock,
|
|
353
|
+
options: {
|
|
354
|
+
labels: { copyCode: string; downloadCode: string };
|
|
355
|
+
highlight: Highlighter | null;
|
|
356
|
+
lineNumbers: boolean;
|
|
357
|
+
download: boolean;
|
|
358
|
+
}
|
|
359
|
+
): string {
|
|
360
|
+
const lang = node.lang;
|
|
361
|
+
const code = node.value.replace(/\n$/, '');
|
|
362
|
+
const title = codeTitle(node);
|
|
363
|
+
const bare = title === null && lang === null;
|
|
364
|
+
let lines: HighlightToken[][] | null = null;
|
|
365
|
+
if (options.highlight) {
|
|
366
|
+
try {
|
|
367
|
+
lines = highlightWhole(options.highlight, code, lang);
|
|
368
|
+
} catch {
|
|
369
|
+
// A highlighter that throws leaves the code plain
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
const body = lines ? linesHtml(lines) : escapeHtml(showHidden(code));
|
|
373
|
+
let gutter = '';
|
|
374
|
+
const numbered = options.lineNumbers && lang !== null;
|
|
375
|
+
if (numbered) {
|
|
376
|
+
let count = 1;
|
|
377
|
+
for (let at = code.indexOf(NEWLINE); at !== -1; at = code.indexOf(NEWLINE, at + 1)) count++;
|
|
378
|
+
const linesClass = classes(styles.lines);
|
|
379
|
+
gutter =
|
|
380
|
+
`<span aria-hidden="true" data-markdown-chrome="true" class="${classes(styles.numbers)}">` +
|
|
381
|
+
numbersFor(count)
|
|
382
|
+
.map((group) => `<span class="${linesClass}">${group}</span>`)
|
|
383
|
+
.join('') +
|
|
384
|
+
'</span>';
|
|
385
|
+
}
|
|
386
|
+
const named = title ? escapeHtml(title) : '';
|
|
387
|
+
return (
|
|
388
|
+
`<div data-markdown-code="true" data-markdown-block="${node.id}" class="${classes(prose.block, styles.frame, bare && styles.bare)}">` +
|
|
389
|
+
`<div data-markdown-chrome="true" class="${classes(styles.header, bare && styles.bareHeader)}">` +
|
|
390
|
+
`<span${title && lang ? ` title="${escapeHtml(lang)}"` : ''} class="${classes(styles.label)}">` +
|
|
391
|
+
// (with the space the template has between the icon's place and the words)
|
|
392
|
+
(title ? glyphHtml('file', 12) : '') +
|
|
393
|
+
' ' +
|
|
394
|
+
(named || escapeHtml(lang ?? '')) +
|
|
395
|
+
'</span>' +
|
|
396
|
+
`<span class="${classes(styles.actions)}">` +
|
|
397
|
+
actionsHtml(title, options.labels, options.download) +
|
|
398
|
+
'</span></div>' +
|
|
399
|
+
`<pre dir="ltr" data-markdown-scroll class="${classes(styles.pre, lang === null && styles.laidOut, lang === null && styles.wrap, bare && styles.barePre, numbered && styles.numbered)}"${lang === null ? '' : ` style="--narvi-code-lines: ${countLines(code)};"`}>` +
|
|
400
|
+
gutter +
|
|
401
|
+
`<code class="${classes(styles.code, numbered && styles.numberedCode)}">${body}</code></pre></div>`
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
</script>
|
|
405
|
+
|
|
406
|
+
<script lang="ts">
|
|
407
|
+
let { node, enter = false }: MarkdownCodeProps = $props();
|
|
408
|
+
|
|
409
|
+
const md = getMarkdownContext();
|
|
410
|
+
|
|
411
|
+
// Still arriving: its fence has not closed, and the document is being written
|
|
412
|
+
const live = $derived(node.open && md.streaming());
|
|
413
|
+
// While the fence's own line is still being written, its language is not known yet:
|
|
414
|
+
// "c", "cs", "css". It counts once the first line of code has started.
|
|
415
|
+
const named = $derived(!live || node.value !== '');
|
|
416
|
+
const lang = $derived(named ? node.lang : null);
|
|
417
|
+
// The code without the line ending that closes it
|
|
418
|
+
const code = $derived(node.value.replace(/\n$/, ''));
|
|
419
|
+
const title = $derived(named ? codeTitle(node) : null);
|
|
420
|
+
const bare = $derived(title === null && lang === null);
|
|
421
|
+
|
|
422
|
+
// A block that streams is highlighted as it arrives: the stream keeps its place, so only
|
|
423
|
+
// new lines are tokenized, and the lines already drawn are never drawn again
|
|
424
|
+
let session: {
|
|
425
|
+
stream: HighlightStream;
|
|
426
|
+
lang: string | null;
|
|
427
|
+
fed: string;
|
|
428
|
+
lines: Lines;
|
|
429
|
+
ended: boolean;
|
|
430
|
+
} | null = null;
|
|
431
|
+
|
|
432
|
+
// A block that streams with no highlighter to feed keeps its lines the same way
|
|
433
|
+
let plain: { fed: string; lines: Lines } | null = null;
|
|
434
|
+
|
|
435
|
+
// A highlighter that throws must not take the code with it: from then on the block is plain
|
|
436
|
+
let broken = false;
|
|
437
|
+
|
|
438
|
+
function drop() {
|
|
439
|
+
if (session && !session.ended) {
|
|
440
|
+
try {
|
|
441
|
+
session.stream.dispose();
|
|
442
|
+
} catch {
|
|
443
|
+
// Nothing more to free
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
session = null;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const view = $derived.by((): View => {
|
|
450
|
+
if (!broken) {
|
|
451
|
+
try {
|
|
452
|
+
const coloured = highlight();
|
|
453
|
+
if (coloured) return coloured;
|
|
454
|
+
} catch {
|
|
455
|
+
broken = true;
|
|
456
|
+
drop();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return uncoloured();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
function highlight(): View | null {
|
|
463
|
+
const value = node.value;
|
|
464
|
+
// A session is for one run of code that only grows. Another language, other code, or
|
|
465
|
+
// code that carries on after the session ended (a stream that paused and resumed)
|
|
466
|
+
// starts afresh. (Compared as slices, which the engine does a block at a time;
|
|
467
|
+
// `startsWith` reads every character, and this runs on every update.)
|
|
468
|
+
if (
|
|
469
|
+
session &&
|
|
470
|
+
(session.lang !== lang ||
|
|
471
|
+
value.length < session.fed.length ||
|
|
472
|
+
value.slice(0, session.fed.length) !== session.fed ||
|
|
473
|
+
(session.ended && (live || value !== session.fed)))
|
|
474
|
+
) {
|
|
475
|
+
drop();
|
|
476
|
+
}
|
|
477
|
+
if (!session && live && named && md.highlightStream) {
|
|
478
|
+
const stream = md.highlightStream(lang);
|
|
479
|
+
if (stream) session = { stream, lang, fed: '', lines: new Lines(), ended: false };
|
|
480
|
+
}
|
|
481
|
+
if (session) {
|
|
482
|
+
plain = null;
|
|
483
|
+
if (!session.ended) {
|
|
484
|
+
const delta = value.slice(session.fed.length);
|
|
485
|
+
if (delta) {
|
|
486
|
+
for (const line of session.stream.push(delta)) session.lines.add(tokensHtml(line));
|
|
487
|
+
session.fed = value;
|
|
488
|
+
}
|
|
489
|
+
if (!live) {
|
|
490
|
+
const rest = session.stream.end();
|
|
491
|
+
// Code that ends with a line ending has no line after it
|
|
492
|
+
if (value.endsWith('\n') && rest.length > 0 && rest[rest.length - 1]!.length === 0)
|
|
493
|
+
rest.pop();
|
|
494
|
+
for (const line of rest) session.lines.add(tokensHtml(line));
|
|
495
|
+
session.ended = true;
|
|
496
|
+
session.stream.dispose();
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (session.ended) return session.lines.view('');
|
|
500
|
+
// The line being written is coloured on its own, without the lines above it: right
|
|
501
|
+
// almost always, and put right when the line ends
|
|
502
|
+
const text = value.slice(value.lastIndexOf('\n') + 1);
|
|
503
|
+
let tail: HighlightToken[] = text ? [{ text }] : [];
|
|
504
|
+
if (text && md.highlight) tail = md.highlight(text, lang)?.[0] ?? tail;
|
|
505
|
+
return session.lines.view(tokensHtml(tail));
|
|
506
|
+
}
|
|
507
|
+
// Without a stream to feed, a block still being written waits for its fence to close
|
|
508
|
+
if (live || !md.highlight) return null;
|
|
509
|
+
const lines = highlightWhole(md.highlight, code, lang);
|
|
510
|
+
if (!lines) return null;
|
|
511
|
+
plain = null;
|
|
512
|
+
return { kind: 'whole', html: linesHtml(lines) };
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** The code with no colour: one piece when it was whole from the start, lines when it streams. */
|
|
516
|
+
function uncoloured(): View {
|
|
517
|
+
if (!live && !plain) return { kind: 'text', text: showHidden(code) };
|
|
518
|
+
// Only what arrived since the last look is read: the lines before it are kept
|
|
519
|
+
const value = node.value;
|
|
520
|
+
if (
|
|
521
|
+
plain &&
|
|
522
|
+
(value.length < plain.fed.length || value.slice(0, plain.fed.length) !== plain.fed)
|
|
523
|
+
)
|
|
524
|
+
plain = null;
|
|
525
|
+
plain ??= { fed: '', lines: new Lines() };
|
|
526
|
+
let from = plain.fed.length;
|
|
527
|
+
for (let at = value.indexOf('\n', from); at !== -1; at = value.indexOf('\n', from)) {
|
|
528
|
+
plain.lines.add(escapeHtml(showHidden(value.slice(from, at))));
|
|
529
|
+
from = at + 1;
|
|
530
|
+
}
|
|
531
|
+
plain.fed = value.slice(0, from);
|
|
532
|
+
return plain.lines.view(escapeHtml(showHidden(value.slice(from))));
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
$effect(() => drop);
|
|
536
|
+
|
|
537
|
+
// What the block is as tall as before it is laid out (see `styles.pre`). A block still
|
|
538
|
+
// being written is on the screen, and laid out as it grows.
|
|
539
|
+
const lineCount = $derived(live || node.lang === null ? undefined : countLines(code));
|
|
540
|
+
|
|
541
|
+
// Lines are numbered only where they do not wrap: a number per line means nothing beside
|
|
542
|
+
// a line that runs onto three
|
|
543
|
+
const numbers = $derived.by((): string[] | null => {
|
|
544
|
+
if (!md.code().lineNumbers || node.lang === null) return null;
|
|
545
|
+
let count = 1;
|
|
546
|
+
if (view.kind === 'lines') {
|
|
547
|
+
// The whole lines, and the one being written
|
|
548
|
+
const whole = (view.groups.length - 1) * GROUP + view.groups.at(-1)!.lines.length;
|
|
549
|
+
count = Math.max(1, whole + (view.tail === '' ? 0 : 1));
|
|
550
|
+
} else {
|
|
551
|
+
for (let at = code.indexOf(NEWLINE); at !== -1; at = code.indexOf(NEWLINE, at + 1)) count++;
|
|
552
|
+
}
|
|
553
|
+
return numbersFor(count);
|
|
554
|
+
});
|
|
555
|
+
</script>
|
|
556
|
+
|
|
557
|
+
<div
|
|
558
|
+
data-markdown-code
|
|
559
|
+
data-markdown-block={node.id}
|
|
560
|
+
data-open={live || undefined}
|
|
561
|
+
{...attrs([prose.block, styles.frame, bare && styles.bare, enter && prose.enter])}
|
|
562
|
+
>
|
|
563
|
+
<!-- Always there, so the code does not move down when the fence closes and the button arrives -->
|
|
564
|
+
<div data-markdown-chrome {...attrs([styles.header, bare && styles.bareHeader])}>
|
|
565
|
+
<!-- A file name the fence gives stands in for the language -->
|
|
566
|
+
<span {...attrs([styles.label])} title={title ? (lang ?? undefined) : undefined}>
|
|
567
|
+
{#if title}<Glyph name="file" size={12} />{/if}
|
|
568
|
+
{title ?? lang ?? ''}
|
|
569
|
+
</span>
|
|
570
|
+
{#if !live}
|
|
571
|
+
<!-- Markup, pressed through the document's root: see `actionsHtml` -->
|
|
572
|
+
<!-- eslint-disable svelte/no-at-html-tags -->
|
|
573
|
+
<span {...attrs([styles.actions])}
|
|
574
|
+
>{@html actionsHtml(title, md.labels, md.code().download)}</span
|
|
575
|
+
>
|
|
576
|
+
<!-- eslint-enable svelte/no-at-html-tags -->
|
|
577
|
+
{/if}
|
|
578
|
+
</div>
|
|
579
|
+
<!-- Narvi's own markup around escaped text: see `tokensHtml` -->
|
|
580
|
+
<!-- eslint-disable svelte/no-at-html-tags -->
|
|
581
|
+
<!-- prettier-ignore -->
|
|
582
|
+
<pre dir="ltr" use:scrollable style:--narvi-code-lines={lineCount} {...attrs([styles.pre, (live || node.lang === null) && styles.laidOut, node.lang === null && styles.wrap, bare && styles.barePre, numbers !== null && styles.numbered])}>{#if numbers}<span aria-hidden="true" data-markdown-chrome {...attrs([styles.numbers])}>{#each numbers as group, g (g)}<span {...attrs([styles.lines])}>{group}</span>{/each}</span>{/if}<code {...attrs([styles.code, numbers !== null && styles.numberedCode])}>{#if view.kind === 'text'}{view.text}{:else if view.kind === 'whole'}{@html view.html}{:else}{#each view.groups as group, g (g)}<span data-markdown-lines {...attrs([styles.lines])}>{#each group.lines as line, i (i)}{@html i > 0 ? NEWLINE + line : line}{/each}{#if g === view.groups.length - 1}{@html view.tail}{:else}{NEWLINE}{/if}</span>{/each}{/if}</code></pre>
|
|
583
|
+
</div>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CodeBlock } from '../../markdown/ast.js';
|
|
2
|
+
import type { Highlighter } from '../../markdown/plugin.js';
|
|
3
|
+
export type MarkdownCodeProps = {
|
|
4
|
+
node: CodeBlock;
|
|
5
|
+
enter?: boolean;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* A finished code block as markup, as this component draws one that was whole from the
|
|
9
|
+
* start: for `static`. Its buttons carry `data-markdown-action`, and the block its id, so
|
|
10
|
+
* that the document's root can do for them what their own handlers would.
|
|
11
|
+
*/
|
|
12
|
+
export declare function codeHtml(node: CodeBlock, options: {
|
|
13
|
+
labels: {
|
|
14
|
+
copyCode: string;
|
|
15
|
+
downloadCode: string;
|
|
16
|
+
};
|
|
17
|
+
highlight: Highlighter | null;
|
|
18
|
+
lineNumbers: boolean;
|
|
19
|
+
download: boolean;
|
|
20
|
+
}): string;
|
|
21
|
+
declare const MarkdownCode: import("svelte").Component<MarkdownCodeProps, {}, "">;
|
|
22
|
+
type MarkdownCode = ReturnType<typeof MarkdownCode>;
|
|
23
|
+
export default MarkdownCode;
|