@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,452 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: markdown
|
|
3
|
+
description: >
|
|
4
|
+
Use when rendering Markdown with @diegopluna/narvi, especially an AI reply that streams: the
|
|
5
|
+
Markdown component, streaming and tidy, plugins (transforms, components by node type,
|
|
6
|
+
highlighters), the link and image policy, raw HTML, and parsing with MarkdownStream or parse
|
|
7
|
+
from @diegopluna/narvi/markdown.
|
|
8
|
+
metadata:
|
|
9
|
+
purpose: >
|
|
10
|
+
Render Markdown, streamed or finished, with Narvi's own incremental CommonMark and GFM
|
|
11
|
+
renderer: keep finished blocks from being drawn again, extend it with plugins instead of
|
|
12
|
+
patching the DOM, and decide which links and images text from a model may load.
|
|
13
|
+
type: core
|
|
14
|
+
library: '@diegopluna/narvi'
|
|
15
|
+
library_version: '0.1.0'
|
|
16
|
+
requires:
|
|
17
|
+
- setup
|
|
18
|
+
sources:
|
|
19
|
+
- src/lib/components/markdown/**
|
|
20
|
+
- src/lib/markdown/**
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
# Render Markdown with Narvi
|
|
24
|
+
|
|
25
|
+
This assumes Narvi is installed and its styles load (see the `setup` skill). Narvi renders
|
|
26
|
+
Markdown with its own parser: CommonMark 0.31.2 plus GFM (tables, task lists, strikethrough,
|
|
27
|
+
bare links, footnotes) and GitHub's alerts. It departs from CommonMark in one place, for
|
|
28
|
+
Chinese, Japanese and Korean text: emphasis closes beside their punctuation, so
|
|
29
|
+
`**テスト。**次` is bold, as people writing without spaces expect. It needs no other Markdown
|
|
30
|
+
library. Don't add `marked`,
|
|
31
|
+
`svelte-streamdown` or `{@html}` next to it.
|
|
32
|
+
|
|
33
|
+
## Setup
|
|
34
|
+
|
|
35
|
+
```svelte
|
|
36
|
+
<script lang="ts">
|
|
37
|
+
import { Markdown } from '@diegopluna/narvi';
|
|
38
|
+
|
|
39
|
+
let { text, done }: { text: string; done: boolean } = $props();
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<Markdown value={text} streaming={!done} />
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
- `value` is **all the text so far**, not the latest chunk. Pass the whole string on every
|
|
46
|
+
update; when it continues the previous value, only the new part is read.
|
|
47
|
+
- `streaming` is true while the text is still being written. When it turns false, the document
|
|
48
|
+
finishes in place: blocks keep their elements, nothing is drawn again, and the page doesn't jump.
|
|
49
|
+
- Sizes are in `em`, so the document takes the font size of its container. Set the size on the
|
|
50
|
+
container or with `sx`, for example `sx={styles.reply}` with `fontSize: font.sizeReading`.
|
|
51
|
+
- Inside a chat, put it in `Message.Content`:
|
|
52
|
+
`<Message.Content><Markdown value={reply} streaming={!done} /></Message.Content>`.
|
|
53
|
+
|
|
54
|
+
Other props:
|
|
55
|
+
|
|
56
|
+
- `breaks` makes a line ending inside a paragraph break the line, as chat messages are usually
|
|
57
|
+
meant. Off by default, as Markdown has it. Use it for text a person typed, not for a model's.
|
|
58
|
+
- `headingOffset={2}` moves headings down two levels, so `#` is an `h3` under the page's own
|
|
59
|
+
headings. They keep the size they're written with.
|
|
60
|
+
- `ontaskchange={(item, checked) => …}` makes task items tickable. Without it the boxes are
|
|
61
|
+
shown but can't be ticked. `item.checkedAt` is where the item's `[` is in `value`: write `x`
|
|
62
|
+
or a space at `checkedAt + 1` and pass the new text back. Return `false`, or reject, and the
|
|
63
|
+
box goes back.
|
|
64
|
+
- `labels` rewords or translates what the controls show and say (the copy buttons, the table
|
|
65
|
+
menu, "Load image from" and "Load video from", the alert titles): `labels={{ copyCode: 'Copiar
|
|
66
|
+
código' }}`. The type is `MarkdownLabels`, exported from `@diegopluna/narvi`.
|
|
67
|
+
- `singleTilde` makes `~text~` strike through. It's off, so `~5 minutes` and `~/path` stay as
|
|
68
|
+
written; `~~text~~` always strikes through.
|
|
69
|
+
- `frontmatter` reads YAML front matter at the top of a file (`---`, `key: value` lines, `---`)
|
|
70
|
+
and leaves it out; without it, front matter is drawn as a rule and a heading. Turn it on for
|
|
71
|
+
Markdown files, not for replies: a reply that starts with `---` starts with a rule.
|
|
72
|
+
- `caret` shows a caret at the end of the text while it streams (`caret="circle"` for a dot).
|
|
73
|
+
`animated` fades text in a few words at a time as it arrives, where a new block otherwise
|
|
74
|
+
fades in whole. Both are off by default and do nothing once `streaming` is false.
|
|
75
|
+
- `lineNumbers` numbers the lines of code blocks that have a language, and `download` gives
|
|
76
|
+
code blocks a button that saves the code, named by the fence's file name or its language.
|
|
77
|
+
- `static` says whether finished blocks are written as markup, not built from parts: about
|
|
78
|
+
twice as fast to show, in a fifth of the memory. Leave it out. Narvi then writes markup for
|
|
79
|
+
text that was whole when the component was made and has not changed since (a reply loaded
|
|
80
|
+
from history), and draws parts for text that streams or is edited here, which change in
|
|
81
|
+
place. `static={false}` never writes markup, and `static` (true) does whenever nothing
|
|
82
|
+
streams. Prose, lists, tasks, quotes, code blocks, tables and file chips are written so,
|
|
83
|
+
and their buttons, links and boxes still do what they do (`links.open`, `ontaskchange`, the
|
|
84
|
+
`open` of `files()`). Images, formulas, diagrams, footnotes, alerts and raw HTML are drawn
|
|
85
|
+
by their parts as ever, with the block they are in. Markup written for a text is kept and
|
|
86
|
+
used again when the same text is shown with the same props and the same plugin objects
|
|
87
|
+
(a virtualised thread scrolling back), so keep `plugins` and `labels` the same objects
|
|
88
|
+
across rows: a new array of the same plugins is fine, new plugin objects are not. A plugin
|
|
89
|
+
whose output depends on something besides the text must be made again when that changes.
|
|
90
|
+
- `tidy={false}` and `gfm={false}` turn off the tidy preview and the GFM extensions.
|
|
91
|
+
|
|
92
|
+
A quote that starts with `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]`
|
|
93
|
+
is drawn as an alert with that title. Headings get the ids GitHub gives them, made unique to
|
|
94
|
+
each document, and a `#heading` link scrolls to its heading without changing the page's address.
|
|
95
|
+
|
|
96
|
+
In a thread, a reply that streams and a reply loaded from history take the same props. The
|
|
97
|
+
one loaded finished is written as markup, and the one that streams keeps the parts it was
|
|
98
|
+
drawn with after it ends:
|
|
99
|
+
|
|
100
|
+
```svelte
|
|
101
|
+
<script lang="ts">
|
|
102
|
+
import { Markdown } from '@diegopluna/narvi';
|
|
103
|
+
|
|
104
|
+
type Turn = { text: string; done: boolean };
|
|
105
|
+
let { turn }: { turn: Turn } = $props();
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<Markdown value={turn.text} streaming={!turn.done} caret animated />
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## How streaming behaves
|
|
112
|
+
|
|
113
|
+
- A finished block keeps its object, and is keyed by a stable id, so Svelte never re-renders
|
|
114
|
+
it. Only the last, unfinished block changes as text arrives.
|
|
115
|
+
- With `tidy` (on by default), the unfinished end reads as it will. `**bold` shows bold, an
|
|
116
|
+
open code span runs to the end, and a link waiting for its address is drawn as a link
|
|
117
|
+
already, with its words and nowhere to go yet (a `link` node with `pending: true` and an
|
|
118
|
+
empty `url`), so it doesn't turn from text into a link when its address ends. A bare `**`, a
|
|
119
|
+
half-written tag, entity or image, a line that so far is only a marker (`-`, `#`, `1.`) and
|
|
120
|
+
a table's header row before its delimiter row are held back. Text that is complete is never tidied.
|
|
121
|
+
- Blocks that arrive while streaming fade in. A reply that is already written appears at once.
|
|
122
|
+
- Footnotes are numbered as soon as they're referenced. If the finished text never defines one,
|
|
123
|
+
its reference shows as text.
|
|
124
|
+
|
|
125
|
+
Don't throttle to whole paragraphs to avoid flicker: the tidy step does that job. Batching
|
|
126
|
+
updates to about once per frame (or every 50 ms) is still worthwhile for very fast streams.
|
|
127
|
+
|
|
128
|
+
`streaming` may turn off and on again, for a reply that pauses for a tool call and carries on:
|
|
129
|
+
text that continues the old value is still read from where it stopped.
|
|
130
|
+
|
|
131
|
+
## First-party plugins
|
|
132
|
+
|
|
133
|
+
Three plugins ship on their own entry points, so an app only loads what it imports. Create the
|
|
134
|
+
plugin array once and reuse it: in a module script, or in the component's script when a plugin
|
|
135
|
+
needs the component's state. Never write a new array in the markup:
|
|
136
|
+
|
|
137
|
+
```svelte
|
|
138
|
+
<script lang="ts" module>
|
|
139
|
+
import { diagrams } from '@diegopluna/narvi/markdown/diagrams';
|
|
140
|
+
import { highlight } from '@diegopluna/narvi/markdown/highlight';
|
|
141
|
+
import { math } from '@diegopluna/narvi/markdown/math';
|
|
142
|
+
|
|
143
|
+
const plugins = [highlight(), math(), diagrams()];
|
|
144
|
+
</script>
|
|
145
|
+
|
|
146
|
+
<script lang="ts">
|
|
147
|
+
import { Markdown } from '@diegopluna/narvi';
|
|
148
|
+
|
|
149
|
+
let { text, done }: { text: string; done: boolean } = $props();
|
|
150
|
+
</script>
|
|
151
|
+
|
|
152
|
+
<Markdown value={text} streaming={!done} {plugins} />
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
- **`highlight()`** colours code in 152 languages with `@pierre/highlights`. A block that
|
|
156
|
+
streams is tokenized line by line as it arrives, never from the top again. Code takes Narvi's
|
|
157
|
+
syntax colours by default. Pass `highlight({ themes: { light, dark } })` with Zed-compatible
|
|
158
|
+
themes (for example from `@pierre/highlights/themes`) to use an editor theme. Importing the
|
|
159
|
+
module loads about 150 KB (gzipped) of WebAssembly. To keep it out of the first load, import
|
|
160
|
+
it in the browser and add it to the plugins once, when it arrives (see below).
|
|
161
|
+
- **`math()`** turns on formulas (`$x$`, `$$x$$`, `\(x\)`, `\[x\]` and a fenced `math` block)
|
|
162
|
+
and typesets them with KaTeX, which loads on the first formula. Prices and shell variables
|
|
163
|
+
stay text: "It costs $5 and $10" and `$HOME/bin:$PATH` are not formulas, and in "$5 and $10,
|
|
164
|
+
with $n$ tries" only `$n$` is one. A formula shows as
|
|
165
|
+
its TeX source until it closes, and when KaTeX can't read it. A `$$` or `\[` on a line of its
|
|
166
|
+
own that meets a blank line before its closing delimiter was not a formula, and is text.
|
|
167
|
+
`\[a-z\]` is a regex's escaped brackets, and `f\(x\)` is "f(x)". Without this plugin, a
|
|
168
|
+
dollar sign is always text.
|
|
169
|
+
- **`diagrams()`** draws a fenced `mermaid` block as a diagram, with Pele, in Narvi's colours.
|
|
170
|
+
It draws once the fence closes, offers the code and a full-size view that pans and zooms,
|
|
171
|
+
and says why when a diagram can't be drawn.
|
|
172
|
+
|
|
173
|
+
Two more are for replies from coding agents:
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import { files } from '@diegopluna/narvi/markdown/files';
|
|
177
|
+
import { images } from '@diegopluna/narvi/markdown/images';
|
|
178
|
+
|
|
179
|
+
declare const tree: { find(path: string): string | null };
|
|
180
|
+
declare const editor: { open(path: string, line?: number): void };
|
|
181
|
+
|
|
182
|
+
export const plugins = [
|
|
183
|
+
files({
|
|
184
|
+
// Says which paths are real files, and returns them as the app wants them
|
|
185
|
+
resolve: (path) => tree.find(path),
|
|
186
|
+
open: (file) => editor.open(file.path, file.line)
|
|
187
|
+
}),
|
|
188
|
+
// No image loads unasked unless `allow` says so
|
|
189
|
+
images({ allow: (url) => url.startsWith('https://cdn.example.com/') })
|
|
190
|
+
];
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
- **`files()`** turns inline code that names a file into a chip: `src/app.ts`,
|
|
194
|
+
`src/app.ts:42`, `src/app.ts:42:7`, `src/app.ts:42-50` and `src/app.ts#L42-L50`. A Markdown
|
|
195
|
+
link whose address is a file path, with or without a `#L10` fragment, becomes a chip too.
|
|
196
|
+
It keeps the link's words when they're prose ("the config") or name that file; one word, or
|
|
197
|
+
words that name another file, are replaced by the real name, so a chip can't claim to be a
|
|
198
|
+
file it isn't. A chip made from code shows the file's name and the place in it (`app.ts:42`),
|
|
199
|
+
and has the whole path as its title. A path that leaves the project (`../x`, `~/x`) is shown
|
|
200
|
+
whole. Network shares, paths that start with `-`, and paths with invisible characters never
|
|
201
|
+
become chips. With `open` it's a button that calls
|
|
202
|
+
`open(file, event)`, where `file` is `{ path, line?, column?, endLine? }`. With `resolve`,
|
|
203
|
+
every inline code without spaces is asked, so a bare `package.json` can be found, and a path
|
|
204
|
+
it returns null for stays code. `resolve` receives the path without its `:42` or `#L42`, and
|
|
205
|
+
what it returns becomes `file.path`.
|
|
206
|
+
Without `resolve`, only text that looks like a path becomes a chip: "and/or", `v1.2.3` and
|
|
207
|
+
`github.com/owner/repo` don't.
|
|
208
|
+
- **`images()`** holds an image the app hasn't allowed: it shows as a button that names the
|
|
209
|
+
image's host, and loads when pressed. By default no image loads unasked, not even one from
|
|
210
|
+
the app's own site, since that's a request with the person's cookies. To let the app's own
|
|
211
|
+
site through, pass `images({ allow: sameOrigin })`, with `sameOrigin` from the same module.
|
|
212
|
+
`allow` is given the address as written; the app's `links.image` still decides which
|
|
213
|
+
addresses may load at all, and can send them through a proxy.
|
|
214
|
+
Use it whenever the Markdown comes from a model or from other people, since an image is
|
|
215
|
+
requested as soon as it's shown. An `<img>` in raw HTML is held behind the same button. The
|
|
216
|
+
button reads "Load image from" and the host; reword it with the `loadImage` label.
|
|
217
|
+
|
|
218
|
+
An app that serves its own images (attachments, screenshots an agent took) should allow
|
|
219
|
+
only where they live, not its whole site. Resolve the address first, so `..` can't walk out
|
|
220
|
+
of the folder:
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import { images } from '@diegopluna/narvi/markdown/images';
|
|
224
|
+
|
|
225
|
+
export const ownImages = images({
|
|
226
|
+
allow(url) {
|
|
227
|
+
const to = new URL(url, location.href);
|
|
228
|
+
return to.origin === location.origin && to.pathname.startsWith('/attachments/');
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
`url.startsWith('/attachments/')` alone would let `/attachments/../api/logout` through.
|
|
234
|
+
|
|
235
|
+
Tables need no plugin: each has a button, shown on hover or focus, that copies it as Markdown
|
|
236
|
+
or CSV or saves a CSV file. In the CSV, a cell a spreadsheet would run as a formula (one that
|
|
237
|
+
starts with `=`, `+`, `-` or `@`) is written as text. `tableToCsv` and `tableToMarkdown` from
|
|
238
|
+
`@diegopluna/narvi/markdown` do the same for a `table` node.
|
|
239
|
+
|
|
240
|
+
When a plugin needs the component's state, or should load late, build the array in the
|
|
241
|
+
component's script. A new array of the same plugin objects changes nothing, but a new plugin
|
|
242
|
+
object (calling `highlight()` again) transforms and draws the document again, so make each
|
|
243
|
+
plugin once. Hold the array in `$state.raw` or a plain `const`, never in deep `$state`, which
|
|
244
|
+
wraps each plugin in a proxy that is a new object to the cache:
|
|
245
|
+
|
|
246
|
+
```svelte
|
|
247
|
+
<script lang="ts">
|
|
248
|
+
import { Markdown } from '@diegopluna/narvi';
|
|
249
|
+
import type { MarkdownPlugin } from '@diegopluna/narvi/markdown';
|
|
250
|
+
import { files } from '@diegopluna/narvi/markdown/files';
|
|
251
|
+
|
|
252
|
+
let { text }: { text: string } = $props();
|
|
253
|
+
let opened = $state<string[]>([]);
|
|
254
|
+
|
|
255
|
+
// Made once, when the component is: the callback can use the component's state
|
|
256
|
+
const base: MarkdownPlugin[] = [files({ open: (file) => opened.push(file.path) })];
|
|
257
|
+
let plugins = $state.raw(base);
|
|
258
|
+
|
|
259
|
+
// The highlighter joins once, after the page has loaded. `$effect` runs in the browser only
|
|
260
|
+
$effect(() => {
|
|
261
|
+
void import('@diegopluna/narvi/markdown/highlight').then(({ highlight }) => {
|
|
262
|
+
plugins = [highlight(), ...base];
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
</script>
|
|
266
|
+
|
|
267
|
+
<Markdown value={text} {plugins} />
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Writing plugins
|
|
271
|
+
|
|
272
|
+
`plugins` takes `MarkdownPlugin` objects. A plugin can rewrite blocks after they're parsed
|
|
273
|
+
(`transform`, with `mapText` and `mapInlines`), draw a node type or a node of its own with a
|
|
274
|
+
Svelte component (`components`), draw a fenced language (`languages`), colour code
|
|
275
|
+
(`highlight`, `highlightStream`), decide links and images (`links`), and say how to write its
|
|
276
|
+
nodes as markup for `static` (`static`). Where two plugins overlap, the later one wins, one
|
|
277
|
+
piece at a time; transforms all run, in order.
|
|
278
|
+
|
|
279
|
+
Read the [plugin reference](references/plugins.md) before writing one. It's also at
|
|
280
|
+
`node_modules/@diegopluna/narvi/skills/markdown/references/plugins.md`.
|
|
281
|
+
|
|
282
|
+
Replace Narvi's rendering with a plugin, never by patching the rendered DOM with a
|
|
283
|
+
`MutationObserver`. Svelte owns those nodes, and edits made from outside are lost or break the
|
|
284
|
+
next update.
|
|
285
|
+
|
|
286
|
+
## Links, images and raw HTML
|
|
287
|
+
|
|
288
|
+
The default policy allows links to `http:`, `https:`, `mailto:`, `tel:` and addresses without a
|
|
289
|
+
scheme, and images from `http:`, `https:` and addresses without a scheme. A refused link shows
|
|
290
|
+
as its text; a refused image shows its alt text. Links to another origin open in a new tab.
|
|
291
|
+
When a link's words read as an address on one site and the link goes to another, the real host
|
|
292
|
+
is shown after the words, and a tooltip that names the other site is dropped.
|
|
293
|
+
|
|
294
|
+
**In a desktop app** (a page on `file:` or a scheme of its own, not `http:` or `https:`), an
|
|
295
|
+
address without a scheme is a file on the machine, and following it would load that file in
|
|
296
|
+
the app's window. There the default policy shows such a link as text, unless a plugin gives
|
|
297
|
+
`links.open` (then the app decides where it goes) or its own `links.href`. Links to a heading
|
|
298
|
+
of the same document always work. For links to files, use `files()`.
|
|
299
|
+
|
|
300
|
+
An address is judged as a browser will read it: tabs and line endings anywhere in it and
|
|
301
|
+
control characters at its ends are dropped first, so `java	script:` in raw HTML is refused
|
|
302
|
+
like `javascript:`. `//host/path` is allowed only when the page itself is on `http:` or
|
|
303
|
+
`https:`, and an address that starts with backslashes never is, so a reply can't reach a
|
|
304
|
+
network share from a desktop app. When you write your own `links.href` or `links.image`, ask
|
|
305
|
+
`readUrl` where the address goes instead of testing how it starts:
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import { readUrl, type MarkdownPlugin } from '@diegopluna/narvi/markdown';
|
|
309
|
+
|
|
310
|
+
const ownSiteOnly: MarkdownPlugin = {
|
|
311
|
+
name: 'own-site-only',
|
|
312
|
+
links: {
|
|
313
|
+
// `kind` is 'relative', 'network' (`//host`), 'scheme' or 'unsafe'
|
|
314
|
+
href(url) {
|
|
315
|
+
const read = readUrl(url);
|
|
316
|
+
return read.kind === 'relative' ? read.url : null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
Your function is handed the address already read that way, and a `javascript:` address is
|
|
323
|
+
refused whatever it returns. A test like `url.startsWith('/')` still lets `//evil.example` and `/\evil.example`
|
|
324
|
+
through. Test `readUrl(url).kind`, or parse the address with `new URL(url, location.href)` and
|
|
325
|
+
compare its `origin`.
|
|
326
|
+
|
|
327
|
+
To open links yourself (in the app's router, a side panel or an in-app browser), give
|
|
328
|
+
`links.open`. It's called on click, before the browser follows the link, with the address
|
|
329
|
+
that `links.href` returned (as written in the Markdown, unless your policy changed it). Links
|
|
330
|
+
to another origin open in a new tab; `event.preventDefault()` stops that too:
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { readUrl, type MarkdownPlugin } from '@diegopluna/narvi/markdown';
|
|
334
|
+
|
|
335
|
+
declare function navigate(href: string): void;
|
|
336
|
+
|
|
337
|
+
const router: MarkdownPlugin = {
|
|
338
|
+
name: 'router',
|
|
339
|
+
links: {
|
|
340
|
+
open(href, event) {
|
|
341
|
+
// Only addresses on this site; `//host` is another one, though it starts with `/`
|
|
342
|
+
if (readUrl(href).kind !== 'relative') return; // let the browser follow the rest
|
|
343
|
+
event.preventDefault();
|
|
344
|
+
navigate(href);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
**A remote image is requested as soon as it's shown**, so text from a model can send data
|
|
351
|
+
out through an image's address. When rendering model output, decide which hosts load:
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
import type { MarkdownPlugin } from '@diegopluna/narvi/markdown';
|
|
355
|
+
|
|
356
|
+
const safeImages: MarkdownPlugin = {
|
|
357
|
+
name: 'safe-images',
|
|
358
|
+
links: {
|
|
359
|
+
image: (url) => (url.startsWith('https://images.example.com/') ? url : null)
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Raw HTML goes through an allowlist and becomes real elements. HTML blocks are joined with the
|
|
365
|
+
Markdown they wrap, so the body of a `<details>` renders inside it. Formatting, tables, `details`,
|
|
366
|
+
`kbd`, `mark`, `img`, `video` and `a` stay; `style`, `class`, `id`, `name`, `on*` handlers, scripts,
|
|
367
|
+
iframes and forms are dropped. The same policy checks addresses in `href` and `src`. A tag that
|
|
368
|
+
isn't HTML, such as `<T>` or `<MyComponent>` in a sentence about code, shows as the text it was
|
|
369
|
+
written as.
|
|
370
|
+
|
|
371
|
+
Input can't hang the page: nesting stops at 64 levels of quotes and lists and 32 of emphasis
|
|
372
|
+
and links, a table at 128 columns, and a formula at 10,000 characters; a document's formulas
|
|
373
|
+
share a limit on how much they draw. Past a limit the text shows as written.
|
|
374
|
+
|
|
375
|
+
A `<video>` is drawn by the image component, as an `<img>` is: its node is an `image` with
|
|
376
|
+
`video: true` and the address of the video or of its first `<source>`. So `images()` holds it
|
|
377
|
+
behind the same button ("Load video from"), and a component you register for `image` receives
|
|
378
|
+
it. Shown, it has controls and fetches nothing until it's played; `autoplay`, `poster` and
|
|
379
|
+
`loop` are dropped. A transform can mark an image as a video itself, for a link to an `.mp4`,
|
|
380
|
+
by returning the image node with `video: true`.
|
|
381
|
+
|
|
382
|
+
In a code block, control characters and direction overrides are drawn as what they are (`␛`,
|
|
383
|
+
`⟨U+202E⟩`), so code can't read one way and run another, and the copy button leaves control
|
|
384
|
+
characters out.
|
|
385
|
+
|
|
386
|
+
## Without the component
|
|
387
|
+
|
|
388
|
+
`@diegopluna/narvi/markdown` exports the engine:
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
import { MarkdownStream, parse, toHtml } from '@diegopluna/narvi/markdown';
|
|
392
|
+
|
|
393
|
+
const doc = parse('# Hello\n\nSome *text*.');
|
|
394
|
+
const withMath = parse('Area $\\pi r^2$.', { math: true }); // options: gfm (on), math, singleTilde (off)
|
|
395
|
+
const file = parse('---\ntitle: Doors\n---\n# Hello', { frontmatter: true });
|
|
396
|
+
file.frontmatter; // 'title: Doors', as written: parse the YAML yourself
|
|
397
|
+
const html = toHtml(doc); // as the specs have it: raw HTML and any address pass through
|
|
398
|
+
const safe = toHtml(doc, { rawHtml: false, safeLinks: true }); // for text you don't trust
|
|
399
|
+
|
|
400
|
+
const stream = new MarkdownStream();
|
|
401
|
+
stream.append('Some **bo');
|
|
402
|
+
stream.snapshot({ tidy: true }); // the document so far, with the end tidied
|
|
403
|
+
stream.snapshot({ final: true }); // read as all there is, without ending the stream
|
|
404
|
+
stream.append('ld** text');
|
|
405
|
+
stream.finish(); // the final document
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Common mistakes
|
|
409
|
+
|
|
410
|
+
- **HIGH: passing only the latest chunk as `value`.** The component expects all the text so
|
|
411
|
+
far, so it shows only the chunk.
|
|
412
|
+
- **HIGH: rendering model output with `{@html}` or another Markdown library.** It loses
|
|
413
|
+
sanitising, stable blocks and tidy streaming. Use `Markdown`.
|
|
414
|
+
- **HIGH: leaving the default image policy on untrusted text.** Images load as soon as
|
|
415
|
+
they're shown. For model output, add `images()`, which makes them wait for a click, or set
|
|
416
|
+
`links.image` yourself. Either is enough.
|
|
417
|
+
- **HIGH: writing math without the `math()` plugin, or expecting `$5` to break with it.**
|
|
418
|
+
Formulas are off by default. With the plugin, a single dollar opens a formula only when no
|
|
419
|
+
space or comma follows it, a closing dollar is on the same line, and no letter or digit
|
|
420
|
+
follows that.
|
|
421
|
+
- **HIGH: making file paths clickable by patching the DOM, or by rewriting them into links.**
|
|
422
|
+
Use `files({ resolve, open })`. A Markdown link to a relative file path would otherwise
|
|
423
|
+
navigate the app to that path.
|
|
424
|
+
- **HIGH: testing how an address starts in your own link policy** (`url.startsWith('/')`,
|
|
425
|
+
`url.startsWith('https://cdn.example.com')`). `//evil.example`, `/\evil.example` and
|
|
426
|
+
`https://cdn.example.com.evil.example` pass those. Use `readUrl(url).kind`, or compare the
|
|
427
|
+
`origin` of `new URL(url, location.href)`.
|
|
428
|
+
- **MEDIUM: expecting relative links to work in a desktop app without `links.open`.** Off the
|
|
429
|
+
web they show as text by default. Give `links.open` and route them yourself.
|
|
430
|
+
- **MEDIUM: making new plugin objects on every render** (`plugins={[highlight()]}` in the
|
|
431
|
+
markup, or the array in deep `$state`). The caches are per plugin object, so every block is
|
|
432
|
+
transformed and highlighted again. Create each plugin once, in the module script or the
|
|
433
|
+
component's script, and hold the array in a `const` or `$state.raw`.
|
|
434
|
+
- **HIGH: writing a node's text into a `static` writer without `tools.escape`.** The markup is
|
|
435
|
+
put on the page as written, so text from a model would be read as markup. Escape every
|
|
436
|
+
value, including attribute values.
|
|
437
|
+
- **MEDIUM: turning `static` on the moment a reply finishes streaming.** Its blocks would be
|
|
438
|
+
drawn again, as markup this time. Leave `static` out: Narvi decides it once per reply, by
|
|
439
|
+
whether the text was whole when the component was made.
|
|
440
|
+
- **MEDIUM: reading a code block's text with `innerText` while it is off the screen.** Code
|
|
441
|
+
that is off the screen is not laid out yet, and `innerText` leaves out text that is not
|
|
442
|
+
laid out. Use `textContent`, or the text you gave `value`.
|
|
443
|
+
- **MEDIUM: turning `breaks` on for model output.** Models wrap lines in the middle of
|
|
444
|
+
sentences. It's for text a person typed.
|
|
445
|
+
- **MEDIUM: setting the task box in the DOM.** Change the text at `item.checkedAt + 1` in
|
|
446
|
+
`ontaskchange` and pass it back as `value`; the box follows the text.
|
|
447
|
+
|
|
448
|
+
## Completion
|
|
449
|
+
|
|
450
|
+
A streamed reply shows formatting as it arrives, without raw `**` or table pipes flashing, and
|
|
451
|
+
when it finishes, nothing on the page jumps. A link with a `javascript:` address renders as
|
|
452
|
+
text, and an image from a host you didn't allow shows its alt text.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Writing a Markdown plugin
|
|
2
|
+
|
|
3
|
+
For `<Markdown plugins={[…]}>` from `@diegopluna/narvi`. Read the `markdown` skill first: this
|
|
4
|
+
is the reference for the plugin object itself.
|
|
5
|
+
|
|
6
|
+
`plugins` takes `MarkdownPlugin` objects. Where two overlap, the later one wins, one piece at
|
|
7
|
+
a time: one component, one language, one function of `links`. So a plugin that sets only
|
|
8
|
+
`links.open` keeps another plugin's `links.image`. Transforms don't overlap: they all run, in order.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { mapText, type MarkdownPlugin } from '@diegopluna/narvi/markdown';
|
|
12
|
+
|
|
13
|
+
// `@thread:12` becomes a link to that thread
|
|
14
|
+
export const threadLinks: MarkdownPlugin = {
|
|
15
|
+
name: 'thread-links',
|
|
16
|
+
transform: (block) =>
|
|
17
|
+
mapText(block, (value) => {
|
|
18
|
+
if (!value.includes('@thread:')) return null;
|
|
19
|
+
return value.split(/(@thread:\d+)/).map((part) => {
|
|
20
|
+
const m = part.match(/^@thread:(\d+)$/);
|
|
21
|
+
return m
|
|
22
|
+
? {
|
|
23
|
+
type: 'link',
|
|
24
|
+
url: `/threads/${m[1]}`,
|
|
25
|
+
title: null,
|
|
26
|
+
kind: 'literal',
|
|
27
|
+
children: [{ type: 'text', value: `Thread ${m[1]}` }]
|
|
28
|
+
}
|
|
29
|
+
: { type: 'text', value: part };
|
|
30
|
+
});
|
|
31
|
+
})
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- `transform(block, document)` rewrites a block after it's parsed. It's called for the blocks
|
|
36
|
+
that hold text or code (paragraphs, headings, tables, code blocks), wherever they're nested,
|
|
37
|
+
and never for the lists, items and quotes around them. A finished block keeps its object, so
|
|
38
|
+
it's transformed once. Return the same block to change nothing. `mapText` is a helper for
|
|
39
|
+
rewriting text nodes that skips code, links and images.
|
|
40
|
+
- `components` replaces Narvi's component for a node type: `{ inlineCode: FileChip }`. The
|
|
41
|
+
component receives `{ node, children }`, where `children` is a snippet that renders the
|
|
42
|
+
node's contents as Narvi would. Node types are `paragraph`, `heading`, `list`, `code`,
|
|
43
|
+
`table`, `blockquote`, `thematicBreak`, `html`, `text`, `strong`, `emphasis`, `delete`,
|
|
44
|
+
`inlineCode`, `link`, `image`, `break`, `softBreak` and `footnoteReference`, plus `math` and
|
|
45
|
+
`inlineMath` with formula syntax on, and any type your transform adds.
|
|
46
|
+
- A plugin that throws takes down only its own part. A component that throws falls back to
|
|
47
|
+
Narvi's drawing of that node, a transform that throws is skipped, a highlighter that throws
|
|
48
|
+
leaves the code plain, and a `links` function that throws refuses the link or image.
|
|
49
|
+
- `highlight(code, lang)` returns lines of `{ text, kind }` tokens, or null for plain code.
|
|
50
|
+
The kinds Narvi colours are `keyword`, `string`, `number`, `constant`, `comment`,
|
|
51
|
+
`function`, `type`, `property`, `tag`, `attribute`, `punctuation`, `operator`, `inserted`
|
|
52
|
+
and `deleted`.
|
|
53
|
+
- `highlightStream(lang)` returns `{ push(chunk), end(), dispose() }` for code that arrives in
|
|
54
|
+
pieces, or null for a language it doesn't know. Without it, a streaming block is highlighted
|
|
55
|
+
once its fence closes.
|
|
56
|
+
- `languages` gives a component for fenced code in a language: `{ mermaid: Diagram }`. It
|
|
57
|
+
takes over once the fence has closed and receives `{ node }`, a `code` node with `lang`,
|
|
58
|
+
`meta` and `value`.
|
|
59
|
+
- `syntax: { math: true }` turns on formula syntax, which adds `math` and `inlineMath` nodes.
|
|
60
|
+
- A transform can add its own inline node: `{ type: 'custom', name: 'mention', data: {…},
|
|
61
|
+
children: [] }`. The component registered under that `name` in `components` draws it and
|
|
62
|
+
receives `{ node, children }`. `mapInlines(block, (node, { inLink }) => …)` visits every inline
|
|
63
|
+
node; `mapText` is the same for text only.
|
|
64
|
+
- `static` says how to write a node your plugin draws as markup, for text that is written
|
|
65
|
+
as markup (text that was whole when it was first shown, unless `static={false}`). Without
|
|
66
|
+
it, a block that holds your node is built from parts, which is correct and slower. It is used only while your component is the one that draws the node:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import type { InlineCustom, MarkdownPlugin } from '@diegopluna/narvi/markdown';
|
|
70
|
+
import Mention from './Mention.svelte';
|
|
71
|
+
|
|
72
|
+
declare function openProfile(who: string): void;
|
|
73
|
+
|
|
74
|
+
export const mentions: MarkdownPlugin = {
|
|
75
|
+
name: 'mentions',
|
|
76
|
+
components: { mention: Mention },
|
|
77
|
+
static: {
|
|
78
|
+
mention: {
|
|
79
|
+
// `tools.part` marks the element whose clicks come back to `click`
|
|
80
|
+
html: (node: InlineCustom, tools) =>
|
|
81
|
+
`<button type="button"${tools.part} data-who="${tools.escape(String(node.data.who))}">` +
|
|
82
|
+
`@${tools.escape(String(node.data.who))}</button>`,
|
|
83
|
+
click: (element) => openProfile(element.dataset.who!)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
What `html` returns is put on the page as it is. Put every piece of text and every
|
|
90
|
+
attribute value from the node through `tools.escape`, and write the same elements your
|
|
91
|
+
component draws. Return null for a node only the component can draw.
|
|
92
|
+
|
|
93
|
+
- `withProps(Component, props)` registers a component with extra props:
|
|
94
|
+
`components: { mention: withProps(Mention, { onopen }) }`.
|
|
95
|
+
- Give an element `data-markdown-copy="…"` to say what a copy of it holds. That's how a formula
|
|
96
|
+
copies as its TeX and a file chip as its path. `Markdown` handles copies that start inside
|
|
97
|
+
it. For a selection that starts above a reply and runs into it, put the same handler on what
|
|
98
|
+
holds the conversation: `<div oncopy={copyAsWritten}>`, from `@diegopluna/narvi`.
|
|
99
|
+
- `links` is the policy for links and images (below).
|
|
100
|
+
|
|
101
|
+
Replace Narvi's rendering with a plugin, never by patching the rendered DOM with a
|
|
102
|
+
`MutationObserver`. Svelte owns those nodes, and edits made from outside are lost or break the
|
|
103
|
+
next update.
|