@kabmo-labs/kabmo 0.17.0 → 0.18.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/dist/client/index.html +1 -1
- package/dist/client/index.js +538 -457
- package/dist/client/static/js/index-hp75vsv9.css +1 -0
- package/dist/client/static/js/{index-rerjq5th.js → index-tc09dtca.js} +823 -814
- package/package.json +1 -1
- package/src/client/components/common/MarkdownRenderer.tsx +136 -89
- package/src/client/components/common/anchored-menu/index.test.tsx +141 -0
- package/src/client/components/common/anchored-menu/index.tsx +143 -0
- package/src/client/components/common/markdown-review/ReviewPopover.tsx +144 -0
- package/src/client/components/common/markdown-review/ReviewSummary.tsx +108 -0
- package/src/client/components/common/markdown-review/index.tsx +129 -0
- package/src/client/components/common/server-control/Panel.tsx +33 -23
- package/src/client/components/common/server-control/index.test.tsx +11 -5
- package/src/client/components/common/server-control/index.tsx +10 -8
- package/src/client/components/layout/session-sidebar/CategoryHeader.tsx +42 -21
- package/src/client/components/layout/session-sidebar/CategoryHeader.worktree.test.tsx +36 -17
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +8 -7
- package/src/client/components/layout/session-sidebar/about-modal/NotesDialog.tsx +94 -0
- package/src/client/components/layout/session-sidebar/about-modal/VersionLine.test.tsx +68 -0
- package/src/client/components/layout/session-sidebar/about-modal/VersionLine.tsx +36 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +57 -34
- package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +28 -10
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +7 -6
- package/src/client/components/layout/session-sidebar/modern/workspace-header.test.tsx +1 -1
- package/src/client/components/layout/update-popup/ReleaseNotes.tsx +8 -3
- package/src/client/components/layout/update-popup/index.tsx +5 -1
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +13 -15
- package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +23 -1
- package/src/client/components/mobile/mobile-right-sidebar/MissingFileBanner.tsx +30 -0
- package/src/client/components/mobile/mobile-right-sidebar/PreviewScroller.tsx +18 -2
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +22 -67
- package/src/client/components/mobile/mobile-session-sidebar/ItemMenu.tsx +110 -0
- package/src/client/components/settings/SettingsSidebarList.tsx +20 -0
- package/src/client/components/settings/categories/AgentSettings.tsx +23 -50
- package/src/client/components/settings/categories/CommandSettings.tsx +2 -2
- package/src/client/components/settings/categories/McpSettings.tsx +4 -21
- package/src/client/components/settings/categories/OmpSettings.tsx +147 -194
- package/src/client/components/settings/categories/SkillSettings.tsx +2 -2
- package/src/client/components/settings/categories/TokenUsageSettings.tsx +211 -229
- package/src/client/components/settings/categories/agent-settings/DetailPane.tsx +229 -237
- package/src/client/components/settings/categories/agent-settings/ModelPicker.tsx +71 -0
- package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +9 -2
- package/src/client/components/settings/categories/agent-settings/levels.ts +38 -0
- package/src/client/components/settings/categories/behavior-settings/Editor.tsx +27 -7
- package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +23 -5
- package/src/client/components/settings/categories/command-settings/DetailPane.tsx +75 -33
- package/src/client/components/settings/categories/command-settings/SidebarList.tsx +7 -0
- package/src/client/components/settings/categories/mcp-settings/DetailPane.tsx +91 -76
- package/src/client/components/settings/categories/mcp-settings/EnvVarsSection.tsx +83 -0
- package/src/client/components/settings/categories/mcp-settings/ImportModal.tsx +26 -13
- package/src/client/components/settings/categories/mcp-settings/SidebarList.tsx +20 -0
- package/src/client/components/settings/categories/omp-settings/ConfigTabs.tsx +97 -36
- package/src/client/components/settings/categories/omp-settings/EngineHeader.tsx +151 -0
- package/src/client/components/settings/categories/omp-settings/ExtensionsPanel.tsx +68 -32
- package/src/client/components/settings/categories/omp-settings/OrderedChips.tsx +119 -0
- package/src/client/components/settings/categories/omp-settings/PresetBar.tsx +14 -7
- package/src/client/components/settings/categories/omp-settings/RoleEditor.tsx +15 -6
- package/src/client/components/settings/categories/omp-settings/RowEditor.tsx +165 -226
- package/src/client/components/settings/categories/omp-settings/types.ts +28 -0
- package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +35 -7
- package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +34 -14
- package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +21 -0
- package/src/client/components/settings/categories/skill-settings/pane.test.tsx +15 -6
- package/src/client/components/settings/categories/token-usage-settings/Breakdown.tsx +242 -102
- package/src/client/components/settings/categories/token-usage-settings/Chart.tsx +130 -56
- package/src/client/components/settings/categories/token-usage-settings/Controls.tsx +135 -0
- package/src/client/components/settings/categories/token-usage-settings/MetricsGrid.tsx +55 -33
- package/src/client/components/settings/categories/token-usage-settings/Provenance.tsx +59 -0
- package/src/client/components/settings/categories/token-usage-settings/panel.test.tsx +239 -0
- package/src/client/components/workspace/browser-panel/Screencast.test.ts +1 -0
- package/src/client/components/workspace/browser-panel/Screencast.tsx +17 -22
- package/src/client/components/workspace/browser-panel/TabStrip.test.tsx +158 -0
- package/src/client/components/workspace/browser-panel/TabStrip.tsx +90 -0
- package/src/client/components/workspace/browser-panel/index.tsx +50 -4
- package/src/client/components/workspace/browser-toolbar/index.tsx +41 -1
- package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +3 -1
- package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +2 -0
- package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +13 -2
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.test.ts +88 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +24 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.test.ts +34 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +5 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactChip.tsx +7 -6
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.test.tsx +237 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.tsx +191 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +1 -1
- package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +21 -7
- package/src/client/components/workspace/diff-panel/changes.test.ts +51 -0
- package/src/client/components/workspace/diff-panel/changes.ts +28 -12
- package/src/client/components/workspace/diff-panel/merge.test.ts +28 -0
- package/src/client/components/workspace/diff-panel/merge.ts +21 -1
- package/src/client/components/workspace/editor/Body.tsx +7 -0
- package/src/client/components/workspace/editor/FileDocument.tsx +15 -21
- package/src/client/components/workspace/editor/FilePane.tsx +12 -0
- package/src/client/components/workspace/editor/PreviewPane.tsx +70 -0
- package/src/client/components/workspace/editor/Toolbar.tsx +17 -1
- package/src/client/components/workspace/editor/index.tsx +25 -22
- package/src/client/components/workspace/editor/toolbar-clusters.test.ts +14 -0
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +12 -86
- package/src/client/components/workspace/editor/toolbar-toggle.tsx +53 -0
- package/src/client/components/workspace/editor/toolbar-write-clusters.tsx +112 -0
- package/src/client/components/workspace/git-panel/commit-modal.test.ts +5 -3
- package/src/client/components/workspace/user-browser-panel/AddressBar.tsx +5 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateBar.test.tsx +201 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateBar.tsx +112 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateLayer.test.tsx +225 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateLayer.tsx +217 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateTarget.tsx +99 -0
- package/src/client/components/workspace/user-browser-panel/Frame.tsx +30 -1
- package/src/client/components/workspace/user-browser-panel/index.tsx +81 -2
- package/src/client/data/agent-data/builtin.ts +0 -10
- package/src/client/data/agent-data/orchestrator.ts +0 -45
- package/src/client/data/agent-data/specialist.ts +0 -100
- package/src/client/data/mock/token-usage.ts +170 -120
- package/src/client/data/settings/omp-schema.json +33 -1
- package/src/client/hooks/browser/use-annotate.test.ts +143 -0
- package/src/client/hooks/browser/use-annotate.ts +98 -0
- package/src/client/hooks/browser/use-close-tab.test.ts +148 -0
- package/src/client/hooks/browser/use-close-tab.ts +67 -0
- package/src/client/hooks/browser/use-screencast.ts +13 -1
- package/src/client/hooks/chat/composer/browser-context.test.ts +117 -0
- package/src/client/hooks/chat/composer/browser-context.ts +40 -16
- package/src/client/hooks/chat/composer/items.test.ts +1 -1
- package/src/client/hooks/chat/composer/review-context.ts +38 -0
- package/src/client/hooks/chat/composer/use-draft.test.ts +132 -0
- package/src/client/hooks/chat/composer/use-draft.ts +31 -2
- package/src/client/hooks/chat/timeline/row-window.test.ts +15 -2
- package/src/client/hooks/editor/use-document-annotations.ts +124 -0
- package/src/client/hooks/editor/use-editor-panel-ui.ts +52 -0
- package/src/client/hooks/editor/use-file-review-mode.ts +35 -0
- package/src/client/hooks/editor/use-preview-review.ts +148 -0
- package/src/client/hooks/settings/agents.ts +149 -0
- package/src/client/hooks/settings/crud-list.test.ts +28 -6
- package/src/client/hooks/settings/crud-list.ts +96 -57
- package/src/client/hooks/settings/omp-config.ts +164 -0
- package/src/client/hooks/settings/useTokenUsage.ts +125 -0
- package/src/client/hooks/settings/write-feedback.test.ts +104 -0
- package/src/client/hooks/settings/write-feedback.ts +63 -0
- package/src/client/hooks/ui/markdown-hydration.ts +125 -0
- package/src/client/hooks/ui/updates/index.test.ts +100 -1
- package/src/client/hooks/ui/updates/index.ts +50 -0
- package/src/client/hooks/ui/updates/notes.test.ts +138 -0
- package/src/client/hooks/ui/updates/notes.ts +57 -0
- package/src/client/lib/browser/element-at.test.ts +229 -0
- package/src/client/lib/browser/element-at.ts +139 -0
- package/src/client/lib/review/client.ts +73 -0
- package/src/client/lib/router/search-params.ts +42 -20
- package/src/client/tailwind.css +65 -0
- package/src/server/lib/btw/lifecycle.server.ts +2 -2
- package/src/server/lib/db/schema.ts +7 -0
- package/src/server/lib/db/seed-terminal.test.ts +4 -0
- package/src/server/lib/omp/config/agent-definitions.test.ts +273 -0
- package/src/server/lib/omp/config/agents.ts +231 -61
- package/src/server/lib/omp/config/definitions.test.ts +9 -105
- package/src/server/lib/omp/config/extensions.ts +16 -7
- package/src/server/lib/omp/config/omp-config-files.test.ts +27 -0
- package/src/server/lib/omp/rpc/constants.test.ts +11 -0
- package/src/server/lib/omp/rpc/constants.ts +15 -0
- package/src/server/lib/omp/rpc/process.ts +11 -14
- package/src/server/lib/omp/rpc/session-commands.ts +6 -3
- package/src/server/lib/omp/rpc/utility-agent-dir.test.ts +134 -0
- package/src/server/lib/omp/rpc/utility-agent-dir.ts +138 -0
- package/src/server/lib/omp/rpc/utility.ts +12 -0
- package/src/server/lib/review/schema.server.ts +45 -0
- package/src/server/lib/review/store.server.ts +138 -0
- package/src/server/lib/terminal/attach-replay.test.ts +132 -0
- package/src/server/lib/terminal/output.test.ts +166 -0
- package/src/server/lib/terminal/output.ts +104 -0
- package/src/server/lib/terminal/runtime.server.ts +5 -0
- package/src/server/lib/terminal/send-result.test.ts +33 -0
- package/src/server/lib/terminal/send-result.ts +32 -0
- package/src/server/lib/terminal/session.server.ts +45 -11
- package/src/server/lib/terminal/store-liveness.test.ts +4 -0
- package/src/server/lib/updates/active-notes.test.ts +149 -0
- package/src/server/lib/updates/active-notes.ts +75 -0
- package/src/server/lib/updates/changelog-file.ts +42 -11
- package/src/server/lib/updates/changelog.test.ts +15 -1
- package/src/server/lib/updates/omp.ts +42 -15
- package/src/server/lib/updates/release-url.ts +27 -1
- package/src/server/lib/usage/index-parse.test.ts +200 -0
- package/src/server/lib/usage/index-parse.ts +178 -0
- package/src/server/lib/usage/index-query.server.ts +248 -0
- package/src/server/lib/usage/index-scan.server.ts +343 -0
- package/src/server/lib/usage/index-schema.server.ts +82 -0
- package/src/server/lib/usage/index.test.ts +234 -0
- package/src/server/lib/usage/providers.server.ts +4 -53
- package/src/server/routes/browser/index.ts +3 -0
- package/src/server/routes/browser/stream.ts +31 -2
- package/src/server/routes/browser/tabs.test.ts +94 -0
- package/src/server/routes/browser/tabs.ts +85 -0
- package/src/server/routes/index.ts +2 -0
- package/src/server/routes/review/bindings.ts +11 -0
- package/src/server/routes/review/index.ts +127 -0
- package/src/server/routes/review/routes.test.ts +191 -0
- package/src/server/routes/settings/agents.test.ts +180 -0
- package/src/server/routes/settings/agents.ts +95 -102
- package/src/server/routes/settings/mcp.test.ts +135 -0
- package/src/server/routes/settings/mcp.ts +14 -6
- package/src/server/routes/settings/settings-store-routes.test.ts +6 -113
- package/src/server/routes/telemetry/tokens.test.ts +145 -0
- package/src/server/routes/telemetry/tokens.ts +70 -149
- package/src/server/routes/terminal/ws.ts +6 -5
- package/src/server/routes/updates/index.ts +2 -0
- package/src/server/routes/updates/notes.ts +34 -0
- package/src/server/routes/updates/routes.test.ts +49 -0
- package/src/shared/lib/browser/annotate.test.ts +283 -0
- package/src/shared/lib/browser/annotate.ts +231 -0
- package/src/shared/lib/browser/browser-viewer.test.ts +26 -4
- package/src/shared/lib/browser/frame-gate.test.ts +91 -0
- package/src/shared/lib/browser/frame-gate.ts +69 -0
- package/src/shared/lib/browser/frame-params.test.ts +43 -0
- package/src/shared/lib/browser/frame-params.ts +60 -0
- package/src/shared/lib/browser/frame-pipeline.test.ts +132 -0
- package/src/shared/lib/browser/frame-pipeline.ts +80 -0
- package/src/shared/lib/browser/page-context.ts +60 -4
- package/src/shared/lib/browser/snapshot.test.ts +115 -0
- package/src/shared/lib/browser/snapshot.ts +112 -0
- package/src/shared/lib/browser/tab-close.test.ts +74 -0
- package/src/shared/lib/browser/tab-close.ts +77 -0
- package/src/shared/lib/browser/viewer.ts +45 -26
- package/src/shared/lib/chat/timeline/block-chrome.ts +19 -0
- package/src/shared/lib/chat/timeline/tool-render-mode.ts +17 -4
- package/src/shared/lib/chat/tool/artifact.ts +55 -20
- package/src/shared/lib/chat/tool/bash-facts.ts +96 -0
- package/src/shared/lib/chat/tool/minimized.test.ts +98 -0
- package/src/shared/lib/chat/tool/minimized.ts +64 -0
- package/src/shared/lib/chat/tool/summary.test.ts +28 -8
- package/src/shared/lib/chat/tool/summary.ts +3 -32
- package/src/shared/lib/markdown/blocks/anchor.ts +63 -0
- package/src/shared/lib/markdown/blocks/blocks.test.ts +141 -0
- package/src/shared/lib/markdown/blocks/renderer.ts +120 -0
- package/src/shared/lib/markdown/blocks/scan.ts +100 -0
- package/src/shared/lib/markdown/blocks/types.ts +62 -0
- package/src/shared/lib/markdown/marked.ts +58 -147
- package/src/shared/lib/markdown/math-extensions.ts +168 -0
- package/src/shared/lib/markdown/review/prompt.ts +77 -0
- package/src/shared/lib/markdown/review/reconcile.ts +55 -0
- package/src/shared/lib/markdown/review/review.test.ts +96 -0
- package/src/shared/lib/markdown/xml-sections.test.ts +140 -0
- package/src/shared/lib/markdown/xml-sections.ts +139 -0
- package/src/shared/lib/models/provider/label.ts +57 -0
- package/src/shared/lib/omp/config/agent-file.test.ts +54 -0
- package/src/shared/lib/omp/config/agent-file.ts +43 -0
- package/src/shared/lib/omp/config/row-value.test.ts +95 -0
- package/src/shared/lib/omp/config/row-value.ts +97 -0
- package/src/shared/lib/omp/config/schema.test.ts +31 -0
- package/src/shared/lib/omp/config/schema.ts +6 -0
- package/src/shared/lib/omp/rpc/frame.test.ts +48 -47
- package/src/shared/lib/omp/rpc/frame.ts +49 -40
- package/src/shared/lib/ui/menu-anchor.test.ts +69 -0
- package/src/shared/lib/ui/menu-anchor.ts +89 -0
- package/src/shared/lib/usage/breakdown.test.ts +197 -0
- package/src/shared/lib/usage/breakdown.ts +241 -0
- package/src/shared/lib/usage/range.test.ts +204 -0
- package/src/shared/lib/usage/range.ts +240 -0
- package/src/shared/lib/usage/view.test.ts +85 -0
- package/src/shared/lib/usage/view.ts +74 -0
- package/src/shared/types/browser.ts +78 -0
- package/src/shared/types/index.ts +1 -0
- package/src/shared/types/settings/agent.ts +28 -6
- package/src/shared/types/settings/state.ts +4 -3
- package/src/shared/types/settings/write.ts +6 -0
- package/src/shared/types/tokenUsage.ts +128 -21
- package/src/shared/types/updates.ts +20 -0
- package/dist/client/static/js/index-3ve31rzx.css +0 -1
- package/src/server/lib/omp/session/usage/aggregate-shaping.test.ts +0 -149
- package/src/server/lib/omp/session/usage/aggregate.test.ts +0 -243
- package/src/server/lib/omp/session/usage/aggregate.ts +0 -330
- package/src/server/lib/omp/session/usage/shaping.ts +0 -130
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The block map: what a rendered document says its blocks ARE.
|
|
8
|
+
*
|
|
9
|
+
* The properties here are the ones the review feature rests on, and each was
|
|
10
|
+
* measured before it was written down:
|
|
11
|
+
*
|
|
12
|
+
* - `raw` partitions the source exactly, so accumulating line counts names the
|
|
13
|
+
* real line of every block (a leading blank line, front-matter, a document
|
|
14
|
+
* with no trailing newline and one with `$$…$$` were all checked).
|
|
15
|
+
* - The identity is content-derived, so inserting a paragraph above every block
|
|
16
|
+
* leaves every key untouched, and editing a block's own text is the ONLY thing
|
|
17
|
+
* that changes one.
|
|
18
|
+
* - The mapped render adds attributes and nothing else: strip them and the HTML
|
|
19
|
+
* is byte-identical to `renderMarkdown`, including the copy button, the table
|
|
20
|
+
* scroller and the directive/mermaid placeholders.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { describe, expect, test } from 'bun:test';
|
|
24
|
+
|
|
25
|
+
import { renderMarkdown, renderMappedMarkdown } from '@/shared/lib/markdown/marked';
|
|
26
|
+
import { blockKeyOf, hashBlockText, normalizeBlockText, quoteOf } from '@/shared/lib/markdown/blocks/anchor';
|
|
27
|
+
|
|
28
|
+
const stripAttributes = (html: string): string => html.replace(/ data-md-(?:block|start|end)="[^"]*"/g, '');
|
|
29
|
+
|
|
30
|
+
const CORPUS = [
|
|
31
|
+
'# Heading',
|
|
32
|
+
'',
|
|
33
|
+
'Paragraph with **bold**, `code` and a [link](a.md).',
|
|
34
|
+
'',
|
|
35
|
+
'> quote line',
|
|
36
|
+
'> second',
|
|
37
|
+
'',
|
|
38
|
+
'- one',
|
|
39
|
+
'- two',
|
|
40
|
+
'',
|
|
41
|
+
'```ts',
|
|
42
|
+
'const a = 1;',
|
|
43
|
+
'```',
|
|
44
|
+
'',
|
|
45
|
+
'| a | b |',
|
|
46
|
+
'| - | - |',
|
|
47
|
+
'| 1 | 2 |',
|
|
48
|
+
'',
|
|
49
|
+
'---',
|
|
50
|
+
'',
|
|
51
|
+
'$$y = 1$$',
|
|
52
|
+
'',
|
|
53
|
+
'End.',
|
|
54
|
+
].join('\n');
|
|
55
|
+
|
|
56
|
+
describe('the block map', () => {
|
|
57
|
+
test('every top-level block carries its identity and its source lines', () => {
|
|
58
|
+
const { blocks } = renderMappedMarkdown(CORPUS, { allowHtml: true });
|
|
59
|
+
expect(blocks.map((block) => block.type)).toEqual([
|
|
60
|
+
'heading',
|
|
61
|
+
'paragraph',
|
|
62
|
+
'blockquote',
|
|
63
|
+
'list',
|
|
64
|
+
'code',
|
|
65
|
+
'table',
|
|
66
|
+
'hr',
|
|
67
|
+
'blockMathDisplay',
|
|
68
|
+
'paragraph',
|
|
69
|
+
]);
|
|
70
|
+
// The heading is line 1; the paragraph that follows the blank line is line 3.
|
|
71
|
+
expect(blocks[0]).toMatchObject({ lineStart: 1, lineEnd: 1 });
|
|
72
|
+
expect(blocks[1]).toMatchObject({ lineStart: 3, lineEnd: 3 });
|
|
73
|
+
// A fenced block spans its fence and body, and its trailing newline is the
|
|
74
|
+
// separator rather than a line of its own.
|
|
75
|
+
expect(blocks[4]).toMatchObject({ lineStart: 11, lineEnd: 13 });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('a block map that misses a rendered block would be a silent hole', () => {
|
|
79
|
+
const { html, blocks } = renderMappedMarkdown(CORPUS, { allowHtml: true });
|
|
80
|
+
// Every block the map names must exist in the DOM, and vice versa. This is
|
|
81
|
+
// the test that fails when a new block kind (or a `marked` rename) slips past
|
|
82
|
+
// `BLOCK_RENDERERS`.
|
|
83
|
+
const rendered = (html.match(/data-md-block=/g) ?? []).length;
|
|
84
|
+
expect(rendered).toBe(blocks.length);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('the mapped render changes nothing but the attributes', () => {
|
|
88
|
+
const plain = renderMarkdown(CORPUS, { allowHtml: true });
|
|
89
|
+
const mapped = renderMappedMarkdown(CORPUS, { allowHtml: true });
|
|
90
|
+
expect(stripAttributes(mapped.html)).toBe(plain);
|
|
91
|
+
// The overrides `marked.ts` installs must survive the mapper: it captures the
|
|
92
|
+
// renderer table at registration, so wrapping too early would drop them.
|
|
93
|
+
expect(mapped.html).toContain('code-copy-float');
|
|
94
|
+
expect(mapped.html).toContain('md-table-scroll');
|
|
95
|
+
expect(mapped.html).toContain('math-pending');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('the chat path stays byte-identical while the mapper is registered', () => {
|
|
99
|
+
const first = renderMarkdown('# T\n\npara\n');
|
|
100
|
+
const again = renderMarkdown('# T\n\npara\n');
|
|
101
|
+
expect(again).toBe(first);
|
|
102
|
+
expect(first).not.toContain('data-md-block');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('block identity', () => {
|
|
107
|
+
test('an edit above a block does not change its key; its own text does', () => {
|
|
108
|
+
const before = renderMappedMarkdown('# A\n\nfirst\n\nsecond\n', { allowHtml: true }).blocks;
|
|
109
|
+
const after = renderMappedMarkdown('# A\n\ninserted\n\nfirst\n\nsecond\n', { allowHtml: true }).blocks;
|
|
110
|
+
const keysBefore = new Set(before.map((block) => block.key));
|
|
111
|
+
// Every original block is still matched by its own key, except the one whose
|
|
112
|
+
// position the new paragraph took: its text is the same, so its key is the
|
|
113
|
+
// same too. Insertion is not an edit.
|
|
114
|
+
for (const key of keysBefore) expect(after.some((block) => block.key === key)).toBe(true);
|
|
115
|
+
|
|
116
|
+
const edited = renderMappedMarkdown('# A\n\nfirst\n\nsecond changed\n', { allowHtml: true }).blocks;
|
|
117
|
+
const lost = before.filter((block) => !edited.some((next) => next.key === block.key));
|
|
118
|
+
expect(lost.map((block) => block.quote)).toEqual(['second']);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('a reflow is not an edit, because whitespace is normalized', () => {
|
|
122
|
+
const one = renderMappedMarkdown('a single line\n', { allowHtml: true }).blocks[0];
|
|
123
|
+
const two = renderMappedMarkdown('a single\nline\n', { allowHtml: true }).blocks[0];
|
|
124
|
+
expect(two.key).toBe(one.key);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('two identical blocks are told apart by their ordinal', () => {
|
|
128
|
+
const seen = new Map<string, number>();
|
|
129
|
+
expect(blockKeyOf('paragraph', 'same', seen)).toBe('paragraph:' + hashBlockText('same') + '#1');
|
|
130
|
+
expect(blockKeyOf('paragraph', 'same', seen)).toBe('paragraph:' + hashBlockText('same') + '#2');
|
|
131
|
+
// A different TYPE with the same text is a different block.
|
|
132
|
+
expect(blockKeyOf('heading', 'same', seen).endsWith('#1')).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('the quote is one line and bounded', () => {
|
|
136
|
+
expect(normalizeBlockText(' a\n\n b ')).toBe('a b');
|
|
137
|
+
const long = quoteOf('x'.repeat(400));
|
|
138
|
+
expect(long.length).toBe(160);
|
|
139
|
+
expect(long.endsWith('…')).toBe(true);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Put the block identity on the rendered markup, and nowhere else.
|
|
8
|
+
*
|
|
9
|
+
* The attributes go on the OUTERMOST element of each block, which is what makes
|
|
10
|
+
* this compose with the wrappers `marked.ts` already emits for code blocks,
|
|
11
|
+
* tables and math placeholders: the hydrators (`hydrateMermaidBlocks`,
|
|
12
|
+
* `hydrateMathBlocks`, `hydrateDirectiveBlocks`) rewrite grandchildren, so a
|
|
13
|
+
* reviewer's attributes on the wrapper survive every hydrate and every theme
|
|
14
|
+
* repaint.
|
|
15
|
+
*
|
|
16
|
+
* ## Registration order is part of the contract
|
|
17
|
+
*
|
|
18
|
+
* `registerBlockMapper` captures the renderer as it stands. It MUST run after
|
|
19
|
+
* every `marked.use({ renderer })` in `marked.ts`, or it captures the
|
|
20
|
+
* un-overridden `code`/`table` and every previewed document silently loses its
|
|
21
|
+
* copy buttons and its table scroller. The capture is also what keeps the
|
|
22
|
+
* chat path byte-identical: a token with no tag renders exactly as before.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { Token } from 'marked';
|
|
26
|
+
|
|
27
|
+
import { BLOCK_ATTRIBUTE, type BlockToken } from '@/shared/lib/markdown/blocks/scan';
|
|
28
|
+
import type { MdBlock } from '@/shared/lib/markdown/blocks/types';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The block-level renderers that own a top-level token.
|
|
32
|
+
*
|
|
33
|
+
* A name missing here is a block nobody can annotate, so `registerBlockMapper`
|
|
34
|
+
* refuses to register instead of skipping it: a `marked` upgrade that renames
|
|
35
|
+
* one must fail loudly.
|
|
36
|
+
*/
|
|
37
|
+
export const BLOCK_RENDERERS = [
|
|
38
|
+
'heading',
|
|
39
|
+
'paragraph',
|
|
40
|
+
'blockquote',
|
|
41
|
+
'list',
|
|
42
|
+
'code',
|
|
43
|
+
'table',
|
|
44
|
+
'html',
|
|
45
|
+
'hr',
|
|
46
|
+
] as const;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The BLOCK-level math extension renderers.
|
|
50
|
+
*
|
|
51
|
+
* `$$…$$` and `\[…\]` are blocks of the document; `$…$` and `\(…\)` are
|
|
52
|
+
* inline and live inside a block that is already tagged. They are in their own
|
|
53
|
+
* table because that is where marked keeps them — and because the core table
|
|
54
|
+
* cannot name them at all (measured: registering `blockMathDisplay` through
|
|
55
|
+
* `marked.use({ renderer })` throws "renderer does not exist").
|
|
56
|
+
*/
|
|
57
|
+
export const EXTENSION_BLOCK_RENDERERS = ['blockMath', 'blockMathDisplay'] as const;
|
|
58
|
+
|
|
59
|
+
/** The first start tag of a rendered block, after any leading whitespace. */
|
|
60
|
+
const LEADING_TAG = /^(\s*)<([a-zA-Z][\w-]*)/;
|
|
61
|
+
|
|
62
|
+
/** The identity attributes, or the html untouched when the token has no block. */
|
|
63
|
+
export function annotateBlockHtml(html: string, token: unknown): string {
|
|
64
|
+
const block = (token as BlockToken | undefined)?.[BLOCK_ATTRIBUTE] as MdBlock | undefined;
|
|
65
|
+
if (!block) return html;
|
|
66
|
+
const attributes = `data-md-block="${block.key}" data-md-start="${block.lineStart}" data-md-end="${block.lineEnd}"`;
|
|
67
|
+
return html.replace(LEADING_TAG, `$1<$2 ${attributes}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* What `registerBlockMapper` needs of the `Marked` instance.
|
|
72
|
+
*
|
|
73
|
+
* Deliberately structural and loosely typed: `marked`'s own `defaults.renderer`
|
|
74
|
+
* is `_Renderer | null | undefined`, and the extension table is untyped in the
|
|
75
|
+
* public surface. Every slot is checked at runtime before it is wrapped, so the
|
|
76
|
+
* cast is the honest place to put the looseness — one module, not a `any` at
|
|
77
|
+
* each call site.
|
|
78
|
+
*/
|
|
79
|
+
interface MapperHost {
|
|
80
|
+
defaults: {
|
|
81
|
+
renderer?: unknown;
|
|
82
|
+
extensions?: { renderers?: unknown } | null;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** One renderer slot, already bound to the object it was read from. */
|
|
87
|
+
type RendererSlot = (...args: unknown[]) => string;
|
|
88
|
+
|
|
89
|
+
/** Replace a renderer slot with one that prints the block identity. */
|
|
90
|
+
function wrap(owner: Record<string, unknown>, name: string): void {
|
|
91
|
+
const original = owner[name];
|
|
92
|
+
if (typeof original !== 'function') {
|
|
93
|
+
throw new Error(`marked renderer "${name}" is missing — the block map cannot be built`);
|
|
94
|
+
}
|
|
95
|
+
owner[name] = function blockMapper(this: unknown, token: Token, ...rest: unknown[]): string {
|
|
96
|
+
const html = (original as RendererSlot).call(this, token, ...rest);
|
|
97
|
+
return annotateBlockHtml(html, token);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Wrap every block renderer so a tagged token prints its identity.
|
|
103
|
+
*
|
|
104
|
+
* Mutates the instance's renderer table rather than calling `marked.use`: the
|
|
105
|
+
* options form of `marked.parser` bypasses the extension renderers entirely
|
|
106
|
+
* (measured — it throws on `$x^2$`), so the only safe place for this wrapper is
|
|
107
|
+
* the instance's own chain.
|
|
108
|
+
*/
|
|
109
|
+
export function registerBlockMapper(instance: MapperHost): void {
|
|
110
|
+
const renderer = instance.defaults.renderer as Record<string, unknown> | null | undefined;
|
|
111
|
+
if (!renderer) throw new Error('marked has no renderer table — the block map cannot be built');
|
|
112
|
+
for (const name of BLOCK_RENDERERS) wrap(renderer, name);
|
|
113
|
+
|
|
114
|
+
const extensions = instance.defaults.extensions?.renderers as Record<string, unknown> | null | undefined;
|
|
115
|
+
for (const name of EXTENSION_BLOCK_RENDERERS) {
|
|
116
|
+
// Absent means this instance has no math extensions; skipping is correct
|
|
117
|
+
// there, and the block-map test fails loudly if the names ever change.
|
|
118
|
+
if (extensions && typeof extensions[name] === 'function') wrap(extensions, name);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Turn a markdown source into the blocks a rendered preview will contain, and
|
|
8
|
+
* tag each token so the renderer can put that identity on the markup.
|
|
9
|
+
*
|
|
10
|
+
* The line ranges come from `raw`, and that is exact rather than approximate:
|
|
11
|
+
* `marked`'s top-level tokens partition the source verbatim, so accumulating
|
|
12
|
+
* `raw.split('\n').length - 1` per token — including the `space` tokens — names
|
|
13
|
+
* the real line every block starts on (measured on this repo's own documents;
|
|
14
|
+
* see the block-map tests).
|
|
15
|
+
*
|
|
16
|
+
* ## The tokens are returned because they ARE the render input
|
|
17
|
+
*
|
|
18
|
+
* Annotating the token objects and then calling `marked.parse(source)` does
|
|
19
|
+
* nothing: `parse` lexes again, and the fresh tokens carry no tag. The only
|
|
20
|
+
* shape that works is one lexer pass, then `parser(tokens)` on those objects.
|
|
21
|
+
*
|
|
22
|
+
* ## `space` and `def` are not blocks
|
|
23
|
+
*
|
|
24
|
+
* `space` is the gap between blocks (it carries lines, never content), and a
|
|
25
|
+
* definition token renders nothing at all. Both are walked for their lines and
|
|
26
|
+
* dropped from the result, so a review can never be attached to whitespace.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { Token } from 'marked';
|
|
30
|
+
|
|
31
|
+
import { blockKeyOf, quoteOf } from '@/shared/lib/markdown/blocks/anchor';
|
|
32
|
+
import type { MdBlock } from '@/shared/lib/markdown/blocks/types';
|
|
33
|
+
|
|
34
|
+
/** Where the map is written onto the token, read by `renderer.ts`. */
|
|
35
|
+
export const BLOCK_ATTRIBUTE = '__mdBlock';
|
|
36
|
+
|
|
37
|
+
/** A token carrying its block, once `mapBlocks` has run. */
|
|
38
|
+
export type BlockToken = Token & { [BLOCK_ATTRIBUTE]?: MdBlock };
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The lexer surface `mapBlocks` needs — the `Marked` instance satisfies it.
|
|
42
|
+
*
|
|
43
|
+
* Called as a METHOD, never destructured: `marked.lexer` reads `this.defaults`,
|
|
44
|
+
* so `const { lexer } = marked; lexer(source)` throws `this.defaults` undefined.
|
|
45
|
+
*/
|
|
46
|
+
export interface BlockLexer {
|
|
47
|
+
lexer(source: string): Token[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface BlockScan {
|
|
51
|
+
/** The same tokens, each block-level one tagged with its {@link MdBlock}. */
|
|
52
|
+
tokens: Token[];
|
|
53
|
+
blocks: MdBlock[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Token types that occupy lines but render no block of their own. */
|
|
57
|
+
const NOT_A_BLOCK: Record<string, true> = { space: true, def: true };
|
|
58
|
+
|
|
59
|
+
function linesIn(raw: string): number {
|
|
60
|
+
return raw.split('\n').length - 1;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Lex `source` and tag every block token with its key and line range.
|
|
65
|
+
*
|
|
66
|
+
* The returned tokens MUST be handed to `marked.parser`. Rendering the source
|
|
67
|
+
* again would lex a second time and lose every tag.
|
|
68
|
+
*/
|
|
69
|
+
export function mapBlocks(instance: BlockLexer, source: string): BlockScan {
|
|
70
|
+
const tokens = instance.lexer(source);
|
|
71
|
+
const blocks: MdBlock[] = [];
|
|
72
|
+
const seen = new Map<string, number>();
|
|
73
|
+
let line = 1;
|
|
74
|
+
|
|
75
|
+
for (const token of tokens) {
|
|
76
|
+
const raw = typeof token.raw === 'string' ? token.raw : '';
|
|
77
|
+
if (NOT_A_BLOCK[token.type] === true) {
|
|
78
|
+
line += linesIn(raw);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
// A trailing newline is the SEPARATOR between blocks, not part of the last
|
|
82
|
+
// line of this one — counting it would report a block as one line longer
|
|
83
|
+
// than the text it renders.
|
|
84
|
+
const body = raw.replace(/\n+$/, '');
|
|
85
|
+
const lineStart = line;
|
|
86
|
+
const lineEnd = lineStart + linesIn(body);
|
|
87
|
+
const block: MdBlock = {
|
|
88
|
+
key: blockKeyOf(token.type, raw, seen),
|
|
89
|
+
type: token.type,
|
|
90
|
+
lineStart,
|
|
91
|
+
lineEnd,
|
|
92
|
+
quote: quoteOf(body),
|
|
93
|
+
};
|
|
94
|
+
blocks.push(block);
|
|
95
|
+
(token as BlockToken)[BLOCK_ATTRIBUTE] = block;
|
|
96
|
+
line += linesIn(raw);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return { tokens, blocks };
|
|
100
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One renderable block of a markdown document, as the RENDER produced it.
|
|
8
|
+
*
|
|
9
|
+
* The rendered preview has no lines and no identity: it is one HTML string.
|
|
10
|
+
* A reviewer, though, points at a paragraph — so every top-level block carries
|
|
11
|
+
* a key derived from its own text, and the source line range it came from.
|
|
12
|
+
*
|
|
13
|
+
* The key is content-derived on purpose: a line number would re-attach a note
|
|
14
|
+
* to whatever moved into that line after the next edit, while an anchor that
|
|
15
|
+
* names the block follows the block when text above it is inserted, deleted or
|
|
16
|
+
* reflowed (see `anchor.ts`).
|
|
17
|
+
*/
|
|
18
|
+
export interface MdBlock {
|
|
19
|
+
/** `type:hash#occurrence` — stable across edits above the block. */
|
|
20
|
+
key: string;
|
|
21
|
+
/** The marked token type (`heading`, `paragraph`, `code`, …). */
|
|
22
|
+
type: string;
|
|
23
|
+
/** 1-based first source line of the block. Display data, never identity. */
|
|
24
|
+
lineStart: number;
|
|
25
|
+
/** 1-based last source line of the block. */
|
|
26
|
+
lineEnd: number;
|
|
27
|
+
/** The block's text, whitespace-collapsed, truncated for display and prompt. */
|
|
28
|
+
quote: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A judgement on one block. Two answers, because a review is a decision. */
|
|
32
|
+
export type BlockVerdict = 'good' | 'needs-work';
|
|
33
|
+
|
|
34
|
+
/** A stored judgement, as the server holds it and the preview paints it. */
|
|
35
|
+
export interface DocumentAnnotation {
|
|
36
|
+
id: string;
|
|
37
|
+
/** `<resolvedRoot>\0<repo>` — what makes two workspaces two documents. */
|
|
38
|
+
scope: string;
|
|
39
|
+
/** Workspace-relative path of the document. */
|
|
40
|
+
path: string;
|
|
41
|
+
/** The block this judgement belongs to (see {@link MdBlock.key}). */
|
|
42
|
+
blockKey: string;
|
|
43
|
+
/** The block's line range when it was judged; updated on re-save. */
|
|
44
|
+
lineStart: number;
|
|
45
|
+
lineEnd: number;
|
|
46
|
+
verdict: BlockVerdict;
|
|
47
|
+
note: string;
|
|
48
|
+
/** The block's text when it was judged — what an outdated mark still shows. */
|
|
49
|
+
quote: string;
|
|
50
|
+
createdAt: number;
|
|
51
|
+
updatedAt: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What a save sends: everything but the id and the timestamps. */
|
|
55
|
+
export type AnnotationDraft = Omit<DocumentAnnotation, 'id' | 'createdAt' | 'updatedAt'>;
|
|
56
|
+
|
|
57
|
+
/** The document a review belongs to, as the client asks for it. */
|
|
58
|
+
export interface ReviewTarget {
|
|
59
|
+
path: string;
|
|
60
|
+
root?: string | null;
|
|
61
|
+
repo?: string | null;
|
|
62
|
+
}
|
|
@@ -32,7 +32,10 @@ import { codeSafeAutolink } from '@/shared/lib/markdown/autolink';
|
|
|
32
32
|
import remend from 'remend';
|
|
33
33
|
import { highlightCode } from '@/shared/lib/code/syntax-highlight';
|
|
34
34
|
import { directiveBlockHtml, isDirectiveLanguage, parseDirectiveAttributes } from '@/shared/lib/markdown/directives';
|
|
35
|
-
import {
|
|
35
|
+
import { mathExtensions, setProseEscaped } from '@/shared/lib/markdown/math-extensions';
|
|
36
|
+
import { registerBlockMapper } from '@/shared/lib/markdown/blocks/renderer';
|
|
37
|
+
import { mapBlocks } from '@/shared/lib/markdown/blocks/scan';
|
|
38
|
+
import type { MdBlock } from '@/shared/lib/markdown/blocks/types';
|
|
36
39
|
|
|
37
40
|
/** Escape HTML for safe interpolation inside highlighted output. */
|
|
38
41
|
function escapeHtml(value: string): string {
|
|
@@ -44,56 +47,6 @@ function escapeHtml(value: string): string {
|
|
|
44
47
|
.replace(/'/g, ''');
|
|
45
48
|
}
|
|
46
49
|
|
|
47
|
-
/**
|
|
48
|
-
* True while `renderMarkdown` parses prose it escaped itself. Set around the
|
|
49
|
-
* `marked.parse` call, read by `renderMathPlaceholder`. Synchronous by
|
|
50
|
-
* construction: `marked.parse` never awaits, so no nested `renderMarkdown`
|
|
51
|
-
* can observe the flag mid-parse.
|
|
52
|
-
*/
|
|
53
|
-
let proseEscaped = false;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Emit a math placeholder instead of rendering KaTeX inline.
|
|
57
|
-
*
|
|
58
|
-
* `katex` is ~522 kB of JS worth loading only when a message contains math, so
|
|
59
|
-
* it is not imported here. The placeholder carries the TeX source and the
|
|
60
|
-
* display flag; `katex.ts` swaps it for real markup once the library lands
|
|
61
|
-
* (see `hydrateMathBlocks`).
|
|
62
|
-
*
|
|
63
|
-
* Two things happen to the captured source here, and both exist because this
|
|
64
|
-
* renderer is the one place all four math extensions funnel through:
|
|
65
|
-
*
|
|
66
|
-
* 1. **Entities are reversed** when `renderMarkdown` escaped the prose itself
|
|
67
|
-
* (`proseEscaped`). `escapeHtmlOutsideCode` rewrites the source BEFORE
|
|
68
|
-
* marked sees it, so a tokenizer captures `x < y` rather than `x < y` and
|
|
69
|
-
* KaTeX reports a syntax error for a perfectly good formula. Only the three
|
|
70
|
-
* entities that escaper emits are reversed, and only when it ran — a
|
|
71
|
-
* document (`allowHtml`) whose own text legitimately holds `&` is left
|
|
72
|
-
* alone.
|
|
73
|
-
* 2. **The result is percent-encoded**, the way a mermaid block carries its
|
|
74
|
-
* source. HTML-escaping the payload instead re-escapes what step 1 just
|
|
75
|
-
* restored, and the attribute round-trip hides the extra layer from both
|
|
76
|
-
* ends. An encoded payload holds no HTML-special character, so nothing
|
|
77
|
-
* between here and `renderPlaceholder` can alter it.
|
|
78
|
-
*/
|
|
79
|
-
function renderMathPlaceholder(tex: string, displayMode: boolean): string {
|
|
80
|
-
const restored = proseEscaped
|
|
81
|
-
? tex.replace(/&(amp|lt|gt);/g, (_, name: string) => (name === 'amp' ? '&' : name === 'lt' ? '<' : '>'))
|
|
82
|
-
: tex;
|
|
83
|
-
const display = displayMode ? ' data-math-display="1"' : '';
|
|
84
|
-
const span = `<span class="${MATH_PENDING_CLASS}" data-math="${encodeURIComponent(restored)}"${display}></span>`;
|
|
85
|
-
// Block math must stay inside a block element: DOMPurify drops a bare
|
|
86
|
-
// `<span>` that sits at the top level of the fragment, which silently deleted
|
|
87
|
-
// every `$$…$$` formula. KaTeX's own output is inline-level too, so the old
|
|
88
|
-
// synchronous renderer relied on the same wrapper being present.
|
|
89
|
-
return displayMode ? `<p>${span}</p>` : span;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
interface TokenLike {
|
|
93
|
-
type: string;
|
|
94
|
-
text?: string;
|
|
95
|
-
raw?: string;
|
|
96
|
-
}
|
|
97
50
|
|
|
98
51
|
const marked = new Marked({
|
|
99
52
|
gfm: true,
|
|
@@ -190,97 +143,15 @@ marked.use({
|
|
|
190
143
|
},
|
|
191
144
|
});
|
|
192
145
|
|
|
193
|
-
marked.use({
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
tokenizer(src: string) {
|
|
203
|
-
const match = /^\\\(([\s\S]+?)\\\)/.exec(src);
|
|
204
|
-
return match
|
|
205
|
-
? { type: 'inlineMath', raw: match[0], text: match[1] }
|
|
206
|
-
: undefined;
|
|
207
|
-
},
|
|
208
|
-
renderer(token: TokenLike) {
|
|
209
|
-
return renderMathPlaceholder(token.text ?? '', false);
|
|
210
|
-
},
|
|
211
|
-
},
|
|
212
|
-
// \[ ... \] block math
|
|
213
|
-
{
|
|
214
|
-
name: 'blockMath',
|
|
215
|
-
level: 'block',
|
|
216
|
-
start(src: string) {
|
|
217
|
-
return src.match(/^\\\[/)?.index;
|
|
218
|
-
},
|
|
219
|
-
tokenizer(src: string) {
|
|
220
|
-
const match = /^\\\[([\s\S]+?)\\\]/.exec(src);
|
|
221
|
-
return match
|
|
222
|
-
? { type: 'blockMath', raw: match[0], text: match[1] }
|
|
223
|
-
: undefined;
|
|
224
|
-
},
|
|
225
|
-
renderer(token: TokenLike) {
|
|
226
|
-
return renderMathPlaceholder(token.text ?? '', true);
|
|
227
|
-
},
|
|
228
|
-
},
|
|
229
|
-
// $$ ... $$ display math (block-level, must be its own line)
|
|
230
|
-
{
|
|
231
|
-
name: 'blockMathDisplay',
|
|
232
|
-
level: 'block',
|
|
233
|
-
start(src: string) {
|
|
234
|
-
return src.match(/^\$\$/)?.index;
|
|
235
|
-
},
|
|
236
|
-
tokenizer(src: string) {
|
|
237
|
-
const match = /^\$\$([\s\S]+?)\$\$(?:\n|$)/.exec(src);
|
|
238
|
-
return match
|
|
239
|
-
? { type: 'blockMathDisplay', raw: match[0], text: match[1] }
|
|
240
|
-
: undefined;
|
|
241
|
-
},
|
|
242
|
-
renderer(token: TokenLike) {
|
|
243
|
-
return renderMathPlaceholder(token.text ?? '', true);
|
|
244
|
-
},
|
|
245
|
-
},
|
|
246
|
-
// $ ... $ inline math. The flanking rules are the whole defence: a single
|
|
247
|
-
// `$` is the one delimiter that collides with ordinary text, and this
|
|
248
|
-
// pipeline renders shell output, currency, SQL and awk programs all day.
|
|
249
|
-
//
|
|
250
|
-
// opening $ — not preceded by a word char (checked in `start`, the only
|
|
251
|
-
// place the preceding character is visible), and not
|
|
252
|
-
// followed by a digit, `$` or `{`. The digit guard is what
|
|
253
|
-
// makes `$5`, `$1` and `$10` text; `{` keeps `${HOME}` out.
|
|
254
|
-
// closing $ — not preceded by whitespace, and not followed by a WORD
|
|
255
|
-
// char or `$`. That trailing guard is load-bearing twice
|
|
256
|
-
// over: it rejects `PATH=$PATH:$HOME` (closer followed by
|
|
257
|
-
// `H`) and `a$b$c` (followed by `c`), and it is the reason
|
|
258
|
-
// the rule no longer depends on where `start` happened to
|
|
259
|
-
// land — the tokenizer is anchored at `^` and cannot see
|
|
260
|
-
// the preceding character, so `a$b$c` was math while
|
|
261
|
-
// `ax$b$c` was text on the identical rule.
|
|
262
|
-
//
|
|
263
|
-
// `$n$ = 3` and `$x^2$ benar` still parse: a space after the closer is not
|
|
264
|
-
// a word char.
|
|
265
|
-
{
|
|
266
|
-
name: 'inlineMathDollar',
|
|
267
|
-
level: 'inline',
|
|
268
|
-
start(src: string) {
|
|
269
|
-
const match = /(^|[^\w$])\$(?!\$)(?!\s)/.exec(src);
|
|
270
|
-
return match ? match.index + (match[1]?.length ?? 0) : undefined;
|
|
271
|
-
},
|
|
272
|
-
tokenizer(src: string) {
|
|
273
|
-
const match = /^\$(?![\d${])((?:\\\$|[^$])*?)(?<!\s)\$(?![\w$])/.exec(src);
|
|
274
|
-
return match
|
|
275
|
-
? { type: 'inlineMathDollar', raw: match[0], text: match[1] }
|
|
276
|
-
: undefined;
|
|
277
|
-
},
|
|
278
|
-
renderer(token: TokenLike) {
|
|
279
|
-
return renderMathPlaceholder(token.text ?? '', false);
|
|
280
|
-
},
|
|
281
|
-
},
|
|
282
|
-
],
|
|
283
|
-
});
|
|
146
|
+
marked.use({ extensions: mathExtensions() });
|
|
147
|
+
|
|
148
|
+
// LAST, and the order is the contract: the mapper captures these renderers as
|
|
149
|
+
// they stand, so registering it before the overrides above would capture the
|
|
150
|
+
// un-wrapped `code`/`table` and every previewed document would lose its copy
|
|
151
|
+
// buttons and its table scroller. A math block's token is not in this table at
|
|
152
|
+
// all (it belongs to the extension), which is why the mapper also wraps
|
|
153
|
+
// `defaults.extensions.renderers` — see `blocks/renderer.ts`.
|
|
154
|
+
registerBlockMapper(marked);
|
|
284
155
|
|
|
285
156
|
/** Escape HTML special chars in text, leaving backtick-delimited code intact
|
|
286
157
|
* (inline `code` and fenced ``` blocks) so code like `a < b` is not mangled
|
|
@@ -313,23 +184,63 @@ export interface RenderMarkdownOptions {
|
|
|
313
184
|
allowHtml?: boolean;
|
|
314
185
|
}
|
|
315
186
|
|
|
187
|
+
/**
|
|
188
|
+
* The healed source both entry points parse — one spelling of the pipeline, so
|
|
189
|
+
* the preview and the mapped preview can never disagree about what a document
|
|
190
|
+
* says.
|
|
191
|
+
*/
|
|
192
|
+
function heal(markdown: string, allowHtml: boolean): string {
|
|
193
|
+
const source = allowHtml ? markdown : escapeHtmlOutsideCode(markdown);
|
|
194
|
+
return remend(source, { katex: true, inlineKatex: false })
|
|
195
|
+
.replace(INCOMPLETE_LINK_RE, (_, text: string) => `<span class="md-incomplete-link">${text}</span>`);
|
|
196
|
+
}
|
|
197
|
+
|
|
316
198
|
/**
|
|
317
199
|
* Heal streaming markdown (remend) then parse to HTML via marked.
|
|
318
200
|
* Synchronous and side-effect free — safe in both server loaders and the
|
|
319
201
|
* browser. The output must be sanitized with DOMPurify before injection.
|
|
320
202
|
*/
|
|
321
203
|
export function renderMarkdown(markdown: string, options: RenderMarkdownOptions = {}): string {
|
|
322
|
-
const
|
|
323
|
-
const healed = remend(source, { katex: true, inlineKatex: false })
|
|
324
|
-
.replace(INCOMPLETE_LINK_RE, (_, text: string) => `<span class="md-incomplete-link">${text}</span>`);
|
|
204
|
+
const healed = heal(markdown, options.allowHtml === true);
|
|
325
205
|
// The math renderers need to know the prose was escaped, so they can hand
|
|
326
206
|
// KaTeX the source the author wrote instead of `x < y`. `marked.parse`
|
|
327
207
|
// never awaits, so no nested render can observe this mid-parse.
|
|
328
|
-
|
|
208
|
+
setProseEscaped(options.allowHtml !== true);
|
|
329
209
|
try {
|
|
330
210
|
return (marked.parse(healed) as string).trim();
|
|
331
211
|
} finally {
|
|
332
|
-
|
|
212
|
+
setProseEscaped(false);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The mapped render: the same HTML, plus the block each element came from. */
|
|
217
|
+
export interface MappedMarkdown {
|
|
218
|
+
html: string;
|
|
219
|
+
blocks: MdBlock[];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Render a document AND say which source block each rendered element is.
|
|
224
|
+
*
|
|
225
|
+
* The preview needs the second half: a reviewer points at a paragraph, and a
|
|
226
|
+
* line number cannot express that (the render has no lines). The identity rides
|
|
227
|
+
* on `data-md-block`, a content-derived anchor, so it survives an edit above the
|
|
228
|
+
* block — see `blocks/anchor.ts`.
|
|
229
|
+
*
|
|
230
|
+
* Deliberately NOT `marked.parse(healed)`: that lexes a second time and the
|
|
231
|
+
* fresh tokens carry no tag. One lexer pass, then `parser(tokens)` on those
|
|
232
|
+
* objects. The attributes are a strict no-op for an untagged token, so the
|
|
233
|
+
* chat path (`renderMarkdown`) stays byte-identical.
|
|
234
|
+
*/
|
|
235
|
+
export function renderMappedMarkdown(markdown: string, options: RenderMarkdownOptions = {}): MappedMarkdown {
|
|
236
|
+
const healed = heal(markdown, options.allowHtml === true);
|
|
237
|
+
const { tokens, blocks } = mapBlocks(marked, healed);
|
|
238
|
+
setProseEscaped(options.allowHtml !== true);
|
|
239
|
+
try {
|
|
240
|
+
const html = (marked.parser(tokens) as string).trim();
|
|
241
|
+
return { html, blocks };
|
|
242
|
+
} finally {
|
|
243
|
+
setProseEscaped(false);
|
|
333
244
|
}
|
|
334
245
|
}
|
|
335
246
|
|