@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,714 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Markdown, as it is written. Give it the text so far on every update while a reply streams:
|
|
3
|
+
only what is new is read, finished blocks are never drawn again, and the unfinished end is
|
|
4
|
+
tidied so half-written syntax doesn't flash. When `streaming` turns off, the document
|
|
5
|
+
finishes in place, without being rebuilt.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts" module>
|
|
8
|
+
import { untrack } from 'svelte';
|
|
9
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
10
|
+
import type {
|
|
11
|
+
Block,
|
|
12
|
+
Document,
|
|
13
|
+
FootnoteDefinition,
|
|
14
|
+
FootnoteReference,
|
|
15
|
+
Heading,
|
|
16
|
+
Inline,
|
|
17
|
+
ListItem
|
|
18
|
+
} from '../../markdown/ast.js';
|
|
19
|
+
import { placeCaret } from './caret.js';
|
|
20
|
+
import { copyAsWritten } from './copy.js';
|
|
21
|
+
import { inlineText } from '../../markdown/table.js';
|
|
22
|
+
import { defaultLabels, type MarkdownLabels } from './labels.js';
|
|
23
|
+
import { MarkdownStream } from '../../markdown/parse.js';
|
|
24
|
+
import type { MarkdownPlugin } from '../../markdown/plugin.js';
|
|
25
|
+
import * as stylex from '@stylexjs/stylex';
|
|
26
|
+
import { attrs, classes, type Sx } from '../../internal/sx.js';
|
|
27
|
+
import { media } from '../../media.stylex.js';
|
|
28
|
+
import { colors } from '../../tokens.stylex.js';
|
|
29
|
+
import { resolvePlugins, setMarkdownContext, type MarkdownContext } from './context.js';
|
|
30
|
+
import MarkdownBlocks from './MarkdownBlocks.svelte';
|
|
31
|
+
import MarkdownFootnotes from './MarkdownFootnotes.svelte';
|
|
32
|
+
import { SvelteSet } from 'svelte/reactivity';
|
|
33
|
+
import { codeTitle } from '../../markdown/code-info.js';
|
|
34
|
+
import { showCopied } from '../message/MessageCopy.svelte';
|
|
35
|
+
import { withoutControls } from './hidden.js';
|
|
36
|
+
import { codeFileName, saveFile } from './save.js';
|
|
37
|
+
import { keep, kept, own, OWN_ID, serialOf } from './kept.js';
|
|
38
|
+
import { measureScrollables } from './scrollable.js';
|
|
39
|
+
import { staticHtml, type StaticContext } from './static.js';
|
|
40
|
+
import { prose } from './styles.js';
|
|
41
|
+
|
|
42
|
+
export type MarkdownProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
|
|
43
|
+
/** The Markdown text: all of it so far, while it streams. */
|
|
44
|
+
value: string;
|
|
45
|
+
/** Whether the text is still being written. */
|
|
46
|
+
streaming?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Whether to tidy the unfinished end while streaming: close emphasis that has started,
|
|
49
|
+
* show a link waiting for its address as text, and hold back what is still ambiguous.
|
|
50
|
+
* On by default.
|
|
51
|
+
*/
|
|
52
|
+
tidy?: boolean;
|
|
53
|
+
/** GitHub Flavored Markdown: tables, task lists, strikethrough, autolinks, footnotes. On by default. */
|
|
54
|
+
gfm?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Whether a line ending inside a paragraph breaks the line, as GitHub does in comments
|
|
57
|
+
* and as chat messages are usually meant. Off by default, as Markdown has it.
|
|
58
|
+
*/
|
|
59
|
+
breaks?: boolean;
|
|
60
|
+
/** Whether `~text~` strikes through, as `~~text~~` does. Off by default: see the docs. */
|
|
61
|
+
singleTilde?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Whether YAML front matter at the top of a file is read and left out, where it would
|
|
64
|
+
* otherwise be drawn as a rule and a heading. Off by default: turn it on for files, not
|
|
65
|
+
* for replies.
|
|
66
|
+
*/
|
|
67
|
+
frontmatter?: boolean;
|
|
68
|
+
/** Plugins, in order: later ones win where they overlap. */
|
|
69
|
+
plugins?: ReadonlyArray<MarkdownPlugin>;
|
|
70
|
+
/**
|
|
71
|
+
* Makes task items tickable. It is called with the item, which says where its box is
|
|
72
|
+
* in the text (`checkedAt`), and the new state; change the text and pass it back as
|
|
73
|
+
* `value`. Return `false`, or reject, and the box goes back.
|
|
74
|
+
*/
|
|
75
|
+
ontaskchange?: (item: ListItem, checked: boolean) => unknown;
|
|
76
|
+
/**
|
|
77
|
+
* Whether code blocks number their lines. Off by default. A block with no language,
|
|
78
|
+
* whose lines wrap, has none.
|
|
79
|
+
*/
|
|
80
|
+
lineNumbers?: boolean;
|
|
81
|
+
/** Whether a code block has a button that saves it as a file. Off by default. */
|
|
82
|
+
download?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Shows a caret at the end of the text while it streams: `true` or `'block'` for a
|
|
85
|
+
* block, `'circle'` for a dot. Off by default.
|
|
86
|
+
*/
|
|
87
|
+
caret?: boolean | 'block' | 'circle';
|
|
88
|
+
/**
|
|
89
|
+
* Fades text in a few words at a time as it arrives, where a new block otherwise fades
|
|
90
|
+
* in whole. Off by default. Code is not faded.
|
|
91
|
+
*/
|
|
92
|
+
animated?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Whether finished blocks are written as markup, not built from parts: about twice as
|
|
95
|
+
* fast to show and a fifth of the memory. Text, lists, tasks, quotes, code blocks, tables
|
|
96
|
+
* and file chips are written so; images, formulas, diagrams, footnotes, alerts, raw HTML
|
|
97
|
+
* and a plugin's node that has no writer of its own are drawn by their parts as ever,
|
|
98
|
+
* with the block they are in. Left out, it is on for text that was whole when the
|
|
99
|
+
* component was made and has not changed since, and off for text that streams or is
|
|
100
|
+
* edited here, whose parts change in place. `true` writes markup whenever nothing
|
|
101
|
+
* streams, `false` never does. Don't turn it on for a reply the moment it finishes
|
|
102
|
+
* streaming: it would be drawn again.
|
|
103
|
+
*/
|
|
104
|
+
static?: boolean;
|
|
105
|
+
/** The words the controls show, to translate or reword. */
|
|
106
|
+
labels?: Partial<MarkdownLabels>;
|
|
107
|
+
/**
|
|
108
|
+
* How many levels to move headings down, so they sit under the page's own: with 2, `#`
|
|
109
|
+
* is an `h3`. They keep the size they are written with.
|
|
110
|
+
*/
|
|
111
|
+
headingOffset?: number;
|
|
112
|
+
sx?: Sx;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const blink = stylex.keyframes({
|
|
116
|
+
'0%': { opacity: 1 },
|
|
117
|
+
'50%': { opacity: 1 },
|
|
118
|
+
'50.01%': { opacity: 0 },
|
|
119
|
+
'100%': { opacity: 0 }
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// The caret takes no room in its line: it is an empty box whose shape is its shadow, so a
|
|
123
|
+
// line is as wide with it as without, and a caret at a line's end starts no new line and
|
|
124
|
+
// no scrollbar. It is `accent`, the colour of something live.
|
|
125
|
+
const caretStyles = stylex.create({
|
|
126
|
+
base: {
|
|
127
|
+
display: 'inline-block',
|
|
128
|
+
width: 0,
|
|
129
|
+
height: '0.5em',
|
|
130
|
+
color: colors.accent,
|
|
131
|
+
pointerEvents: 'none',
|
|
132
|
+
userSelect: 'none',
|
|
133
|
+
WebkitUserSelect: 'none',
|
|
134
|
+
animationName: { default: null, [media.motion]: blink },
|
|
135
|
+
animationDuration: '1.1s',
|
|
136
|
+
animationIterationCount: 'infinite'
|
|
137
|
+
},
|
|
138
|
+
block: { boxShadow: '0.36em 0 0 0.28em currentColor', borderRadius: '1px' },
|
|
139
|
+
circle: {
|
|
140
|
+
width: '1px',
|
|
141
|
+
height: '1px',
|
|
142
|
+
marginInlineEnd: '-1px',
|
|
143
|
+
borderRadius: '50%',
|
|
144
|
+
boxShadow: '0.42em -0.3em 0 0.3em currentColor'
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const caretClasses = {
|
|
148
|
+
block: classes(caretStyles.base, caretStyles.block),
|
|
149
|
+
circle: classes(caretStyles.base, caretStyles.circle)
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** The text of a heading, for its anchor. A finished heading is read once. */
|
|
153
|
+
const headingText = new WeakMap<Heading, string>();
|
|
154
|
+
|
|
155
|
+
/** GitHub's way of naming a heading: lower case, punctuation dropped, spaces as hyphens. */
|
|
156
|
+
function slug(text: string): string {
|
|
157
|
+
return text
|
|
158
|
+
.trim()
|
|
159
|
+
.toLowerCase()
|
|
160
|
+
.replace(/[^\p{L}\p{N}\p{M}\s_-]/gu, '')
|
|
161
|
+
.replace(/\s/g, '-');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The footnote references in a block, in order. A finished block is read once. */
|
|
165
|
+
const referencesCache = new WeakMap<Block, FootnoteReference[]>();
|
|
166
|
+
|
|
167
|
+
const NO_FOOTNOTES: FootnoteDefinition[] = [];
|
|
168
|
+
const NO_ORDER = new Map<string, number>();
|
|
169
|
+
|
|
170
|
+
/** The headings in a block, in order. A block never changes once made, so it is read once. */
|
|
171
|
+
const headingsCache = new WeakMap<Block, Heading[]>();
|
|
172
|
+
|
|
173
|
+
function headingsIn(block: Block): Heading[] {
|
|
174
|
+
let found = headingsCache.get(block);
|
|
175
|
+
if (found) return found;
|
|
176
|
+
found = [];
|
|
177
|
+
const visit = (b: Block) => {
|
|
178
|
+
if (b.type === 'heading') found!.push(b);
|
|
179
|
+
else if (b.type === 'blockquote' || b.type === 'listItem' || b.type === 'list')
|
|
180
|
+
for (const child of b.children) visit(child);
|
|
181
|
+
};
|
|
182
|
+
visit(block);
|
|
183
|
+
headingsCache.set(block, found);
|
|
184
|
+
return found;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function references(block: Block): FootnoteReference[] {
|
|
188
|
+
const cached = referencesCache.get(block);
|
|
189
|
+
if (cached) return cached;
|
|
190
|
+
const out: FootnoteReference[] = [];
|
|
191
|
+
const visitInlines = (nodes: Inline[]) => {
|
|
192
|
+
for (const n of nodes) {
|
|
193
|
+
if (n.type === 'footnoteReference') out.push(n);
|
|
194
|
+
else if ('children' in n) visitInlines(n.children);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const visit = (b: Block) => {
|
|
198
|
+
if (b.type === 'paragraph' || b.type === 'heading') visitInlines(b.children);
|
|
199
|
+
else if (b.type === 'table')
|
|
200
|
+
for (const row of b.children) for (const cell of row.children) visitInlines(cell.children);
|
|
201
|
+
else if ('children' in b) for (const child of b.children) visit(child as Block);
|
|
202
|
+
};
|
|
203
|
+
visit(block);
|
|
204
|
+
referencesCache.set(block, out);
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
</script>
|
|
208
|
+
|
|
209
|
+
<script lang="ts">
|
|
210
|
+
let {
|
|
211
|
+
value,
|
|
212
|
+
streaming = false,
|
|
213
|
+
tidy = true,
|
|
214
|
+
gfm = true,
|
|
215
|
+
breaks = false,
|
|
216
|
+
singleTilde = false,
|
|
217
|
+
frontmatter = false,
|
|
218
|
+
lineNumbers = false,
|
|
219
|
+
download = false,
|
|
220
|
+
caret = false,
|
|
221
|
+
animated = false,
|
|
222
|
+
static: asked,
|
|
223
|
+
onclick,
|
|
224
|
+
onauxclick,
|
|
225
|
+
onchange,
|
|
226
|
+
onkeydown,
|
|
227
|
+
plugins = [],
|
|
228
|
+
ontaskchange,
|
|
229
|
+
labels,
|
|
230
|
+
headingOffset = 0,
|
|
231
|
+
sx,
|
|
232
|
+
class: className,
|
|
233
|
+
oncopy,
|
|
234
|
+
...rest
|
|
235
|
+
}: MarkdownProps = $props();
|
|
236
|
+
|
|
237
|
+
function copy(event: ClipboardEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
238
|
+
oncopy?.(event);
|
|
239
|
+
copyAsWritten(event);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const id = $props.id();
|
|
243
|
+
|
|
244
|
+
// Text that was whole when the component was made, and has not changed since, is written
|
|
245
|
+
// as markup without being asked: nothing in it will be drawn again, so there is nothing a
|
|
246
|
+
// part would be kept for. Text that streams or is edited here keeps its parts, which
|
|
247
|
+
// change in place. `static` says otherwise, either way.
|
|
248
|
+
const first = untrack(() => value);
|
|
249
|
+
let settled = true;
|
|
250
|
+
const finished = $derived.by(() => {
|
|
251
|
+
if (streaming || value !== first) settled = false;
|
|
252
|
+
return asked ?? settled;
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// The stream lives as long as the component; a change of options starts it again
|
|
256
|
+
const math = $derived(plugins.some((plugin) => plugin.syntax?.math));
|
|
257
|
+
const stream = $derived(new MarkdownStream({ gfm, math, singleTilde, frontmatter }));
|
|
258
|
+
// The same plugins in a new array are still the same plugins: nothing is resolved again,
|
|
259
|
+
// and no block is transformed or highlighted again
|
|
260
|
+
let last: { plugins: MarkdownPlugin[]; resolved: ReturnType<typeof resolvePlugins> } | null =
|
|
261
|
+
null;
|
|
262
|
+
const resolved = $derived.by(() => {
|
|
263
|
+
if (
|
|
264
|
+
last &&
|
|
265
|
+
last.plugins.length === plugins.length &&
|
|
266
|
+
last.plugins.every((plugin, i) => plugin === plugins[i])
|
|
267
|
+
)
|
|
268
|
+
return last.resolved;
|
|
269
|
+
last = { plugins: [...plugins], resolved: resolvePlugins(plugins) };
|
|
270
|
+
return last.resolved;
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// The document is never closed for good, even once the text is whole: more text can still
|
|
274
|
+
// arrive (a reply that carries on, a file that is being edited) and only that is read
|
|
275
|
+
const document: Document = $derived.by(() => {
|
|
276
|
+
stream.set(typeof value === 'string' ? value : '');
|
|
277
|
+
// Once the text is all there it is read as final: nothing in it is still on its way
|
|
278
|
+
return stream.snapshot({ tidy: streaming && tidy, final: !streaming });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const blocks = $derived(document.children.map((block) => resolved.transform(block, document)));
|
|
282
|
+
const footnotes = $derived(
|
|
283
|
+
document.footnotes.map((note) => resolved.transform(note, document) as FootnoteDefinition)
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
// Footnotes are numbered by first reference, across the whole document, and then through
|
|
287
|
+
// the footnotes themselves, which may refer to others
|
|
288
|
+
const footnoteOrder = $derived.by(() => {
|
|
289
|
+
// Built afresh with each document, never changed after: plain collections are right here
|
|
290
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity
|
|
291
|
+
const order = new Map<string, number>();
|
|
292
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity
|
|
293
|
+
const first = new Set<FootnoteReference>();
|
|
294
|
+
// While streaming, a reference is numbered before its definition arrives; once the
|
|
295
|
+
// document is finished, a reference to a footnote that was never defined is text
|
|
296
|
+
const defined = new Map(footnotes.map((note) => [note.label.toLowerCase(), note]));
|
|
297
|
+
const queue: FootnoteDefinition[] = [];
|
|
298
|
+
const take = (block: Block) => {
|
|
299
|
+
for (const reference of references(block)) {
|
|
300
|
+
const label = reference.label.toLowerCase();
|
|
301
|
+
if (order.has(label) || !(streaming || defined.has(label))) continue;
|
|
302
|
+
order.set(label, order.size + 1);
|
|
303
|
+
first.add(reference);
|
|
304
|
+
const note = defined.get(label);
|
|
305
|
+
if (note) queue.push(note);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
for (const block of blocks) take(block);
|
|
309
|
+
for (let i = 0; i < queue.length; i++) take(queue[i]!);
|
|
310
|
+
return { order, first };
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// Headings are named as GitHub names them, so `[see](#install)` goes to "## Install". Two
|
|
314
|
+
// headings with the same words get `-1`, `-2` after the first.
|
|
315
|
+
let namedBefore: Map<Heading, string> | null = null;
|
|
316
|
+
const anchors = $derived.by(() => {
|
|
317
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- built afresh, never changed
|
|
318
|
+
const names = new Map<Heading, string>();
|
|
319
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- built afresh, never changed
|
|
320
|
+
const seen = new Map<string, number>();
|
|
321
|
+
for (const block of blocks) {
|
|
322
|
+
// Most blocks of a reply can hold no heading, and are not looked into
|
|
323
|
+
if (
|
|
324
|
+
block.type !== 'heading' &&
|
|
325
|
+
block.type !== 'blockquote' &&
|
|
326
|
+
block.type !== 'list' &&
|
|
327
|
+
block.type !== 'listItem'
|
|
328
|
+
)
|
|
329
|
+
continue;
|
|
330
|
+
for (const heading of headingsIn(block)) {
|
|
331
|
+
let text = headingText.get(heading);
|
|
332
|
+
if (text === undefined)
|
|
333
|
+
headingText.set(heading, (text = slug(inlineText(heading.children))));
|
|
334
|
+
const count = seen.get(text) ?? 0;
|
|
335
|
+
seen.set(text, count + 1);
|
|
336
|
+
if (text) names.set(heading, count === 0 ? text : `${text}-${count}`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
// The same names for the same headings are the same answer: while a reply streams,
|
|
340
|
+
// most updates name nothing new, and nothing that reads the names is asked again
|
|
341
|
+
const before = namedBefore;
|
|
342
|
+
if (before && before.size === names.size) {
|
|
343
|
+
let same = true;
|
|
344
|
+
for (const [heading, name] of names) {
|
|
345
|
+
if (before.get(heading) !== name) {
|
|
346
|
+
same = false;
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (same) return before;
|
|
351
|
+
}
|
|
352
|
+
namedBefore = names;
|
|
353
|
+
return names;
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
const anchorNames = $derived(new Set(anchors.values()));
|
|
357
|
+
|
|
358
|
+
/** A `#name` as it was written: percent-decoded, in lower case. */
|
|
359
|
+
function anchorName(name: string): string {
|
|
360
|
+
try {
|
|
361
|
+
return decodeURIComponent(name).toLowerCase();
|
|
362
|
+
} catch {
|
|
363
|
+
// Not percent-encoded: the name as written
|
|
364
|
+
return name.toLowerCase();
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const words = $derived(labels ? { ...defaultLabels, ...labels } : defaultLabels);
|
|
369
|
+
|
|
370
|
+
/** Blocks that `static` wrote and that have since been asked for as parts. */
|
|
371
|
+
const woken = new SvelteSet<number>();
|
|
372
|
+
|
|
373
|
+
/** Whether a formula has been drawn in this document: only then are its blocks clipped. */
|
|
374
|
+
let formulas = $state(false);
|
|
375
|
+
|
|
376
|
+
// What `static` writes depends on the plugins, the headings' names and a few props: when
|
|
377
|
+
// one of them changes, what was written is forgotten
|
|
378
|
+
const staticContext = $derived({
|
|
379
|
+
// Not this document's id: what is written may be put into another (`kept.ts`)
|
|
380
|
+
id: OWN_ID,
|
|
381
|
+
breaks,
|
|
382
|
+
overridden: (type: string) => resolved.components.has(type),
|
|
383
|
+
href: resolved.links.href,
|
|
384
|
+
external: resolved.links.external,
|
|
385
|
+
anchor: (heading: Heading) => anchors.get(heading),
|
|
386
|
+
anchorHref: (name: string) => {
|
|
387
|
+
const wanted = anchorName(name);
|
|
388
|
+
return anchorNames.has(wanted) ? wanted : null;
|
|
389
|
+
},
|
|
390
|
+
headingLevel: (depth: number) => Math.min(6, Math.max(1, depth + headingOffset)),
|
|
391
|
+
part: (name: string) => resolved.statics.get(name),
|
|
392
|
+
language: (lang: string | null) => lang !== null && resolved.languages.has(lang.toLowerCase()),
|
|
393
|
+
highlight: resolved.highlight,
|
|
394
|
+
labels: words,
|
|
395
|
+
code: { lineNumbers, download },
|
|
396
|
+
tickable: ontaskchange !== undefined,
|
|
397
|
+
woken: (id: number) => woken.has(id)
|
|
398
|
+
} satisfies StaticContext);
|
|
399
|
+
const staticCache = $derived.by(() => {
|
|
400
|
+
void staticContext;
|
|
401
|
+
void woken.size;
|
|
402
|
+
return new WeakMap<Block, { tight: boolean; depth: number; html: string | null }>();
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
// All that what `static` writes depends on, but the text: two documents with the same
|
|
406
|
+
// text and the same of these are the same markup
|
|
407
|
+
const settings = $derived(
|
|
408
|
+
[
|
|
409
|
+
gfm,
|
|
410
|
+
math,
|
|
411
|
+
singleTilde,
|
|
412
|
+
frontmatter,
|
|
413
|
+
breaks,
|
|
414
|
+
headingOffset,
|
|
415
|
+
lineNumbers,
|
|
416
|
+
download,
|
|
417
|
+
ontaskchange !== undefined
|
|
418
|
+
]
|
|
419
|
+
.map(Number)
|
|
420
|
+
.join(',') + `;${serialOf(labels)};${plugins.map(serialOf).join(',')}`
|
|
421
|
+
);
|
|
422
|
+
const text = $derived(typeof value === 'string' ? value : '');
|
|
423
|
+
// The document as markup written before, when this text has been drawn already. A table
|
|
424
|
+
// asked for as a part is drawn from the text, so then nothing kept is used
|
|
425
|
+
const before = $derived(finished && !streaming && woken.size === 0 ? kept(text, settings) : null);
|
|
426
|
+
|
|
427
|
+
/** The block with an id, wherever it is in the document: a control written as markup names its block by it. */
|
|
428
|
+
function blockById(id: number): Block | null {
|
|
429
|
+
const find = (list: Block[]): Block | null => {
|
|
430
|
+
for (const block of list) {
|
|
431
|
+
if (block.id === id) return block;
|
|
432
|
+
if (
|
|
433
|
+
'children' in block &&
|
|
434
|
+
block.type !== 'paragraph' &&
|
|
435
|
+
block.type !== 'heading' &&
|
|
436
|
+
block.type !== 'table'
|
|
437
|
+
) {
|
|
438
|
+
const found = find(block.children as Block[]);
|
|
439
|
+
if (found) return found;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return null;
|
|
443
|
+
};
|
|
444
|
+
return find(blocks) ?? find(footnotes);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* A click on something written as markup that has a use: a plugin's part (a file chip)
|
|
449
|
+
* or a table's button under `static`, and the buttons of a code block always, which are
|
|
450
|
+
* markup however the block was drawn. None has a handler of its own.
|
|
451
|
+
*/
|
|
452
|
+
function partClicked(event: MouseEvent) {
|
|
453
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
454
|
+
if (!target || !root) return;
|
|
455
|
+
const part = target.closest<HTMLElement>('[data-markdown-part]');
|
|
456
|
+
if (part && root.contains(part)) {
|
|
457
|
+
try {
|
|
458
|
+
resolved.statics.get(part.getAttribute('data-markdown-part')!)?.click?.(part, event);
|
|
459
|
+
} catch {
|
|
460
|
+
// A plugin that throws takes down its own click, not the page's
|
|
461
|
+
}
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (event.type !== 'click') return;
|
|
465
|
+
const button = target.closest<HTMLElement>('[data-markdown-action]');
|
|
466
|
+
const frame = button?.closest<HTMLElement>('[data-markdown-block]');
|
|
467
|
+
if (!button || !frame || !root.contains(button)) return;
|
|
468
|
+
const block = blockById(Number(frame.getAttribute('data-markdown-block')));
|
|
469
|
+
const action = button.getAttribute('data-markdown-action');
|
|
470
|
+
if (block?.type === 'table' && action === 'table') {
|
|
471
|
+
// Drawn again by its component, with its menu open
|
|
472
|
+
woken.add(block.id);
|
|
473
|
+
} else if (action === 'copy' || action === 'download') {
|
|
474
|
+
// The block's own text. A block a plugin made, which the document does not know by
|
|
475
|
+
// that id, gives what it shows
|
|
476
|
+
const shown = () => frame.querySelector('pre code')?.textContent ?? '';
|
|
477
|
+
const code = block?.type === 'code' ? block.value.replace(/\n$/, '') : shown();
|
|
478
|
+
if (action === 'download') {
|
|
479
|
+
if (block?.type === 'code') saveFile(codeFileName(codeTitle(block), block.lang), code);
|
|
480
|
+
} else
|
|
481
|
+
void navigator.clipboard.writeText(withoutControls(code)).then(
|
|
482
|
+
() => showCopied(button, words.copied),
|
|
483
|
+
() => {}
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* The arrow key on the button of a table that `static` wrote opens its menu, as it does on
|
|
490
|
+
* the button a part draws.
|
|
491
|
+
*/
|
|
492
|
+
function tableKey(event: KeyboardEvent) {
|
|
493
|
+
if (event.key !== 'ArrowDown' || !(event.target instanceof Element)) return;
|
|
494
|
+
const button = event.target.closest('[data-markdown-action="table"]');
|
|
495
|
+
const frame = button?.closest('[data-markdown-block]');
|
|
496
|
+
if (!frame || !root?.contains(frame)) return;
|
|
497
|
+
const block = blockById(Number(frame.getAttribute('data-markdown-block')));
|
|
498
|
+
if (block?.type !== 'table') return;
|
|
499
|
+
event.preventDefault();
|
|
500
|
+
woken.add(block.id);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** A task's box, written by `static`, ticked: handed to the app as a box of its own would be. */
|
|
504
|
+
function taskTicked(event: Event) {
|
|
505
|
+
const input = event.target instanceof HTMLInputElement ? event.target : null;
|
|
506
|
+
const id = input?.getAttribute('data-markdown-task');
|
|
507
|
+
if (!input || !id || !ontaskchange) return;
|
|
508
|
+
const item = blockById(Number(id));
|
|
509
|
+
if (item?.type !== 'listItem') return;
|
|
510
|
+
const revert = () => (input.checked = item.checked === true);
|
|
511
|
+
try {
|
|
512
|
+
Promise.resolve(ontaskchange(item, input.checked)).then((kept) => {
|
|
513
|
+
if (kept === false) revert();
|
|
514
|
+
}, revert);
|
|
515
|
+
} catch {
|
|
516
|
+
revert();
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* A click on a link that `static` wrote. Such a link has no handler of its own, so the
|
|
522
|
+
* root does for it what a link's own would: a plain click on a link to a heading scrolls
|
|
523
|
+
* there, and any other is offered to the app first.
|
|
524
|
+
*/
|
|
525
|
+
function linkClicked(event: MouseEvent) {
|
|
526
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
527
|
+
const link = target?.closest('a[data-markdown-href]');
|
|
528
|
+
if (!link || !root?.contains(link)) return;
|
|
529
|
+
const href = link.getAttribute('data-markdown-href')!;
|
|
530
|
+
if (event.type === 'click') {
|
|
531
|
+
const plain = !(event.metaKey || event.ctrlKey || event.shiftKey || event.altKey);
|
|
532
|
+
if (plain && href.startsWith('#') && context.goto(href.slice(1))) {
|
|
533
|
+
event.preventDefault();
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
resolved.links.open?.(href, event);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
let root = $state<HTMLDivElement | null>(null);
|
|
541
|
+
|
|
542
|
+
// What `static` wrote scrolls sideways as a part's own would: looked after once it is drawn
|
|
543
|
+
$effect(() => {
|
|
544
|
+
// Again when what is written changes. Not `blocks`: asking for them reads the text,
|
|
545
|
+
// which a document drawn from kept markup has not done
|
|
546
|
+
void text;
|
|
547
|
+
void settings;
|
|
548
|
+
void woken.size;
|
|
549
|
+
if (!root || !finished || streaming) return;
|
|
550
|
+
return measureScrollables(root);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
// The caret is one element, put at the end of the text after each update (see `caret.ts`)
|
|
554
|
+
let caretElement: HTMLSpanElement | null = null;
|
|
555
|
+
$effect(() => {
|
|
556
|
+
if (!root) return;
|
|
557
|
+
if (!streaming || !caret) {
|
|
558
|
+
caretElement?.remove();
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
// Read, so that this runs again after every change to what is drawn
|
|
562
|
+
void blocks;
|
|
563
|
+
if (!caretElement) {
|
|
564
|
+
caretElement = window.document.createElement('span');
|
|
565
|
+
caretElement.setAttribute('aria-hidden', 'true');
|
|
566
|
+
caretElement.setAttribute('data-markdown-caret', '');
|
|
567
|
+
// Left out of a copy, as the controls are
|
|
568
|
+
caretElement.setAttribute('data-markdown-chrome', '');
|
|
569
|
+
}
|
|
570
|
+
caretElement.className = caret === 'circle' ? caretClasses.circle : caretClasses.block;
|
|
571
|
+
placeCaret(root, caretElement);
|
|
572
|
+
});
|
|
573
|
+
$effect(() => () => caretElement?.remove());
|
|
574
|
+
|
|
575
|
+
let mounted = false;
|
|
576
|
+
$effect(() => {
|
|
577
|
+
mounted = true;
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
const context: MarkdownContext = {
|
|
581
|
+
component: (type) => resolved.components.get(type),
|
|
582
|
+
language: (lang) => (lang ? resolved.languages.get(lang.toLowerCase()) : undefined),
|
|
583
|
+
get highlight() {
|
|
584
|
+
return resolved.highlight;
|
|
585
|
+
},
|
|
586
|
+
get highlightStream() {
|
|
587
|
+
return resolved.highlightStream;
|
|
588
|
+
},
|
|
589
|
+
get links() {
|
|
590
|
+
return resolved.links;
|
|
591
|
+
},
|
|
592
|
+
get urls() {
|
|
593
|
+
return resolved.urls;
|
|
594
|
+
},
|
|
595
|
+
// A new block fades in whole, unless its text fades in a few words at a time
|
|
596
|
+
animate: () => mounted && streaming && !animated,
|
|
597
|
+
fade: () => mounted && streaming && animated,
|
|
598
|
+
footnote: (node) => ({
|
|
599
|
+
number: footnoteOrder.order.get(node.label.toLowerCase()) ?? 0,
|
|
600
|
+
first: footnoteOrder.first.has(node)
|
|
601
|
+
}),
|
|
602
|
+
reveal(id) {
|
|
603
|
+
const target = root?.querySelector<HTMLElement>(`[id="${CSS.escape(id)}"]`);
|
|
604
|
+
if (!target) return false;
|
|
605
|
+
target.scrollIntoView({ block: 'nearest' });
|
|
606
|
+
target.focus({ preventScroll: true });
|
|
607
|
+
return true;
|
|
608
|
+
},
|
|
609
|
+
headingLevel: (depth) => Math.min(6, Math.max(1, depth + headingOffset)),
|
|
610
|
+
id,
|
|
611
|
+
streaming: () => streaming,
|
|
612
|
+
breaks: () => breaks,
|
|
613
|
+
get labels() {
|
|
614
|
+
return words;
|
|
615
|
+
},
|
|
616
|
+
woken: (id) => woken.has(id),
|
|
617
|
+
task: () => ontaskchange,
|
|
618
|
+
math: {
|
|
619
|
+
used: 0,
|
|
620
|
+
drawn: () => {
|
|
621
|
+
if (!untrack(() => formulas)) formulas = true;
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
code: () => ({ lineNumbers, download }),
|
|
625
|
+
static: () => finished && !streaming,
|
|
626
|
+
kept: () => before,
|
|
627
|
+
keep(runs) {
|
|
628
|
+
// With no part in it, and no footnotes under it: markup from end to end
|
|
629
|
+
if (woken.size === 0 && footnotes.length === 0) keep(text, settings, runs);
|
|
630
|
+
},
|
|
631
|
+
own: (html) => own(html, id),
|
|
632
|
+
staticHtml(block, tight, depth) {
|
|
633
|
+
if (!finished || streaming) return null;
|
|
634
|
+
const kept = staticCache.get(block);
|
|
635
|
+
if (kept && kept.tight === tight && kept.depth === depth) return kept.html;
|
|
636
|
+
const html = staticHtml(block, tight, depth, staticContext);
|
|
637
|
+
staticCache.set(block, { tight, depth, html });
|
|
638
|
+
return html;
|
|
639
|
+
},
|
|
640
|
+
anchor: (heading) => anchors.get(heading),
|
|
641
|
+
anchorHref(name) {
|
|
642
|
+
const wanted = anchorName(name);
|
|
643
|
+
return anchorNames.has(wanted) ? `#${id}-h-${wanted}` : null;
|
|
644
|
+
},
|
|
645
|
+
goto(name) {
|
|
646
|
+
const target = root?.querySelector<HTMLElement>(
|
|
647
|
+
`[data-anchor="${CSS.escape(anchorName(name))}"]`
|
|
648
|
+
);
|
|
649
|
+
if (!target) return false;
|
|
650
|
+
target.scrollIntoView({ block: 'start' });
|
|
651
|
+
// Focus follows, so the keyboard and a screen reader carry on from the heading
|
|
652
|
+
target.focus({ preventScroll: true });
|
|
653
|
+
return true;
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
setMarkdownContext(context);
|
|
657
|
+
</script>
|
|
658
|
+
|
|
659
|
+
<div
|
|
660
|
+
data-markdown
|
|
661
|
+
data-streaming={streaming || undefined}
|
|
662
|
+
data-markdown-formulas={formulas || undefined}
|
|
663
|
+
bind:this={root}
|
|
664
|
+
oncopy={copy}
|
|
665
|
+
{...rest}
|
|
666
|
+
onclick={(event) => {
|
|
667
|
+
onclick?.(event);
|
|
668
|
+
linkClicked(event);
|
|
669
|
+
partClicked(event);
|
|
670
|
+
}}
|
|
671
|
+
onauxclick={(event) => {
|
|
672
|
+
onauxclick?.(event);
|
|
673
|
+
linkClicked(event);
|
|
674
|
+
partClicked(event);
|
|
675
|
+
}}
|
|
676
|
+
onchange={(event) => {
|
|
677
|
+
onchange?.(event);
|
|
678
|
+
taskTicked(event);
|
|
679
|
+
}}
|
|
680
|
+
onkeydown={(event) => {
|
|
681
|
+
onkeydown?.(event);
|
|
682
|
+
tableKey(event);
|
|
683
|
+
}}
|
|
684
|
+
{...attrs([prose.root, streaming && prose.streaming, sx], className)}
|
|
685
|
+
>
|
|
686
|
+
<MarkdownBlocks {blocks} top />
|
|
687
|
+
<!-- Markup kept from before has no footnotes under it, and to ask would read the text.
|
|
688
|
+
The part is there either way, so a page drawn on the server finds what it expects -->
|
|
689
|
+
<MarkdownFootnotes
|
|
690
|
+
footnotes={before ? NO_FOOTNOTES : footnotes}
|
|
691
|
+
order={before ? NO_ORDER : footnoteOrder.order}
|
|
692
|
+
/>
|
|
693
|
+
</div>
|
|
694
|
+
|
|
695
|
+
<!--
|
|
696
|
+
An inline formula is clipped to its own place in the line (`prose.mathInline`), but Safari
|
|
697
|
+
does not clip an inline box. So the block a formula is written in keeps what it draws
|
|
698
|
+
within its own width as well: TeX can move ink anywhere, and a reply must not draw over
|
|
699
|
+
the app beside it. A list is clipped rather than its items, whose markers sit outside
|
|
700
|
+
them. StyleX has no way to say "a block that holds a formula", hence the style block.
|
|
701
|
+
The rule is for documents that have drawn a formula, and only those: a browser that has
|
|
702
|
+
a `:has()` rule to keep true looks at it again whenever the text under it changes, which
|
|
703
|
+
while a reply streams is every update.
|
|
704
|
+
-->
|
|
705
|
+
<style>
|
|
706
|
+
:global(
|
|
707
|
+
[data-markdown-formulas]
|
|
708
|
+
:is(p, ul, ol, td, th, h1, h2, h3, h4, h5, h6, summary):has([data-markdown-math='inline'])
|
|
709
|
+
) {
|
|
710
|
+
overflow-x: clip;
|
|
711
|
+
/* Room for a focus ring at the block's edge, where the browser has this */
|
|
712
|
+
overflow-clip-margin: 0.5em;
|
|
713
|
+
}
|
|
714
|
+
</style>
|