@zyro-sec/session-ui 0.0.0-stage → 1.0.3
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/AGENTS.md +25 -0
- package/bunfig.toml +2 -0
- package/component-tests/basic-tool.fixture.tsx +46 -0
- package/component-tests/basic-tool.spec.ts +63 -0
- package/component-tests/file-media.fixture.tsx +39 -0
- package/component-tests/file-media.spec.ts +13 -0
- package/component-tests/file-tool.spec.ts +62 -0
- package/component-tests/file-viewer.fixture.tsx +109 -0
- package/component-tests/file-viewer.spec.ts +134 -0
- package/component-tests/file.spec.ts +33 -0
- package/component-tests/line-comment-v2.spec.ts +15 -0
- package/component-tests/markdown-code-wrap.spec.ts +94 -0
- package/component-tests/markdown-readiness.spec.ts +95 -0
- package/component-tests/markdown.fixture.tsx +104 -0
- package/component-tests/markdown.spec.ts +1564 -0
- package/component-tests/mermaid-preview.spec.ts +43 -0
- package/component-tests/patch-group.spec.ts +79 -0
- package/component-tests/read-group.spec.ts +72 -0
- package/component-tests/read-image.fixture.tsx +66 -0
- package/component-tests/read-image.spec.ts +154 -0
- package/component-tests/session-compaction.spec.ts +254 -0
- package/component-tests/session-lifecycle.spec.ts +190 -0
- package/component-tests/session-message-projection.spec.ts +639 -0
- package/component-tests/session-notice-projection.spec.ts +20 -0
- package/component-tests/session-review-comments.spec.ts +179 -0
- package/component-tests/session-shell.spec.ts +93 -0
- package/component-tests/session-timeline-mobile.spec.ts +201 -0
- package/component-tests/session-timeline-notices.spec.ts +38 -0
- package/component-tests/session-timeline-reasoning.spec.ts +107 -0
- package/component-tests/session-timeline.spec.ts +157 -0
- package/component-tests/session-tool-projection.spec.ts +323 -0
- package/component-tests/text-lifecycle.fixture.tsx +26 -0
- package/component-tests/text-lifecycle.spec.ts +42 -0
- package/component-tests/timeline-subagent-rows.spec.ts +33 -0
- package/component-tests/tool-disclosure.spec.ts +48 -0
- package/component-tests/tool-group-sticky.spec.ts +37 -0
- package/component-tests/tool-group.spec.ts +290 -0
- package/component-tests/tool-status-title.fixture.tsx +22 -0
- package/component-tests/tool-status-title.spec.ts +30 -0
- package/component-tests/user-message-quotes.spec.ts +111 -0
- package/package.json +91 -4
- package/playwright.components.config.ts +4 -0
- package/playwright.local.config.ts +2 -0
- package/src/actions.ts +54 -0
- package/src/components/apply-patch-file.ts +122 -0
- package/src/components/basic-tool.css +487 -0
- package/src/components/basic-tool.tsx +258 -0
- package/src/components/copy-machine.ts +78 -0
- package/src/components/copy.ts +85 -0
- package/src/components/diagram-preview.tsx +28 -0
- package/src/components/disclosure-machine.ts +188 -0
- package/src/components/disclosure.ts +129 -0
- package/src/components/dock-prompt.stories.tsx +73 -0
- package/src/components/dock-prompt.tsx +29 -0
- package/src/components/file-comment-selection.tsx +146 -0
- package/src/components/file-media-machine.ts +165 -0
- package/src/components/file-media.stories.tsx +112 -0
- package/src/components/file-media.tsx +323 -0
- package/src/components/file-search.tsx +73 -0
- package/src/components/file.css +59 -0
- package/src/components/file.tsx +1300 -0
- package/src/components/image-preview.tsx +35 -0
- package/src/components/line-comment-annotations.tsx +580 -0
- package/src/components/line-comment-machine.ts +262 -0
- package/src/components/line-comment-styles.ts +293 -0
- package/src/components/line-comment.stories.tsx +152 -0
- package/src/components/line-comment.tsx +448 -0
- package/src/components/link-segments.ts +20 -0
- package/src/components/linked-text.tsx +21 -0
- package/src/components/markdown-cache-machine.ts +257 -0
- package/src/components/markdown-cache.tsx +556 -0
- package/src/components/markdown-clipboard.ts +101 -0
- package/src/components/markdown-code-machine.ts +200 -0
- package/src/components/markdown-code-state.ts +22 -0
- package/src/components/markdown-fragment-links.ts +74 -0
- package/src/components/markdown-github.svg +3 -0
- package/src/components/markdown-globe.svg +5 -0
- package/src/components/markdown-image-machine.ts +170 -0
- package/src/components/markdown-image.ts +156 -0
- package/src/components/markdown-inline-code-kind.ts +39 -0
- package/src/components/markdown-mermaid.ts +18 -0
- package/src/components/markdown-phase-machine.ts +102 -0
- package/src/components/markdown-projection.ts +13 -0
- package/src/components/markdown-session-links.ts +183 -0
- package/src/components/markdown-solid.test.tsx +92 -0
- package/src/components/markdown-solid.tsx +113 -0
- package/src/components/markdown-stream.ts +95 -0
- package/src/components/markdown-worker-machine.ts +516 -0
- package/src/components/markdown-worker-protocol.ts +73 -0
- package/src/components/markdown-worker.ts +250 -0
- package/src/components/markdown.css +672 -0
- package/src/components/markdown.stories.tsx +199 -0
- package/src/components/markdown.tsx +1612 -0
- package/src/components/markdown.worker.ts +188 -0
- package/src/components/message-file.ts +102 -0
- package/src/components/message-nav.css +134 -0
- package/src/components/message-nav.tsx +159 -0
- package/src/components/message-part.css +2109 -0
- package/src/components/message-part.tsx +3 -0
- package/src/components/question-prompt-experimental.css +576 -0
- package/src/components/question-prompt.css +527 -0
- package/src/components/quote-highlight.css +59 -0
- package/src/components/review-focus-driver.ts +142 -0
- package/src/components/review-focus-machine.ts +95 -0
- package/src/components/session-compaction.css +270 -0
- package/src/components/session-diff.ts +358 -0
- package/src/components/session-error.tsx +41 -0
- package/src/components/session-retry.tsx +92 -0
- package/src/components/session-review-machine.ts +241 -0
- package/src/components/session-review.css +264 -0
- package/src/components/session-review.stories.tsx +232 -0
- package/src/components/session-review.tsx +733 -0
- package/src/components/session-turn.css +282 -0
- package/src/components/shell-submessage.css +24 -0
- package/src/components/timeline-separator.tsx +69 -0
- package/src/components/tool-count-label.css +57 -0
- package/src/components/tool-count-label.tsx +76 -0
- package/src/components/tool-count-summary.css +102 -0
- package/src/components/tool-count-summary.stories.tsx +99 -0
- package/src/components/tool-count-summary.tsx +44 -0
- package/src/components/tool-error-card.css +178 -0
- package/src/components/tool-error-card.stories.tsx +113 -0
- package/src/components/tool-error-card.tsx +202 -0
- package/src/components/tool-status-title-machine.ts +128 -0
- package/src/components/tool-status-title.css +89 -0
- package/src/components/tool-status-title.tsx +163 -0
- package/src/context/data.tsx +107 -0
- package/src/context/index.ts +1 -0
- package/src/context/markdown.tsx +129 -0
- package/src/context/sources.tsx +34 -0
- package/src/document.ts +8 -0
- package/src/file-presentation.ts +34 -0
- package/src/message/action-machine.ts +72 -0
- package/src/message/agent-label.ts +3 -0
- package/src/message/attachment-card.css +119 -0
- package/src/message/attachment-card.tsx +40 -0
- package/src/message/attachment-media-machine.ts +213 -0
- package/src/message/comment-card.tsx +155 -0
- package/src/message/compaction-entrance-machine.ts +93 -0
- package/src/message/current-message.tsx +157 -0
- package/src/message/current-tool-state.ts +161 -0
- package/src/message/message-content.tsx +1391 -0
- package/src/message/paced-text-machine.ts +73 -0
- package/src/message/prompt-message.stories.tsx +149 -0
- package/src/message/relative-time.ts +45 -0
- package/src/message/user-message-quote-parts.ts +16 -0
- package/src/message/user-message-quotes.stories.tsx +797 -0
- package/src/message/user-message-quotes.tsx +135 -0
- package/src/pierre/comment-hover-machine.ts +53 -0
- package/src/pierre/comment-hover.ts +131 -0
- package/src/pierre/comment-selection-machine.ts +257 -0
- package/src/pierre/commented-lines.ts +106 -0
- package/src/pierre/diff-selection.ts +91 -0
- package/src/pierre/file-find-machine.ts +323 -0
- package/src/pierre/file-find.ts +537 -0
- package/src/pierre/file-runtime.ts +42 -0
- package/src/pierre/file-selection.ts +140 -0
- package/src/pierre/index.ts +344 -0
- package/src/pierre/media.ts +140 -0
- package/src/pierre/render-machine.ts +381 -0
- package/src/pierre/selection-bridge.ts +82 -0
- package/src/pierre/selection-machine.ts +198 -0
- package/src/pierre/virtualizer.ts +114 -0
- package/src/pierre/worker.ts +46 -0
- package/src/sources/experiment.ts +59 -0
- package/src/sources/site-icon.tsx +53 -0
- package/src/sources/sources-chip.tsx +57 -0
- package/src/sources/sources-expand-machine.ts +101 -0
- package/src/sources/sources-panel.tsx +249 -0
- package/src/sources/sources-title.tsx +21 -0
- package/src/sources/sources.css +266 -0
- package/src/sources/sources.ts +141 -0
- package/src/storybook/current-session-fixtures.ts +1492 -0
- package/src/storybook/current-session-scenarios.ts +76 -0
- package/src/storybook/current-session-story.tsx +154 -0
- package/src/storybook/index.ts +43 -0
- package/src/styles/index.css +19 -0
- package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
- package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
- package/src/timeline/code-blocks.stories.tsx +105 -0
- package/src/timeline/commentary.stories.tsx +70 -0
- package/src/timeline/commentary.ts +38 -0
- package/src/timeline/detail.ts +111 -0
- package/src/timeline/error-card.stories.tsx +134 -0
- package/src/timeline/error-spacing.stories.tsx +71 -0
- package/src/timeline/facts.ts +493 -0
- package/src/timeline/file-changes.stories.tsx +436 -0
- package/src/timeline/group-label-fixtures.ts +97 -0
- package/src/timeline/mermaid.stories.tsx +131 -0
- package/src/timeline/place.ts +280 -0
- package/src/timeline/placement-fixtures.ts +365 -0
- package/src/timeline/placement-reactivity.ts +133 -0
- package/src/timeline/projection.ts +1261 -0
- package/src/timeline/research-and-agents.stories.tsx +461 -0
- package/src/timeline/session-timeline-row.tsx +1675 -0
- package/src/timeline/session-timeline.tsx +68 -0
- package/src/timeline/steer-arrival-machine.ts +134 -0
- package/src/timeline/terminal-work.stories.tsx +464 -0
- package/src/timeline/timeline-row.stories.tsx +1069 -0
- package/src/timeline/timeline-row.ts +238 -0
- package/src/timeline/turn-phase-machine.ts +112 -0
- package/src/timeline/user-message-fold.stories.tsx +194 -0
- package/src/timeline/user-runs.ts +87 -0
- package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
- package/src/timeline-next/compaction.tsx +236 -0
- package/src/timeline-next/errors.ts +101 -0
- package/src/timeline-next/event-row.tsx +168 -0
- package/src/timeline-next/events.ts +103 -0
- package/src/timeline-next/index.ts +10 -0
- package/src/timeline-next/nested-rail.ts +125 -0
- package/src/timeline-next/projection.ts +1028 -0
- package/src/timeline-next/row.tsx +1728 -0
- package/src/timeline-next/rules.ts +89 -0
- package/src/timeline-next/separator.tsx +73 -0
- package/src/timeline-next/spacing.ts +127 -0
- package/src/timeline-next/status-line.tsx +41 -0
- package/src/timeline-next/status.ts +33 -0
- package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
- package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
- package/src/timeline-next/stories/compare.stories.tsx +486 -0
- package/src/timeline-next/stories/playground.stories.tsx +297 -0
- package/src/timeline-next/stories/session-replay.ts +1156 -0
- package/src/timeline-next/stories/session-timeline.tsx +50 -0
- package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
- package/src/timeline-next/timeline-next.css +649 -0
- package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
- package/src/timeline-next/tools/activity-label.ts +166 -0
- package/src/timeline-next/tools/assistant-content.tsx +84 -0
- package/src/timeline-next/tools/context-group.tsx +619 -0
- package/src/timeline-next/tools/error-card.tsx +147 -0
- package/src/timeline-next/tools/explore.tsx +119 -0
- package/src/timeline-next/tools/file-group.tsx +128 -0
- package/src/timeline-next/tools/patch.tsx +174 -0
- package/src/timeline-next/tools/shell.tsx +186 -0
- package/src/timeline-next/tools/thought.tsx +87 -0
- package/src/timeline-next/tools/tool-display.tsx +119 -0
- package/src/tools/ansi.ts +8 -0
- package/src/tools/context-group-label.ts +123 -0
- package/src/tools/file-runs.ts +35 -0
- package/src/tools/project-path.ts +18 -0
- package/src/tools/read-group.stories.tsx +159 -0
- package/src/tools/run-machine.ts +185 -0
- package/src/tools/shell-output-machine.ts +233 -0
- package/src/tools/shell-output.ts +91 -0
- package/src/tools/subagent-row-variants.stories.tsx +129 -0
- package/src/tools/tool-group.stories.tsx +345 -0
- package/src/tools/tool-renderer.tsx +2876 -0
- package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
- package/src/v2/components/session-file-panel-v2.tsx +149 -0
- package/src/v2/components/session-progress-indicator-v2.css +5 -0
- package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
- package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
- package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
- package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
- package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
- package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
- package/src/v2/components/session-review-v2.css +653 -0
- package/src/v2/components/session-review-v2.tsx +422 -0
- package/src/v2/components/sidebar-presence-machine.ts +69 -0
- package/tsconfig.json +20 -0
- package/README.md +0 -3
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { MarkdownCodeBlock, MarkdownCodeBlockMatch } from "../context/markdown"
|
|
2
|
+
|
|
3
|
+
/** A complete block as plugin content or actions received it; another key means the content must mount again. */
|
|
4
|
+
export type CodeBlockKey = {
|
|
5
|
+
readonly match: MarkdownCodeBlockMatch
|
|
6
|
+
readonly language: string
|
|
7
|
+
readonly source: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** What a Mermaid render draws: whole lines while the block streams, the full source once it is complete. */
|
|
11
|
+
export type MermaidInput = { readonly input: string; readonly complete: boolean }
|
|
12
|
+
|
|
13
|
+
export type MermaidRender = MermaidInput & { readonly attempt: number }
|
|
14
|
+
|
|
15
|
+
type Shared = {
|
|
16
|
+
/** Started renders and plugin mounts. A result whose attempt is not the current one is stale. */
|
|
17
|
+
readonly attempts: number
|
|
18
|
+
readonly actions?: CodeBlockKey
|
|
19
|
+
/** Plugin content that declined or failed for this exact block, so the block keeps its code until it changes. */
|
|
20
|
+
readonly declined?: CodeBlockKey
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* One fenced code block. `code` shows the highlighted source, `diagram-pending` the empty diagram box until the first
|
|
25
|
+
* Mermaid render lands, `diagram` the last good SVG, and `plugin` content a plugin mounted for the block's language.
|
|
26
|
+
* `render` is the Mermaid render in flight; `tried` is the input of the last one that settled, so the same input never
|
|
27
|
+
* renders twice.
|
|
28
|
+
*/
|
|
29
|
+
export type CodeDecorationState =
|
|
30
|
+
| (Shared & { readonly status: "code"; readonly render?: MermaidRender; readonly tried?: MermaidInput })
|
|
31
|
+
| (Shared & { readonly status: "diagram-pending"; readonly render?: MermaidRender })
|
|
32
|
+
| (Shared & {
|
|
33
|
+
readonly status: "diagram"
|
|
34
|
+
readonly svg: string
|
|
35
|
+
readonly render?: MermaidRender
|
|
36
|
+
readonly tried: MermaidInput
|
|
37
|
+
})
|
|
38
|
+
| (Shared & { readonly status: "plugin"; readonly plugin: CodeBlockKey })
|
|
39
|
+
|
|
40
|
+
export type CodeDecorationEvent =
|
|
41
|
+
| {
|
|
42
|
+
readonly type: "sync"
|
|
43
|
+
readonly language: string
|
|
44
|
+
readonly source: string
|
|
45
|
+
readonly complete: boolean
|
|
46
|
+
readonly match: MarkdownCodeBlockMatch | undefined
|
|
47
|
+
}
|
|
48
|
+
| { readonly type: "mermaid.resolved"; readonly attempt: number; readonly svg: string | undefined }
|
|
49
|
+
| { readonly type: "mermaid.failed"; readonly attempt: number }
|
|
50
|
+
| { readonly type: "plugin.failed"; readonly attempt: number }
|
|
51
|
+
| { readonly type: "disposed" }
|
|
52
|
+
|
|
53
|
+
export type CodeDecorationEffect =
|
|
54
|
+
| { readonly type: "clear-actions" }
|
|
55
|
+
| { readonly type: "clear-plugin" }
|
|
56
|
+
| { readonly type: "mount-actions"; readonly block: MarkdownCodeBlock }
|
|
57
|
+
| { readonly type: "mount-plugin"; readonly attempt: number; readonly block: MarkdownCodeBlock }
|
|
58
|
+
| { readonly type: "render-mermaid"; readonly attempt: number; readonly input: string }
|
|
59
|
+
|
|
60
|
+
export type CodeDecorationTransition = {
|
|
61
|
+
readonly state: CodeDecorationState
|
|
62
|
+
readonly effects: readonly CodeDecorationEffect[]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type Sync = Extract<CodeDecorationEvent, { type: "sync" }>
|
|
66
|
+
|
|
67
|
+
export const INITIAL_CODE_DECORATION_STATE: CodeDecorationState = Object.freeze({ status: "code", attempts: 0 })
|
|
68
|
+
|
|
69
|
+
export const EMPTY_CODE_EFFECTS: readonly CodeDecorationEffect[] = Object.freeze([])
|
|
70
|
+
|
|
71
|
+
export function transitionCodeDecoration(
|
|
72
|
+
state: CodeDecorationState,
|
|
73
|
+
event: CodeDecorationEvent,
|
|
74
|
+
): CodeDecorationTransition {
|
|
75
|
+
if (event.type === "sync") return settle(state, sync(state, event, state.declined))
|
|
76
|
+
|
|
77
|
+
if (event.type === "plugin.failed") {
|
|
78
|
+
if (state.status !== "plugin" || event.attempt !== state.attempts) return { state, effects: EMPTY_CODE_EFFECTS }
|
|
79
|
+
const plugin = state.plugin
|
|
80
|
+
|
|
81
|
+
return settle(state, sync(state, { type: "sync", ...plugin, complete: true }, plugin))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (event.type === "disposed") return settle(state, { status: "code", attempts: state.attempts })
|
|
85
|
+
|
|
86
|
+
if (state.status === "plugin" || state.render?.attempt !== event.attempt)
|
|
87
|
+
return { state, effects: EMPTY_CODE_EFFECTS }
|
|
88
|
+
const render = state.render
|
|
89
|
+
const shared = { attempts: state.attempts, actions: state.actions, declined: state.declined }
|
|
90
|
+
|
|
91
|
+
if (event.type === "mermaid.resolved" && event.svg)
|
|
92
|
+
return settle(state, { ...shared, status: "diagram", svg: event.svg, tried: render })
|
|
93
|
+
|
|
94
|
+
// A partial diagram often fails until more lines arrive, so the last good one stays; a complete failure shows the code.
|
|
95
|
+
if (state.status === "diagram" && !render.complete)
|
|
96
|
+
return settle(state, { ...shared, status: "diagram", svg: state.svg, tried: render })
|
|
97
|
+
|
|
98
|
+
return settle(state, { ...shared, status: "code", tried: render })
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function viewCodeDecoration(state: CodeDecorationState) {
|
|
102
|
+
return {
|
|
103
|
+
diagram: state.status === "diagram-pending" || state.status === "diagram",
|
|
104
|
+
pending: state.status === "diagram-pending",
|
|
105
|
+
svg: state.status === "diagram" ? state.svg : undefined,
|
|
106
|
+
content: state.status === "plugin",
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function sync(state: CodeDecorationState, event: Sync, declined: CodeBlockKey | undefined): CodeDecorationState {
|
|
111
|
+
const match = event.match
|
|
112
|
+
const claimed = !!match?.render.has(event.language)
|
|
113
|
+
const failed = declined && sameBlock(declined, event) ? declined : undefined
|
|
114
|
+
|
|
115
|
+
const actions =
|
|
116
|
+
!match || !event.complete || !match.actions.has(event.language)
|
|
117
|
+
? undefined
|
|
118
|
+
: state.actions && sameBlock(state.actions, event)
|
|
119
|
+
? state.actions
|
|
120
|
+
: { match, language: event.language, source: event.source }
|
|
121
|
+
|
|
122
|
+
if (match && claimed && event.complete && !failed) {
|
|
123
|
+
if (state.status === "plugin" && sameBlock(state.plugin, event)) return keep(state, actions, failed)
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
status: "plugin",
|
|
127
|
+
attempts: state.attempts + 1,
|
|
128
|
+
plugin: { match, language: event.language, source: event.source },
|
|
129
|
+
actions,
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Claimed content waits for the closing fence and gets the code until then, not the built-in diagram.
|
|
134
|
+
if (event.language !== "mermaid" || !event.source || (claimed && !failed)) {
|
|
135
|
+
if (state.status === "code" && !state.render && !state.tried) return keep(state, actions, failed)
|
|
136
|
+
|
|
137
|
+
return { status: "code", attempts: state.attempts, actions, declined: failed }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const input = event.complete ? event.source : event.source.slice(0, event.source.lastIndexOf("\n") + 1)
|
|
141
|
+
|
|
142
|
+
if (state.status === "plugin" || (state.status === "code" && !state.render && !state.tried)) {
|
|
143
|
+
if (!input) return { status: "diagram-pending", attempts: state.attempts, actions, declined: failed }
|
|
144
|
+
const attempt = state.attempts + 1
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
status: "diagram-pending",
|
|
148
|
+
attempts: attempt,
|
|
149
|
+
render: { attempt, input, complete: event.complete },
|
|
150
|
+
actions,
|
|
151
|
+
declined: failed,
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const latest = state.render ?? (state.status === "diagram-pending" ? undefined : state.tried)
|
|
156
|
+
|
|
157
|
+
if (!input || (latest?.input === input && latest.complete === event.complete)) return keep(state, actions, failed)
|
|
158
|
+
const render = { attempt: state.attempts + 1, input, complete: event.complete }
|
|
159
|
+
|
|
160
|
+
return { ...state, attempts: render.attempt, render, actions, declined: failed }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function keep(state: CodeDecorationState, actions: CodeBlockKey | undefined, declined: CodeBlockKey | undefined) {
|
|
164
|
+
if (state.actions === actions && state.declined === declined) return state
|
|
165
|
+
|
|
166
|
+
return { ...state, actions, declined }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Effects follow from the difference between the two states, so every transition mounts and clears the same way.
|
|
170
|
+
function settle(state: CodeDecorationState, next: CodeDecorationState): CodeDecorationTransition {
|
|
171
|
+
if (next === state) return { state, effects: EMPTY_CODE_EFFECTS }
|
|
172
|
+
const plugin = state.status === "plugin" ? state.attempts : undefined
|
|
173
|
+
const nextPlugin = next.status === "plugin" ? next : undefined
|
|
174
|
+
const render = state.status === "plugin" ? undefined : state.render
|
|
175
|
+
const nextRender = next.status === "plugin" ? undefined : next.render
|
|
176
|
+
|
|
177
|
+
const effects: CodeDecorationEffect[] = [
|
|
178
|
+
...(state.actions && state.actions !== next.actions ? [{ type: "clear-actions" as const }] : []),
|
|
179
|
+
...(plugin !== undefined && plugin !== nextPlugin?.attempts ? [{ type: "clear-plugin" as const }] : []),
|
|
180
|
+
...(next.actions && next.actions !== state.actions
|
|
181
|
+
? [{ type: "mount-actions" as const, block: block(next.actions) }]
|
|
182
|
+
: []),
|
|
183
|
+
...(nextPlugin && nextPlugin.attempts !== plugin
|
|
184
|
+
? [{ type: "mount-plugin" as const, attempt: nextPlugin.attempts, block: block(nextPlugin.plugin) }]
|
|
185
|
+
: []),
|
|
186
|
+
...(nextRender && nextRender !== render
|
|
187
|
+
? [{ type: "render-mermaid" as const, attempt: nextRender.attempt, input: nextRender.input }]
|
|
188
|
+
: []),
|
|
189
|
+
]
|
|
190
|
+
|
|
191
|
+
return { state: next, effects }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function block(key: CodeBlockKey): MarkdownCodeBlock {
|
|
195
|
+
return { code: key.source.replace(/\n$/, ""), language: key.language }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function sameBlock(key: CodeBlockKey, event: Pick<Sync, "match" | "language" | "source">) {
|
|
199
|
+
return key.match === event.match && key.language === event.language && key.source === event.source
|
|
200
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { MarkdownToken } from "./markdown-worker-protocol"
|
|
2
|
+
|
|
3
|
+
export type RenderedCodeState = {
|
|
4
|
+
language: string
|
|
5
|
+
generation: number
|
|
6
|
+
stableCount: number
|
|
7
|
+
unstable: MarkdownToken[]
|
|
8
|
+
raw: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function shouldResetCodeTokens(
|
|
12
|
+
previous: RenderedCodeState | undefined,
|
|
13
|
+
next: { language: string; generation: number; stableCount: number; raw: string },
|
|
14
|
+
) {
|
|
15
|
+
return (
|
|
16
|
+
!previous ||
|
|
17
|
+
previous.language !== next.language ||
|
|
18
|
+
previous.generation !== next.generation ||
|
|
19
|
+
next.stableCount < previous.stableCount ||
|
|
20
|
+
!next.raw.startsWith(previous.raw)
|
|
21
|
+
)
|
|
22
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** A heading's anchor as GitHub writes it: lowercased, punctuation dropped, spaces as hyphens. */
|
|
2
|
+
export function headingSlug(text: string) {
|
|
3
|
+
return text
|
|
4
|
+
.trim()
|
|
5
|
+
.toLowerCase()
|
|
6
|
+
.replace(/[^\p{L}\p{M}\p{N}\p{Pc}\- ]/gu, "")
|
|
7
|
+
.replaceAll(" ", "-")
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The element a `#fragment` link names in this document: a heading by its slug, where repeated headings take `-1`,
|
|
12
|
+
* `-2` and so on, or an element whose raw HTML `id` or `name` it is.
|
|
13
|
+
*/
|
|
14
|
+
export function fragmentTarget(root: HTMLElement, fragment: string) {
|
|
15
|
+
const slug = fragment.toLowerCase()
|
|
16
|
+
const seen = new Map<string, number>()
|
|
17
|
+
|
|
18
|
+
const heading = Array.from(root.querySelectorAll<HTMLElement>("h1, h2, h3, h4, h5, h6")).find((heading) => {
|
|
19
|
+
const base = headingSlug(heading.textContent ?? "")
|
|
20
|
+
const count = seen.get(base) ?? 0
|
|
21
|
+
seen.set(base, count + 1)
|
|
22
|
+
|
|
23
|
+
return (count === 0 ? base : `${base}-${count}`) === slug
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
if (heading) return heading
|
|
27
|
+
// Sanitizing prefixes raw HTML `id` and `name` so they cannot clobber the app's own globals.
|
|
28
|
+
const named = CSS.escape(`user-content-${fragment}`)
|
|
29
|
+
|
|
30
|
+
return root.querySelector<HTMLElement>(`[id="${named}"], [name="${named}"]`) ?? undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Fragment links scroll to their target in this document instead of navigating the app. */
|
|
34
|
+
export function setupFragmentLinks(root: HTMLElement) {
|
|
35
|
+
const click = (event: MouseEvent) => {
|
|
36
|
+
if (event.defaultPrevented || event.button !== 0 || !(event.target instanceof Element)) return
|
|
37
|
+
const link = event.target.closest('a[href^="#"]')
|
|
38
|
+
|
|
39
|
+
if (!(link instanceof HTMLAnchorElement) || !root.contains(link)) return
|
|
40
|
+
event.preventDefault()
|
|
41
|
+
const fragment = decodeFragment(link.getAttribute("href")!.slice(1))
|
|
42
|
+
const target = fragment ? fragmentTarget(root, fragment) : undefined
|
|
43
|
+
|
|
44
|
+
if (!target) return
|
|
45
|
+
const scroller = scrollParent(root)
|
|
46
|
+
|
|
47
|
+
// `scrollIntoView` would also scroll clipped ancestors and shift the surrounding layout.
|
|
48
|
+
if (!scroller) return target.scrollIntoView({ block: "start" })
|
|
49
|
+
scroller.scrollTo({
|
|
50
|
+
top: scroller.scrollTop + target.getBoundingClientRect().top - scroller.getBoundingClientRect().top,
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
root.addEventListener("click", click)
|
|
55
|
+
|
|
56
|
+
return () => root.removeEventListener("click", click)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function scrollParent(element: HTMLElement) {
|
|
60
|
+
let parent = element.parentElement
|
|
61
|
+
|
|
62
|
+
while (parent) {
|
|
63
|
+
if (/auto|scroll/.test(getComputedStyle(parent).overflowY)) return parent
|
|
64
|
+
parent = parent.parentElement
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function decodeFragment(value: string) {
|
|
69
|
+
try {
|
|
70
|
+
return decodeURIComponent(value)
|
|
71
|
+
} catch {
|
|
72
|
+
return value
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
|
|
2
|
+
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.23.49-2.69-.94-2.69-.94-.36-.92-.89-1.17-.89-1.17-.73-.5.06-.49.06-.49.8.06 1.22.82 1.22.82.71 1.21 1.87.86 2.33.66.07-.51.28-.86.51-1.06-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27s1.36.09 2 .27c1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.19 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/** A read that has not settled after this long is aborted and retried, so a stalled request cannot hang the image. */
|
|
2
|
+
export const IMAGE_READ_TIMEOUT_MS = 15_000
|
|
3
|
+
|
|
4
|
+
/** The first retry waits this long; each later retry doubles it. */
|
|
5
|
+
export const IMAGE_RETRY_BASE_MS = 1_000
|
|
6
|
+
|
|
7
|
+
export const IMAGE_MAX_RETRIES = 3
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* One local Markdown image path while it is in the container.
|
|
11
|
+
* - `loading`: a read is in flight.
|
|
12
|
+
* - `waiting`: a read failed or timed out and the next one is backing off.
|
|
13
|
+
* - `loaded`: resolved to an image `src`; `revoke` marks a `blob:` URL the machine must release (an SVG `data:` URL
|
|
14
|
+
* needs no release).
|
|
15
|
+
* - `failed`: the reader has nothing to show (`undefined`) or every retry failed; the image keeps its alt text.
|
|
16
|
+
*/
|
|
17
|
+
export type MarkdownImageEntry =
|
|
18
|
+
| { readonly status: "loading"; readonly attempt: number; readonly failures: number }
|
|
19
|
+
| { readonly status: "waiting"; readonly attempt: number; readonly failures: number }
|
|
20
|
+
| { readonly status: "loaded"; readonly url: string; readonly revoke: boolean }
|
|
21
|
+
| { readonly status: "failed" }
|
|
22
|
+
|
|
23
|
+
export type MarkdownImagesState =
|
|
24
|
+
| {
|
|
25
|
+
readonly status: "active"
|
|
26
|
+
/** Last attempt id handed out. Each read and each backoff gets a new one, so late results are recognised. */
|
|
27
|
+
readonly attempts: number
|
|
28
|
+
readonly entries: ReadonlyMap<string, MarkdownImageEntry>
|
|
29
|
+
}
|
|
30
|
+
| { readonly status: "disposed" }
|
|
31
|
+
|
|
32
|
+
export type MarkdownImagesEvent =
|
|
33
|
+
| { readonly type: "paths.synced"; readonly paths: ReadonlySet<string> }
|
|
34
|
+
| {
|
|
35
|
+
readonly type: "read.succeeded"
|
|
36
|
+
readonly path: string
|
|
37
|
+
readonly attempt: number
|
|
38
|
+
readonly url: string
|
|
39
|
+
readonly revoke: boolean
|
|
40
|
+
}
|
|
41
|
+
| { readonly type: "read.empty"; readonly path: string; readonly attempt: number }
|
|
42
|
+
| { readonly type: "read.failed"; readonly path: string; readonly attempt: number }
|
|
43
|
+
| { readonly type: "read.timed-out"; readonly path: string; readonly attempt: number }
|
|
44
|
+
| { readonly type: "retry.due"; readonly path: string; readonly attempt: number }
|
|
45
|
+
| { readonly type: "disposed" }
|
|
46
|
+
|
|
47
|
+
export type MarkdownImagesEffect =
|
|
48
|
+
| { readonly type: "read"; readonly path: string; readonly attempt: number; readonly timeoutMs: number }
|
|
49
|
+
| { readonly type: "wait"; readonly path: string; readonly attempt: number; readonly delayMs: number }
|
|
50
|
+
| { readonly type: "cancel"; readonly attempt: number }
|
|
51
|
+
| { readonly type: "revoke-url"; readonly url: string }
|
|
52
|
+
|
|
53
|
+
export type MarkdownImagesTransition = {
|
|
54
|
+
readonly state: MarkdownImagesState
|
|
55
|
+
readonly effects: readonly MarkdownImagesEffect[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const INITIAL_MARKDOWN_IMAGES_STATE: MarkdownImagesState = Object.freeze({
|
|
59
|
+
status: "active",
|
|
60
|
+
attempts: 0,
|
|
61
|
+
entries: new Map(),
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
export const DISPOSED_MARKDOWN_IMAGES_STATE: MarkdownImagesState = Object.freeze({ status: "disposed" })
|
|
65
|
+
|
|
66
|
+
export const NO_MARKDOWN_IMAGE_EFFECTS: readonly MarkdownImagesEffect[] = Object.freeze([])
|
|
67
|
+
|
|
68
|
+
export function transitionMarkdownImages(
|
|
69
|
+
state: MarkdownImagesState,
|
|
70
|
+
event: MarkdownImagesEvent,
|
|
71
|
+
): MarkdownImagesTransition {
|
|
72
|
+
if (state.status === "disposed") return ignore(state, event)
|
|
73
|
+
|
|
74
|
+
if (event.type === "disposed")
|
|
75
|
+
return { state: DISPOSED_MARKDOWN_IMAGES_STATE, effects: [...state.entries.values()].flatMap(release) }
|
|
76
|
+
|
|
77
|
+
if (event.type === "paths.synced") return sync(state, event.paths)
|
|
78
|
+
|
|
79
|
+
if (event.type === "retry.due") {
|
|
80
|
+
const entry = state.entries.get(event.path)
|
|
81
|
+
|
|
82
|
+
if (entry?.status !== "waiting" || entry.attempt !== event.attempt) return ignore(state, event)
|
|
83
|
+
const attempt = state.attempts + 1
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
state: withEntry(state, event.path, { status: "loading", attempt, failures: entry.failures }, attempt),
|
|
87
|
+
effects: [
|
|
88
|
+
{ type: "cancel", attempt: entry.attempt },
|
|
89
|
+
{ type: "read", path: event.path, attempt, timeoutMs: IMAGE_READ_TIMEOUT_MS },
|
|
90
|
+
],
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const entry = state.entries.get(event.path)
|
|
95
|
+
|
|
96
|
+
if (entry?.status !== "loading" || entry.attempt !== event.attempt) return ignore(state, event)
|
|
97
|
+
const cancel = { type: "cancel", attempt: entry.attempt } as const
|
|
98
|
+
|
|
99
|
+
if (event.type === "read.succeeded")
|
|
100
|
+
return {
|
|
101
|
+
state: withEntry(state, event.path, { status: "loaded", url: event.url, revoke: event.revoke }, state.attempts),
|
|
102
|
+
effects: [cancel],
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (event.type === "read.empty" || entry.failures >= IMAGE_MAX_RETRIES)
|
|
106
|
+
return { state: withEntry(state, event.path, { status: "failed" }, state.attempts), effects: [cancel] }
|
|
107
|
+
const attempt = state.attempts + 1
|
|
108
|
+
const failures = entry.failures + 1
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
state: withEntry(state, event.path, { status: "waiting", attempt, failures }, attempt),
|
|
112
|
+
effects: [cancel, { type: "wait", path: event.path, attempt, delayMs: IMAGE_RETRY_BASE_MS * 2 ** entry.failures }],
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function selectMarkdownImageUrl(state: MarkdownImagesState, path: string) {
|
|
117
|
+
if (state.status === "disposed") return
|
|
118
|
+
const entry = state.entries.get(path)
|
|
119
|
+
|
|
120
|
+
return entry?.status === "loaded" ? entry.url : undefined
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function sync(
|
|
124
|
+
state: Extract<MarkdownImagesState, { status: "active" }>,
|
|
125
|
+
paths: ReadonlySet<string>,
|
|
126
|
+
): MarkdownImagesTransition {
|
|
127
|
+
const removed = [...state.entries].filter(([path]) => !paths.has(path))
|
|
128
|
+
const added = [...paths].filter((path) => path && !state.entries.has(path))
|
|
129
|
+
|
|
130
|
+
if (removed.length === 0 && added.length === 0) return { state, effects: NO_MARKDOWN_IMAGE_EFFECTS }
|
|
131
|
+
const entries = new Map(state.entries)
|
|
132
|
+
removed.forEach(([path]) => entries.delete(path))
|
|
133
|
+
|
|
134
|
+
const reads = added.map((path, index) => {
|
|
135
|
+
const attempt = state.attempts + index + 1
|
|
136
|
+
entries.set(path, { status: "loading", attempt, failures: 0 })
|
|
137
|
+
|
|
138
|
+
return { type: "read", path, attempt, timeoutMs: IMAGE_READ_TIMEOUT_MS } as const
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
state: { status: "active", attempts: state.attempts + added.length, entries },
|
|
143
|
+
effects: [...removed.flatMap(([, entry]) => release(entry)), ...reads],
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function release(entry: MarkdownImageEntry): MarkdownImagesEffect[] {
|
|
148
|
+
if (entry.status === "loading" || entry.status === "waiting") return [{ type: "cancel", attempt: entry.attempt }]
|
|
149
|
+
|
|
150
|
+
if (entry.status === "loaded" && entry.revoke) return [{ type: "revoke-url", url: entry.url }]
|
|
151
|
+
|
|
152
|
+
return []
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function withEntry(
|
|
156
|
+
state: Extract<MarkdownImagesState, { status: "active" }>,
|
|
157
|
+
path: string,
|
|
158
|
+
entry: MarkdownImageEntry,
|
|
159
|
+
attempts: number,
|
|
160
|
+
): MarkdownImagesState {
|
|
161
|
+
return { status: "active", attempts, entries: new Map(state.entries).set(path, entry) }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// A reader that ignores its abort signal can still hand back a blob URL after the entry moved on; release it.
|
|
165
|
+
function ignore(state: MarkdownImagesState, event: MarkdownImagesEvent): MarkdownImagesTransition {
|
|
166
|
+
if (event.type === "read.succeeded" && event.revoke)
|
|
167
|
+
return { state, effects: [{ type: "revoke-url", url: event.url }] }
|
|
168
|
+
|
|
169
|
+
return { state, effects: NO_MARKDOWN_IMAGE_EFFECTS }
|
|
170
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { abort } from "@zyro-sec/util/abort"
|
|
2
|
+
import { isLineRangeHash, parsePathLineSuffix } from "@zyro-sec/util/path"
|
|
3
|
+
import type { ReadMarkdownImage } from "../context/markdown"
|
|
4
|
+
import {
|
|
5
|
+
INITIAL_MARKDOWN_IMAGES_STATE,
|
|
6
|
+
selectMarkdownImageUrl,
|
|
7
|
+
transitionMarkdownImages,
|
|
8
|
+
type MarkdownImagesEffect,
|
|
9
|
+
type MarkdownImagesEvent,
|
|
10
|
+
} from "./markdown-image-machine"
|
|
11
|
+
|
|
12
|
+
export function localImagePath(source: string) {
|
|
13
|
+
// Marked percent-encodes image sources, so `C:\tmp\chart.png` arrives as `C:%5Ctmp%5Cchart.png`.
|
|
14
|
+
const value = source.trim().replace(/\\|%5c/gi, "/")
|
|
15
|
+
|
|
16
|
+
if (!value || /[\u0000-\u001f\u007f]/.test(value) || value.startsWith("//")) return
|
|
17
|
+
|
|
18
|
+
if (/^file:/i.test(value)) {
|
|
19
|
+
if (!URL.canParse(value)) return
|
|
20
|
+
const url = new URL(value)
|
|
21
|
+
|
|
22
|
+
if (url.hostname && url.hostname !== "localhost") return
|
|
23
|
+
|
|
24
|
+
return decodePath(url.pathname.replace(/^\/([a-z]:\/)/i, "$1"))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(value) && !/^[a-z]:\//i.test(value)) return
|
|
28
|
+
|
|
29
|
+
return decodePath(value)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A link is local when it names a file on disk instead of a web resource. Fragment-only and
|
|
34
|
+
* query-only hrefs stay in-page; mailto and other schemes stay external. Line-range fragments
|
|
35
|
+
* such as `#L42` and `#L42-L58` stay attached so the file viewer can select the target lines.
|
|
36
|
+
*/
|
|
37
|
+
export function localLinkPath(href: string) {
|
|
38
|
+
const value = href.trim()
|
|
39
|
+
|
|
40
|
+
if (!value || value.startsWith("#") || value.startsWith("?")) return
|
|
41
|
+
|
|
42
|
+
const hashIndex = value.indexOf("#")
|
|
43
|
+
const hash = hashIndex === -1 ? "" : value.slice(hashIndex)
|
|
44
|
+
const target = (value.split(/[?#]/, 1)[0] ?? "").replace(/\\|%5c/gi, "/")
|
|
45
|
+
// `app.tsx:42` is a file and line, not a URL scheme. The `.` or `/` keeps `tel:5551234` external.
|
|
46
|
+
const cited = parsePathLineSuffix(target)
|
|
47
|
+
const line = cited.path !== target && /[./]/.test(cited.path) ? cited : undefined
|
|
48
|
+
const base = localImagePath(line?.path ?? target)
|
|
49
|
+
|
|
50
|
+
if (!base) return
|
|
51
|
+
|
|
52
|
+
if (line?.selection) {
|
|
53
|
+
const range = line.selection.end === line.selection.start ? "" : `-L${line.selection.end}`
|
|
54
|
+
|
|
55
|
+
return `${base}#L${line.selection.start}${range}`
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return `${base}${isLineRangeHash(hash) ? hash : ""}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function decodePath(value: string) {
|
|
62
|
+
try {
|
|
63
|
+
const path = decodeURIComponent(value)
|
|
64
|
+
|
|
65
|
+
if (/[\u0000-\u001f\u007f]/.test(path) || path.startsWith("//") || path.startsWith("\\\\")) return
|
|
66
|
+
|
|
67
|
+
return path
|
|
68
|
+
} catch {
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function createMarkdownImages(read: ReadMarkdownImage) {
|
|
74
|
+
let state = INITIAL_MARKDOWN_IMAGES_STATE
|
|
75
|
+
let root: HTMLElement | undefined
|
|
76
|
+
const pending = new Map<number, { controller?: AbortController; timer: ReturnType<typeof setTimeout> }>()
|
|
77
|
+
|
|
78
|
+
const images = (container: HTMLElement) =>
|
|
79
|
+
Array.from(container.querySelectorAll<HTMLImageElement>("img[data-local-image]"))
|
|
80
|
+
|
|
81
|
+
const syncDom = (container: HTMLElement) =>
|
|
82
|
+
images(container).forEach((image) => {
|
|
83
|
+
const url = selectMarkdownImageUrl(state, image.dataset.localImage ?? "")
|
|
84
|
+
|
|
85
|
+
if (url && image.getAttribute("src") !== url) image.src = url
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const dispatch = (event: MarkdownImagesEvent) => {
|
|
89
|
+
const next = transitionMarkdownImages(state, event)
|
|
90
|
+
const changed = next.state !== state
|
|
91
|
+
state = next.state
|
|
92
|
+
|
|
93
|
+
if (changed && root) syncDom(root)
|
|
94
|
+
next.effects.forEach(run)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const run = (effect: MarkdownImagesEffect) => {
|
|
98
|
+
if (effect.type === "revoke-url") return URL.revokeObjectURL(effect.url)
|
|
99
|
+
|
|
100
|
+
if (effect.type === "cancel") {
|
|
101
|
+
const item = pending.get(effect.attempt)
|
|
102
|
+
pending.delete(effect.attempt)
|
|
103
|
+
|
|
104
|
+
if (!item) return
|
|
105
|
+
clearTimeout(item.timer)
|
|
106
|
+
abort(item.controller)
|
|
107
|
+
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (effect.type === "wait") {
|
|
112
|
+
const retry = { type: "retry.due", path: effect.path, attempt: effect.attempt } as const
|
|
113
|
+
pending.set(effect.attempt, { timer: setTimeout(() => dispatch(retry), effect.delayMs) })
|
|
114
|
+
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const controller = new AbortController()
|
|
119
|
+
const result = { path: effect.path, attempt: effect.attempt }
|
|
120
|
+
const timer = setTimeout(() => dispatch({ type: "read.timed-out", ...result }), effect.timeoutMs)
|
|
121
|
+
pending.set(effect.attempt, { controller, timer })
|
|
122
|
+
void read(effect.path, controller.signal)
|
|
123
|
+
.then(async (blob) => {
|
|
124
|
+
if (controller.signal.aborted) return
|
|
125
|
+
|
|
126
|
+
if (!blob) return dispatch({ type: "read.empty", ...result })
|
|
127
|
+
|
|
128
|
+
// SVG documents must not inherit the app origin if opened outside the image element.
|
|
129
|
+
if (blob.type === "image/svg+xml") {
|
|
130
|
+
const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(await blob.text())}`
|
|
131
|
+
|
|
132
|
+
return dispatch({ type: "read.succeeded", ...result, url, revoke: false })
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
dispatch({ type: "read.succeeded", ...result, url: URL.createObjectURL(blob), revoke: true })
|
|
136
|
+
})
|
|
137
|
+
.catch(() => {
|
|
138
|
+
if (controller.signal.aborted) return
|
|
139
|
+
dispatch({ type: "read.failed", ...result })
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
update(container: HTMLElement) {
|
|
145
|
+
root = container
|
|
146
|
+
const paths = new Set(images(container).flatMap((image) => image.dataset.localImage || []))
|
|
147
|
+
dispatch({ type: "paths.synced", paths })
|
|
148
|
+
// Newly inserted images of an already loaded path get their `src` before paint.
|
|
149
|
+
syncDom(container)
|
|
150
|
+
},
|
|
151
|
+
dispose() {
|
|
152
|
+
root = undefined
|
|
153
|
+
dispatch({ type: "disposed" })
|
|
154
|
+
},
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { parsePathLineSuffix } from "@zyro-sec/util/path"
|
|
2
|
+
|
|
3
|
+
// Common files with no extension. Anything else needs a `.` or `/` to look like a file.
|
|
4
|
+
const fileNames = new Set([
|
|
5
|
+
"CHANGELOG",
|
|
6
|
+
"CODEOWNERS",
|
|
7
|
+
"Containerfile",
|
|
8
|
+
"Dockerfile",
|
|
9
|
+
"Gemfile",
|
|
10
|
+
"Justfile",
|
|
11
|
+
"LICENSE",
|
|
12
|
+
"Makefile",
|
|
13
|
+
"Procfile",
|
|
14
|
+
"README",
|
|
15
|
+
"Rakefile",
|
|
16
|
+
"Vagrantfile",
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `url` for an http(s) URL, `path` for anything that could name a file. A `path` only becomes a link once the host
|
|
21
|
+
* confirms the file exists, so this stays a cheap syntax check.
|
|
22
|
+
*/
|
|
23
|
+
export function inlineCodeKind(text: string): "path" | "url" | undefined {
|
|
24
|
+
if (/^https?:\/\//i.test(text)) return "url"
|
|
25
|
+
|
|
26
|
+
if (!text || /\s/.test(text) || /^[a-z][a-z0-9+.-]*:\/\//i.test(text)) return
|
|
27
|
+
|
|
28
|
+
// Code, globs and markup, not paths.
|
|
29
|
+
if (/[{}<>"'`;=|&*,]/.test(text)) return
|
|
30
|
+
|
|
31
|
+
// A network share (`\\host\share`) would make the server connect to the host just to check it.
|
|
32
|
+
if (/^[\\/]{2}/.test(text)) return
|
|
33
|
+
|
|
34
|
+
const path = parsePathLineSuffix(text).path
|
|
35
|
+
|
|
36
|
+
if (fileNames.has(path)) return "path"
|
|
37
|
+
|
|
38
|
+
if (/[./\\]/.test(path) && /[a-z]/i.test(path)) return "path"
|
|
39
|
+
}
|