@peaske7/readit 0.1.8 → 0.2.1
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/.claude/CLAUDE.md +118 -76
- package/.claude/commands/review.md +1 -1
- package/.claude/roadmap.md +32 -9
- package/.claude/user-stories.md +100 -15
- package/AGENTS.md +30 -26
- package/Makefile +32 -0
- package/README.md +90 -5
- package/biome.json +18 -8
- package/bun.lock +426 -710
- package/bunfig.toml +2 -0
- package/docs/perf-baseline.md +130 -0
- package/docs/superpowers/plans/2026-03-26-surgical-pruning.md +1176 -0
- package/docs/superpowers/specs/2026-03-27-go-server-rewrite-design.md +284 -0
- package/e2e/comments.spec.ts +14 -58
- package/e2e/document-load.spec.ts +1 -23
- package/e2e/export.spec.ts +4 -4
- package/e2e/perf/add-comment.spec.ts +116 -0
- package/e2e/perf/fixtures/generate.ts +327 -0
- package/e2e/perf/initial-load.spec.ts +49 -0
- package/e2e/perf/perf.setup.ts +23 -0
- package/e2e/perf/perf.teardown.ts +9 -0
- package/e2e/perf/screenshot-final.png +0 -0
- package/e2e/perf/scroll.spec.ts +39 -0
- package/e2e/perf/tab-switch.spec.ts +69 -0
- package/e2e/perf/text-selection.spec.ts +119 -0
- package/e2e/perf/utils/metrics.ts +350 -0
- package/e2e/perf/utils/perf-cli.ts +86 -0
- package/e2e/persistence-file.spec.ts +41 -26
- package/e2e/utils/selection.ts +17 -73
- package/go/cmd/readit/main.go +416 -0
- package/go/go.mod +20 -0
- package/go/go.sum +41 -0
- package/go/internal/server/anchor.go +302 -0
- package/go/internal/server/anchor_test.go +111 -0
- package/go/internal/server/comments.go +390 -0
- package/go/internal/server/documents.go +113 -0
- package/go/internal/server/embed.go +17 -0
- package/go/internal/server/headings.go +33 -0
- package/go/internal/server/headings_test.go +75 -0
- package/go/internal/server/htmltext.go +123 -0
- package/go/internal/server/markdown.go +157 -0
- package/go/internal/server/markdown_bench_test.go +42 -0
- package/go/internal/server/markdown_test.go +79 -0
- package/go/internal/server/server.go +453 -0
- package/go/internal/server/server_bench_test.go +122 -0
- package/go/internal/server/settings.go +110 -0
- package/go/internal/server/sse.go +140 -0
- package/go/internal/server/storage.go +275 -0
- package/go/internal/server/storage_test.go +118 -0
- package/go/internal/server/template.go +66 -0
- package/go/internal/server/types.go +101 -0
- package/go/internal/server/watcher.go +74 -0
- package/index.html +4 -14
- package/nvim-readit/lua/readit/health.lua +64 -0
- package/nvim-readit/lua/readit/init.lua +463 -0
- package/nvim-readit/plugin/readit.lua +19 -0
- package/package.json +24 -41
- package/playwright.config.ts +12 -0
- package/shell/_readit +158 -0
- package/shell/readit.zsh +87 -0
- package/src/App.svelte +881 -0
- package/src/{cli/index.ts → cli.ts} +216 -70
- package/src/components/ActionsMenu.svelte +95 -0
- package/src/components/CommentBadge.svelte +67 -0
- package/src/components/CommentErrorBanner.svelte +33 -0
- package/src/components/CommentInput.svelte +75 -0
- package/src/components/CommentListItem.svelte +95 -0
- package/src/components/CommentManager.svelte +129 -0
- package/src/components/CommentNav.svelte +109 -0
- package/src/components/DocumentViewer.svelte +218 -0
- package/src/components/FloatingComment.svelte +107 -0
- package/src/components/Header.svelte +76 -0
- package/src/components/InlineEditor.svelte +72 -0
- package/src/components/MarginNote.svelte +167 -0
- package/src/components/MarginNotesContainer.svelte +33 -0
- package/src/components/RawModal.svelte +126 -0
- package/src/components/ReanchorConfirm.svelte +30 -0
- package/src/components/SettingsModal.svelte +220 -0
- package/src/components/ShortcutCapture.svelte +82 -0
- package/src/components/ShortcutList.svelte +145 -0
- package/src/components/TabBar.svelte +52 -0
- package/src/components/TableOfContents.svelte +125 -0
- package/src/components/ui/ActionLink.svelte +40 -0
- package/src/components/ui/Button.svelte +53 -0
- package/src/components/ui/Dialog.svelte +97 -0
- package/src/components/ui/DropdownMenu.svelte +85 -0
- package/src/components/ui/DropdownMenuItem.svelte +38 -0
- package/src/components/ui/DropdownMenuSeparator.svelte +11 -0
- package/src/components/ui/Text.svelte +42 -0
- package/src/env.d.ts +6 -0
- package/src/index.css +36 -166
- package/src/lib/__fixtures__/bench-data.ts +1 -54
- package/src/lib/anchor.bench.ts +47 -68
- package/src/lib/anchor.test.ts +5 -9
- package/src/lib/anchor.ts +9 -93
- package/src/lib/comment-storage.bench.ts +6 -20
- package/src/lib/comment-storage.test.ts +45 -37
- package/src/lib/comment-storage.ts +23 -64
- package/src/lib/export.bench.ts +9 -23
- package/src/lib/export.ts +7 -14
- package/src/lib/headings.test.ts +103 -0
- package/src/lib/headings.ts +44 -0
- package/src/lib/highlight/core.test.ts +1 -6
- package/src/lib/highlight/dom.ts +53 -280
- package/src/lib/highlight/highlight-registry.ts +221 -0
- package/src/lib/highlight/highlight.bench.ts +92 -0
- package/src/lib/highlight/highlighter.ts +122 -302
- package/src/lib/highlight/{core.ts → resolver.ts} +3 -19
- package/src/lib/highlight/types.ts +0 -40
- package/src/lib/html-text.test.ts +162 -0
- package/src/lib/html-text.ts +161 -0
- package/src/lib/i18n/en.ts +13 -36
- package/src/lib/i18n/ja.ts +14 -37
- package/src/lib/i18n/types.ts +13 -36
- package/src/lib/margin-layout.bench.ts +48 -15
- package/src/lib/margin-layout.ts +2 -31
- package/src/lib/markdown-renderer.test.ts +154 -0
- package/src/lib/markdown-renderer.ts +177 -0
- package/src/lib/mermaid-config.ts +38 -0
- package/src/lib/mermaid-renderer.ts +162 -0
- package/src/lib/mermaid-worker.ts +60 -0
- package/src/lib/positions.ts +157 -0
- package/src/lib/shortcut-registry.ts +138 -103
- package/src/lib/utils.ts +2 -48
- package/src/main.ts +16 -0
- package/src/schema.ts +92 -0
- package/src/{server/index.ts → server.ts} +427 -163
- package/src/stores/app.svelte.ts +231 -0
- package/src/stores/locale.svelte.ts +46 -0
- package/src/stores/settings.svelte.ts +90 -0
- package/src/stores/shortcuts.svelte.ts +104 -0
- package/src/stores/ui.svelte.ts +12 -0
- package/src/template.ts +104 -0
- package/src/test-setup.ts +47 -0
- package/svelte.config.js +5 -0
- package/tsconfig.json +2 -2
- package/vite.config.ts +31 -3
- package/vscode-readit/.mcp.json +7 -0
- package/vscode-readit/.vscodeignore +7 -0
- package/vscode-readit/bun.lock +78 -0
- package/vscode-readit/icon.svg +10 -0
- package/vscode-readit/package.json +110 -0
- package/vscode-readit/src/extension.ts +117 -0
- package/vscode-readit/src/server-manager.ts +272 -0
- package/vscode-readit/src/webview-provider.ts +204 -0
- package/vscode-readit/tsconfig.json +20 -0
- package/e2e/fixtures/sample.html +0 -13
- package/src/App.tsx +0 -416
- package/src/components/ActionsMenu.tsx +0 -112
- package/src/components/DocumentViewer/CodeBlock.tsx +0 -160
- package/src/components/DocumentViewer/DocumentViewer.tsx +0 -259
- package/src/components/DocumentViewer/IframeContainer.tsx +0 -251
- package/src/components/DocumentViewer/InlineCode.tsx +0 -60
- package/src/components/DocumentViewer/MermaidDiagram.tsx +0 -137
- package/src/components/DocumentViewer/index.ts +0 -1
- package/src/components/FloatingTOC.tsx +0 -61
- package/src/components/Header.tsx +0 -65
- package/src/components/InlineEditor.tsx +0 -74
- package/src/components/MarginNote.tsx +0 -207
- package/src/components/MarginNotes.tsx +0 -50
- package/src/components/RawModal.tsx +0 -143
- package/src/components/ReanchorConfirm.tsx +0 -36
- package/src/components/SettingsModal.tsx +0 -310
- package/src/components/ShortcutCapture.tsx +0 -48
- package/src/components/ShortcutList.tsx +0 -198
- package/src/components/TabBar.tsx +0 -60
- package/src/components/TableOfContents.tsx +0 -108
- package/src/components/comments/CommentBadge.tsx +0 -49
- package/src/components/comments/CommentInput.tsx +0 -114
- package/src/components/comments/CommentListItem.tsx +0 -92
- package/src/components/comments/CommentManager.tsx +0 -113
- package/src/components/comments/CommentMinimap.tsx +0 -62
- package/src/components/comments/CommentNav.tsx +0 -109
- package/src/components/ui/ActionBar.tsx +0 -16
- package/src/components/ui/ActionLink.tsx +0 -32
- package/src/components/ui/Button.tsx +0 -55
- package/src/components/ui/Dialog.tsx +0 -156
- package/src/components/ui/DropdownMenu.tsx +0 -114
- package/src/components/ui/SeparatorDot.tsx +0 -9
- package/src/components/ui/Text.tsx +0 -54
- package/src/contexts/CommentContext.tsx +0 -229
- package/src/contexts/LayoutContext.tsx +0 -88
- package/src/contexts/LocaleContext.tsx +0 -35
- package/src/hooks/useClickOutside.ts +0 -35
- package/src/hooks/useClipboard.ts +0 -82
- package/src/hooks/useCommentNavigation.ts +0 -130
- package/src/hooks/useComments.ts +0 -323
- package/src/hooks/useDocument.ts +0 -156
- package/src/hooks/useEditorScheme.ts +0 -51
- package/src/hooks/useFontPreference.ts +0 -59
- package/src/hooks/useHeadings.test.ts +0 -159
- package/src/hooks/useHeadings.ts +0 -129
- package/src/hooks/useKeybindings.ts +0 -108
- package/src/hooks/useKeyboardShortcuts.ts +0 -63
- package/src/hooks/useLayoutMode.ts +0 -44
- package/src/hooks/useLocalePreference.ts +0 -42
- package/src/hooks/useReanchorMode.ts +0 -33
- package/src/hooks/useScrollMetrics.ts +0 -56
- package/src/hooks/useScrollSpy.ts +0 -81
- package/src/hooks/useTextSelection.ts +0 -123
- package/src/hooks/useThemePreference.ts +0 -66
- package/src/lib/context.bench.ts +0 -41
- package/src/lib/context.test.ts +0 -224
- package/src/lib/context.ts +0 -193
- package/src/lib/editor-links.ts +0 -59
- package/src/lib/highlight/colors.ts +0 -37
- package/src/lib/highlight/index.ts +0 -23
- package/src/lib/highlight/script-builder.ts +0 -485
- package/src/lib/html-processor.test.tsx +0 -170
- package/src/lib/html-processor.tsx +0 -95
- package/src/lib/i18n/completeness.test.ts +0 -51
- package/src/lib/i18n/translations.test.ts +0 -39
- package/src/lib/layout-constants.ts +0 -12
- package/src/lib/scroll.test.ts +0 -118
- package/src/lib/scroll.ts +0 -47
- package/src/lib/shortcut-registry.test.ts +0 -173
- package/src/lib/utils.test.ts +0 -110
- package/src/main.tsx +0 -13
- package/src/store/index.test.ts +0 -242
- package/src/store/index.ts +0 -254
- package/src/types/index.ts +0 -127
|
@@ -1,28 +1,61 @@
|
|
|
1
1
|
import { bench, describe } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
COMMENTS_1,
|
|
4
|
+
COMMENTS_10,
|
|
5
|
+
COMMENTS_50,
|
|
6
|
+
} from "./__fixtures__/bench-data";
|
|
3
7
|
import { resolveMarginNotePositions } from "./margin-layout";
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
function makeHighlightPositions(
|
|
10
|
+
commentIds: string[],
|
|
11
|
+
spacing: number,
|
|
12
|
+
): Record<string, number> {
|
|
13
|
+
const positions: Record<string, number> = {};
|
|
14
|
+
for (let i = 0; i < commentIds.length; i++) {
|
|
15
|
+
positions[commentIds[i]] = i * spacing;
|
|
16
|
+
}
|
|
17
|
+
return positions;
|
|
18
|
+
}
|
|
8
19
|
|
|
9
|
-
|
|
10
|
-
|
|
20
|
+
describe("resolveMarginNotePositions — well-spaced", () => {
|
|
21
|
+
bench("1 comment", () => {
|
|
22
|
+
const ids = COMMENTS_1.map((c) => c.id);
|
|
23
|
+
const positions = makeHighlightPositions(ids, 300);
|
|
24
|
+
resolveMarginNotePositions(ids, positions, undefined);
|
|
25
|
+
});
|
|
11
26
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
27
|
+
bench("10 comments", () => {
|
|
28
|
+
const ids = COMMENTS_10.map((c) => c.id);
|
|
29
|
+
const positions = makeHighlightPositions(ids, 300);
|
|
30
|
+
resolveMarginNotePositions(ids, positions, undefined);
|
|
15
31
|
});
|
|
16
32
|
|
|
17
|
-
bench("
|
|
18
|
-
|
|
33
|
+
bench("50 comments", () => {
|
|
34
|
+
const ids = COMMENTS_50.map((c) => c.id);
|
|
35
|
+
const positions = makeHighlightPositions(ids, 300);
|
|
36
|
+
resolveMarginNotePositions(ids, positions, undefined);
|
|
19
37
|
});
|
|
38
|
+
});
|
|
20
39
|
|
|
21
|
-
|
|
22
|
-
|
|
40
|
+
describe("resolveMarginNotePositions — clustered", () => {
|
|
41
|
+
bench("10 comments, 10px apart", () => {
|
|
42
|
+
const ids = COMMENTS_10.map((c) => c.id);
|
|
43
|
+
const positions = makeHighlightPositions(ids, 10);
|
|
44
|
+
resolveMarginNotePositions(ids, positions, undefined);
|
|
23
45
|
});
|
|
24
46
|
|
|
25
|
-
bench("50
|
|
26
|
-
|
|
47
|
+
bench("50 comments, 10px apart", () => {
|
|
48
|
+
const ids = COMMENTS_50.map((c) => c.id);
|
|
49
|
+
const positions = makeHighlightPositions(ids, 10);
|
|
50
|
+
resolveMarginNotePositions(ids, positions, undefined);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("resolveMarginNotePositions — with input zone", () => {
|
|
55
|
+
bench("50 comments, input at middle", () => {
|
|
56
|
+
const ids = COMMENTS_50.map((c) => c.id);
|
|
57
|
+
const positions = makeHighlightPositions(ids, 100);
|
|
58
|
+
const midpoint = 25 * 100;
|
|
59
|
+
resolveMarginNotePositions(ids, positions, midpoint);
|
|
27
60
|
});
|
|
28
61
|
});
|
package/src/lib/margin-layout.ts
CHANGED
|
@@ -1,34 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
MARGIN_NOTE_MIN_GAP_PX,
|
|
4
|
-
} from "./layout-constants";
|
|
1
|
+
const MARGIN_NOTE_MIN_GAP_PX = 150;
|
|
2
|
+
const COMMENT_INPUT_HEIGHT_PX = 160;
|
|
5
3
|
|
|
6
4
|
interface NotePosition {
|
|
7
5
|
commentId: string;
|
|
8
6
|
top: number;
|
|
9
7
|
}
|
|
10
8
|
|
|
11
|
-
/**
|
|
12
|
-
* Resolves margin note positions to avoid overlaps.
|
|
13
|
-
*
|
|
14
|
-
* Algorithm:
|
|
15
|
-
* 1. Build initial positions from highlight positions
|
|
16
|
-
* 2. Handle input zone collision (push notes up or down to avoid input)
|
|
17
|
-
* 3. Resolve note-to-note overlaps:
|
|
18
|
-
* - Pass 1: Notes above input zone → push UP
|
|
19
|
-
* - Pass 2: Notes at/below input zone → push DOWN
|
|
20
|
-
*
|
|
21
|
-
* @param commentIds - Comment IDs in document order (sorted by startOffset)
|
|
22
|
-
* @param highlightPositions - Map of comment ID to top position
|
|
23
|
-
* @param pendingSelectionTop - Top position of input zone (if any)
|
|
24
|
-
* @returns Map of comment ID to resolved top position
|
|
25
|
-
*/
|
|
26
9
|
export function resolveMarginNotePositions(
|
|
27
10
|
commentIds: string[],
|
|
28
11
|
highlightPositions: Record<string, number>,
|
|
29
12
|
pendingSelectionTop: number | undefined,
|
|
30
13
|
): Map<string, number> {
|
|
31
|
-
// Only include comments with known positions (avoids jolt to position 0)
|
|
32
14
|
const positions: NotePosition[] = commentIds
|
|
33
15
|
.filter((id) => id in highlightPositions)
|
|
34
16
|
.map((id) => ({
|
|
@@ -36,10 +18,8 @@ export function resolveMarginNotePositions(
|
|
|
36
18
|
top: highlightPositions[id],
|
|
37
19
|
}));
|
|
38
20
|
|
|
39
|
-
// Sort by top position
|
|
40
21
|
positions.sort((a, b) => a.top - b.top);
|
|
41
22
|
|
|
42
|
-
// Handle input zone collision - check visual overlap, not just top position
|
|
43
23
|
if (pendingSelectionTop !== null && pendingSelectionTop !== undefined) {
|
|
44
24
|
const inputStart = pendingSelectionTop;
|
|
45
25
|
const inputEnd = pendingSelectionTop + COMMENT_INPUT_HEIGHT_PX;
|
|
@@ -47,48 +27,39 @@ export function resolveMarginNotePositions(
|
|
|
47
27
|
for (const pos of positions) {
|
|
48
28
|
const noteBottom = pos.top + MARGIN_NOTE_MIN_GAP_PX;
|
|
49
29
|
|
|
50
|
-
// Check if note visually overlaps with input zone
|
|
51
30
|
const overlaps = noteBottom > inputStart && pos.top < inputEnd;
|
|
52
31
|
|
|
53
32
|
if (overlaps) {
|
|
54
33
|
if (pos.top < inputStart) {
|
|
55
|
-
// Note is above input but overlaps - push UP
|
|
56
34
|
pos.top = Math.max(0, inputStart - MARGIN_NOTE_MIN_GAP_PX);
|
|
57
35
|
} else {
|
|
58
|
-
// Note is within/below input zone - push DOWN
|
|
59
36
|
pos.top = inputEnd;
|
|
60
37
|
}
|
|
61
38
|
}
|
|
62
39
|
}
|
|
63
|
-
// Re-sort after potential position changes
|
|
64
40
|
positions.sort((a, b) => a.top - b.top);
|
|
65
41
|
}
|
|
66
42
|
|
|
67
|
-
// Resolve note-to-note overlaps
|
|
68
43
|
const inputStartForOverlap = pendingSelectionTop ?? Infinity;
|
|
69
44
|
const inputEndForOverlap =
|
|
70
45
|
pendingSelectionTop != null
|
|
71
46
|
? pendingSelectionTop + COMMENT_INPUT_HEIGHT_PX
|
|
72
47
|
: Infinity;
|
|
73
48
|
|
|
74
|
-
// Pass 1: Notes ABOVE input - resolve by pushing UP (bottom to top)
|
|
75
49
|
for (let i = positions.length - 2; i >= 0; i--) {
|
|
76
50
|
const curr = positions[i];
|
|
77
51
|
const next = positions[i + 1];
|
|
78
|
-
// Only process if next note is above the input zone
|
|
79
52
|
if (next.top >= inputStartForOverlap) continue;
|
|
80
53
|
if (next.top - curr.top < MARGIN_NOTE_MIN_GAP_PX) {
|
|
81
54
|
curr.top = Math.max(0, next.top - MARGIN_NOTE_MIN_GAP_PX);
|
|
82
55
|
}
|
|
83
56
|
}
|
|
84
57
|
|
|
85
|
-
// Pass 2: Notes at/below input - resolve by pushing DOWN (top to bottom)
|
|
86
58
|
for (let i = 1; i < positions.length; i++) {
|
|
87
59
|
const prev = positions[i - 1];
|
|
88
60
|
const curr = positions[i];
|
|
89
61
|
if (curr.top - prev.top < MARGIN_NOTE_MIN_GAP_PX) {
|
|
90
62
|
let newTop = prev.top + MARGIN_NOTE_MIN_GAP_PX;
|
|
91
|
-
// If new position lands in input zone, skip to below input
|
|
92
63
|
if (newTop >= inputStartForOverlap && newTop < inputEndForOverlap) {
|
|
93
64
|
newTop = inputEndForOverlap;
|
|
94
65
|
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { JSDOM } from "jsdom";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { renderMarkdown } from "./markdown-renderer";
|
|
4
|
+
|
|
5
|
+
const SAMPLE_MARKDOWN = `# Hello World
|
|
6
|
+
|
|
7
|
+
This is a paragraph with **bold** and *italic* text.
|
|
8
|
+
|
|
9
|
+
## Code Example
|
|
10
|
+
|
|
11
|
+
\`\`\`typescript
|
|
12
|
+
function hello() {
|
|
13
|
+
return "world";
|
|
14
|
+
}
|
|
15
|
+
\`\`\`
|
|
16
|
+
|
|
17
|
+
Some text after the code block.
|
|
18
|
+
|
|
19
|
+
- Item 1
|
|
20
|
+
- Item 2
|
|
21
|
+
- Item 3
|
|
22
|
+
|
|
23
|
+
### Table
|
|
24
|
+
|
|
25
|
+
| Name | Value |
|
|
26
|
+
|------|-------|
|
|
27
|
+
| foo | bar |
|
|
28
|
+
| baz | qux |
|
|
29
|
+
|
|
30
|
+
> A blockquote with some text.
|
|
31
|
+
|
|
32
|
+
Final paragraph.
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
const SAMPLE_WITH_MERMAID = `# Diagrams
|
|
36
|
+
|
|
37
|
+
\`\`\`mermaid
|
|
38
|
+
graph TD
|
|
39
|
+
A --> B
|
|
40
|
+
\`\`\`
|
|
41
|
+
|
|
42
|
+
After mermaid.
|
|
43
|
+
`;
|
|
44
|
+
|
|
45
|
+
describe("renderMarkdown", () => {
|
|
46
|
+
it("renders basic markdown to HTML", async () => {
|
|
47
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
48
|
+
|
|
49
|
+
expect(html).toContain("<h1");
|
|
50
|
+
expect(html).toContain("Hello World");
|
|
51
|
+
expect(html).toContain("<strong>bold</strong>");
|
|
52
|
+
expect(html).toContain("<em>italic</em>");
|
|
53
|
+
expect(html).toContain("<h2");
|
|
54
|
+
expect(html).toContain("Code Example");
|
|
55
|
+
expect(html).toContain("<h3");
|
|
56
|
+
expect(html).toContain("Table");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("extracts headings with correct IDs", async () => {
|
|
60
|
+
const { headings } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
61
|
+
|
|
62
|
+
expect(headings).toEqual([
|
|
63
|
+
{ id: "hello-world", text: "Hello World", level: 1 },
|
|
64
|
+
{ id: "code-example", text: "Code Example", level: 2 },
|
|
65
|
+
{ id: "table", text: "Table", level: 3 },
|
|
66
|
+
]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("injects heading IDs into HTML", async () => {
|
|
70
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
71
|
+
|
|
72
|
+
expect(html).toContain('id="hello-world"');
|
|
73
|
+
expect(html).toContain('id="code-example"');
|
|
74
|
+
expect(html).toContain('id="table"');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("syntax-highlights code blocks with shiki", async () => {
|
|
78
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
79
|
+
|
|
80
|
+
expect(html).toContain("shiki");
|
|
81
|
+
expect(html).toContain("hello");
|
|
82
|
+
expect(html).toContain("world");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("leaves mermaid blocks for client-side hydration", async () => {
|
|
86
|
+
const { html } = await renderMarkdown(SAMPLE_WITH_MERMAID);
|
|
87
|
+
|
|
88
|
+
expect(html).toContain('class="language-mermaid"');
|
|
89
|
+
expect(html).toContain("graph TD");
|
|
90
|
+
expect(html).not.toMatch(/class="shiki[^"]*"[^>]*>.*graph TD/s);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("renders GFM tables", async () => {
|
|
94
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
95
|
+
|
|
96
|
+
expect(html).toContain("<table>");
|
|
97
|
+
expect(html).toContain("<th>");
|
|
98
|
+
expect(html).toContain("foo");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("renders lists", async () => {
|
|
102
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
103
|
+
|
|
104
|
+
expect(html).toContain("<ul>");
|
|
105
|
+
expect(html).toContain("<li>");
|
|
106
|
+
expect(html).toContain("Item 1");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("renders blockquotes", async () => {
|
|
110
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
111
|
+
|
|
112
|
+
expect(html).toContain("<blockquote>");
|
|
113
|
+
expect(html).toContain("A blockquote with some text.");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("handles duplicate headings", async () => {
|
|
117
|
+
const md = `## Section\n\n## Section\n\n## Section\n`;
|
|
118
|
+
const { headings } = await renderMarkdown(md);
|
|
119
|
+
|
|
120
|
+
expect(headings).toEqual([
|
|
121
|
+
{ id: "section", text: "Section", level: 2 },
|
|
122
|
+
{ id: "section-1", text: "Section", level: 2 },
|
|
123
|
+
{ id: "section-2", text: "Section", level: 2 },
|
|
124
|
+
]);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("text offset conformance", () => {
|
|
129
|
+
it("rendered HTML has proper block structure for text offset extraction", async () => {
|
|
130
|
+
const { html } = await renderMarkdown(SAMPLE_MARKDOWN);
|
|
131
|
+
|
|
132
|
+
const dom = new JSDOM(html);
|
|
133
|
+
const doc = dom.window.document;
|
|
134
|
+
|
|
135
|
+
const paragraphs = doc.querySelectorAll("p");
|
|
136
|
+
expect(paragraphs.length).toBeGreaterThan(0);
|
|
137
|
+
|
|
138
|
+
const h1 = doc.querySelector("h1");
|
|
139
|
+
expect(h1?.id).toBe("hello-world");
|
|
140
|
+
expect(h1?.textContent).toBe("Hello World");
|
|
141
|
+
|
|
142
|
+
const pres = doc.querySelectorAll("pre");
|
|
143
|
+
expect(pres.length).toBeGreaterThan(0);
|
|
144
|
+
|
|
145
|
+
const lis = doc.querySelectorAll("li");
|
|
146
|
+
expect(lis.length).toBe(3);
|
|
147
|
+
|
|
148
|
+
const trs = doc.querySelectorAll("tr");
|
|
149
|
+
expect(trs.length).toBeGreaterThan(0);
|
|
150
|
+
|
|
151
|
+
const blockquote = doc.querySelector("blockquote");
|
|
152
|
+
expect(blockquote).not.toBeNull();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import MarkdownIt from "markdown-it";
|
|
2
|
+
import type { BundledLanguage, BundledTheme, HighlighterGeneric } from "shiki";
|
|
3
|
+
import { type Heading, parseMarkdownHeadings } from "./headings";
|
|
4
|
+
import { renderMermaidBlocks } from "./mermaid-renderer";
|
|
5
|
+
|
|
6
|
+
const SHIKI_LANGUAGES: BundledLanguage[] = [
|
|
7
|
+
"bash",
|
|
8
|
+
"css",
|
|
9
|
+
"diff",
|
|
10
|
+
"go",
|
|
11
|
+
"graphql",
|
|
12
|
+
"javascript",
|
|
13
|
+
"json",
|
|
14
|
+
"jsx",
|
|
15
|
+
"markdown",
|
|
16
|
+
"python",
|
|
17
|
+
"rust",
|
|
18
|
+
"sql",
|
|
19
|
+
"tsx",
|
|
20
|
+
"typescript",
|
|
21
|
+
"yaml",
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const SHIKI_THEME: BundledTheme = "one-dark-pro";
|
|
25
|
+
|
|
26
|
+
let shikiInstance: HighlighterGeneric<BundledLanguage, BundledTheme> | null =
|
|
27
|
+
null;
|
|
28
|
+
let shikiPromise: Promise<
|
|
29
|
+
HighlighterGeneric<BundledLanguage, BundledTheme>
|
|
30
|
+
> | null = null;
|
|
31
|
+
|
|
32
|
+
export async function getShiki(): Promise<
|
|
33
|
+
HighlighterGeneric<BundledLanguage, BundledTheme>
|
|
34
|
+
> {
|
|
35
|
+
if (shikiInstance) return shikiInstance;
|
|
36
|
+
if (shikiPromise) return shikiPromise;
|
|
37
|
+
|
|
38
|
+
shikiPromise = import("shiki").then(async ({ createHighlighter }) => {
|
|
39
|
+
const highlighter = await createHighlighter({
|
|
40
|
+
themes: [SHIKI_THEME],
|
|
41
|
+
langs: SHIKI_LANGUAGES,
|
|
42
|
+
});
|
|
43
|
+
shikiInstance = highlighter;
|
|
44
|
+
return highlighter;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return shikiPromise;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function createMarkdownRenderer(
|
|
51
|
+
shiki: HighlighterGeneric<BundledLanguage, BundledTheme>,
|
|
52
|
+
): MarkdownIt {
|
|
53
|
+
const md = new MarkdownIt({
|
|
54
|
+
html: true, // Allow raw HTML (matches rehype-raw behavior)
|
|
55
|
+
linkify: true,
|
|
56
|
+
typographer: false,
|
|
57
|
+
highlight(code: string, lang: string): string {
|
|
58
|
+
if (lang === "mermaid") {
|
|
59
|
+
return `<pre><code class="language-mermaid">${md.utils.escapeHtml(code)}</code></pre>`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const language = normalizeLanguage(lang);
|
|
63
|
+
|
|
64
|
+
if (language && shiki.getLoadedLanguages().includes(language)) {
|
|
65
|
+
return shiki.codeToHtml(code, {
|
|
66
|
+
lang: language,
|
|
67
|
+
theme: SHIKI_THEME,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return `<pre class="shiki"><code>${md.utils.escapeHtml(code)}</code></pre>`;
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return md;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function normalizeLanguage(lang: string): BundledLanguage | undefined {
|
|
79
|
+
const aliases: Record<string, BundledLanguage> = {
|
|
80
|
+
sh: "bash",
|
|
81
|
+
shell: "bash",
|
|
82
|
+
js: "javascript",
|
|
83
|
+
ts: "typescript",
|
|
84
|
+
py: "python",
|
|
85
|
+
rs: "rust",
|
|
86
|
+
yml: "yaml",
|
|
87
|
+
md: "markdown",
|
|
88
|
+
};
|
|
89
|
+
const normalized = lang.toLowerCase().trim();
|
|
90
|
+
if (SHIKI_LANGUAGES.includes(normalized as BundledLanguage)) {
|
|
91
|
+
return normalized as BundledLanguage;
|
|
92
|
+
}
|
|
93
|
+
return aliases[normalized];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function injectHeadingIds(html: string, headings: Heading[]): string {
|
|
97
|
+
let headingIdx = 0;
|
|
98
|
+
|
|
99
|
+
return html.replace(
|
|
100
|
+
/<(h[1-6])([^>]*)>([\s\S]*?)<\/\1>/gi,
|
|
101
|
+
(match, tag: string, attrs: string, content: string) => {
|
|
102
|
+
if (headingIdx >= headings.length) return match;
|
|
103
|
+
|
|
104
|
+
const heading = headings[headingIdx];
|
|
105
|
+
headingIdx++;
|
|
106
|
+
|
|
107
|
+
if (/\bid\s*=/.test(attrs)) return match;
|
|
108
|
+
|
|
109
|
+
return `<${tag} id="${heading.id}"${attrs}>${content}</${tag}>`;
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface RenderResult {
|
|
115
|
+
html: string;
|
|
116
|
+
headings: Heading[];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const MERMAID_BLOCK_RE =
|
|
120
|
+
/<pre><code class="language-mermaid">([\s\S]*?)<\/code><\/pre>/g;
|
|
121
|
+
|
|
122
|
+
function unescapeHtml(str: string): string {
|
|
123
|
+
return str
|
|
124
|
+
.replace(/&/g, "&")
|
|
125
|
+
.replace(/</g, "<")
|
|
126
|
+
.replace(/>/g, ">")
|
|
127
|
+
.replace(/"/g, '"');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function replaceMermaidBlocks(html: string): Promise<string> {
|
|
131
|
+
MERMAID_BLOCK_RE.lastIndex = 0;
|
|
132
|
+
const matches: { fullMatch: string; code: string; index: number }[] = [];
|
|
133
|
+
|
|
134
|
+
let match: RegExpExecArray | null = MERMAID_BLOCK_RE.exec(html);
|
|
135
|
+
while (match !== null) {
|
|
136
|
+
matches.push({
|
|
137
|
+
fullMatch: match[0],
|
|
138
|
+
code: unescapeHtml(match[1]),
|
|
139
|
+
index: match.index,
|
|
140
|
+
});
|
|
141
|
+
match = MERMAID_BLOCK_RE.exec(html);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (matches.length === 0) return html;
|
|
145
|
+
|
|
146
|
+
const codes = matches.map((m) => m.code);
|
|
147
|
+
const svgs = await renderMermaidBlocks(codes);
|
|
148
|
+
|
|
149
|
+
// Replace in reverse order to preserve string indices
|
|
150
|
+
for (let i = matches.length - 1; i >= 0; i--) {
|
|
151
|
+
const svg = svgs[i];
|
|
152
|
+
if (svg !== null) {
|
|
153
|
+
const { fullMatch, index } = matches[i];
|
|
154
|
+
const replacement = `<div class="mermaid-container">${svg}</div>`;
|
|
155
|
+
html =
|
|
156
|
+
html.slice(0, index) +
|
|
157
|
+
replacement +
|
|
158
|
+
html.slice(index + fullMatch.length);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return html;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export async function renderMarkdown(content: string): Promise<RenderResult> {
|
|
166
|
+
const shiki = await getShiki();
|
|
167
|
+
const md = createMarkdownRenderer(shiki);
|
|
168
|
+
|
|
169
|
+
const headings = parseMarkdownHeadings(content);
|
|
170
|
+
|
|
171
|
+
let html = md.render(content);
|
|
172
|
+
|
|
173
|
+
html = injectHeadingIds(html, headings);
|
|
174
|
+
html = await replaceMermaidBlocks(html);
|
|
175
|
+
|
|
176
|
+
return { html, headings };
|
|
177
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export const MERMAID_THEME = {
|
|
2
|
+
fontSize: "16px",
|
|
3
|
+
primaryColor: "rgba(245, 222, 160, 0.8)",
|
|
4
|
+
primaryTextColor: "#3f3f46",
|
|
5
|
+
primaryBorderColor: "#c9a84a",
|
|
6
|
+
secondaryColor: "rgba(168, 196, 228, 0.6)",
|
|
7
|
+
secondaryTextColor: "#3f3f46",
|
|
8
|
+
secondaryBorderColor: "#5b7fa8",
|
|
9
|
+
tertiaryColor: "rgba(170, 210, 170, 0.6)",
|
|
10
|
+
tertiaryTextColor: "#3f3f46",
|
|
11
|
+
tertiaryBorderColor: "#5a9a62",
|
|
12
|
+
background: "#ffffff",
|
|
13
|
+
mainBkg: "#ffffff",
|
|
14
|
+
textColor: "#3f3f46",
|
|
15
|
+
lineColor: "#a1a1aa",
|
|
16
|
+
nodeBkg: "rgba(245, 222, 160, 0.6)",
|
|
17
|
+
nodeBorder: "#c9a84a",
|
|
18
|
+
clusterBkg: "rgba(250, 250, 250, 0.8)",
|
|
19
|
+
clusterBorder: "#e4e4e7",
|
|
20
|
+
actorBkg: "rgba(168, 196, 228, 0.5)",
|
|
21
|
+
actorBorder: "#5b7fa8",
|
|
22
|
+
actorTextColor: "#3f3f46",
|
|
23
|
+
signalColor: "#3f3f46",
|
|
24
|
+
signalTextColor: "#3f3f46",
|
|
25
|
+
noteBkgColor: "rgba(245, 222, 160, 0.5)",
|
|
26
|
+
noteBorderColor: "#c9a84a",
|
|
27
|
+
noteTextColor: "#3f3f46",
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
export function getMermaidInitConfig() {
|
|
31
|
+
return {
|
|
32
|
+
startOnLoad: false,
|
|
33
|
+
theme: "base" as const,
|
|
34
|
+
securityLevel: "strict" as const,
|
|
35
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
36
|
+
themeVariables: MERMAID_THEME,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
interface PendingRequest {
|
|
2
|
+
resolve: (svg: string) => void;
|
|
3
|
+
reject: (err: Error) => void;
|
|
4
|
+
timer: ReturnType<typeof setTimeout>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const RENDER_TIMEOUT_MS = 15_000;
|
|
8
|
+
const MAX_CONSECUTIVE_ERRORS = 3;
|
|
9
|
+
|
|
10
|
+
let worker: Worker | null = null;
|
|
11
|
+
let workerReady: Promise<void> | null = null;
|
|
12
|
+
const pendingRequests = new Map<string, PendingRequest>();
|
|
13
|
+
let requestCounter = 0;
|
|
14
|
+
let consecutiveErrors = 0;
|
|
15
|
+
|
|
16
|
+
function resetWorker(
|
|
17
|
+
reason: Error,
|
|
18
|
+
currentWorker: Worker | null = worker,
|
|
19
|
+
): void {
|
|
20
|
+
if (currentWorker) currentWorker.terminate();
|
|
21
|
+
if (worker === currentWorker) {
|
|
22
|
+
worker = null;
|
|
23
|
+
workerReady = null;
|
|
24
|
+
}
|
|
25
|
+
for (const [, pending] of pendingRequests) {
|
|
26
|
+
clearTimeout(pending.timer);
|
|
27
|
+
pending.reject(reason);
|
|
28
|
+
}
|
|
29
|
+
pendingRequests.clear();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createWorker(): { worker: Worker; ready: Promise<void> } {
|
|
33
|
+
const w = new Worker(new URL("./mermaid-worker.ts", import.meta.url).href, {
|
|
34
|
+
type: "module",
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const ready = new Promise<void>((resolve, reject) => {
|
|
38
|
+
const timeout = setTimeout(() => {
|
|
39
|
+
w.removeEventListener("message", onReady);
|
|
40
|
+
w.removeEventListener("error", onStartupError);
|
|
41
|
+
w.terminate();
|
|
42
|
+
if (worker === w) {
|
|
43
|
+
worker = null;
|
|
44
|
+
workerReady = null;
|
|
45
|
+
}
|
|
46
|
+
reject(new Error("Mermaid worker failed to start within 30s"));
|
|
47
|
+
}, 30_000);
|
|
48
|
+
|
|
49
|
+
function onStartupError(event: ErrorEvent) {
|
|
50
|
+
clearTimeout(timeout);
|
|
51
|
+
w.removeEventListener("message", onReady);
|
|
52
|
+
w.removeEventListener("error", onStartupError);
|
|
53
|
+
w.terminate();
|
|
54
|
+
if (worker === w) {
|
|
55
|
+
worker = null;
|
|
56
|
+
workerReady = null;
|
|
57
|
+
}
|
|
58
|
+
reject(new Error(`Mermaid worker failed to start: ${event.message}`));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function onReady(event: MessageEvent) {
|
|
62
|
+
if (event.data?.type === "ready") {
|
|
63
|
+
clearTimeout(timeout);
|
|
64
|
+
w.removeEventListener("message", onReady);
|
|
65
|
+
w.removeEventListener("error", onStartupError);
|
|
66
|
+
resolve();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
w.addEventListener("message", onReady);
|
|
70
|
+
w.addEventListener("error", onStartupError);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
w.addEventListener("message", (event: MessageEvent) => {
|
|
74
|
+
const { id, svg, error } = event.data;
|
|
75
|
+
if (!id) return;
|
|
76
|
+
|
|
77
|
+
const pending = pendingRequests.get(id);
|
|
78
|
+
if (!pending) return;
|
|
79
|
+
|
|
80
|
+
clearTimeout(pending.timer);
|
|
81
|
+
pendingRequests.delete(id);
|
|
82
|
+
|
|
83
|
+
if (error) {
|
|
84
|
+
consecutiveErrors++;
|
|
85
|
+
pending.reject(new Error(error));
|
|
86
|
+
} else {
|
|
87
|
+
consecutiveErrors = 0;
|
|
88
|
+
pending.resolve(svg);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
w.addEventListener("error", (event) => {
|
|
93
|
+
resetWorker(new Error(`Worker error: ${event.message}`), w);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return { worker: w, ready };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function ensureWorker(): Promise<Worker> {
|
|
100
|
+
if (worker && workerReady) {
|
|
101
|
+
await workerReady;
|
|
102
|
+
|
|
103
|
+
if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
|
|
104
|
+
resetWorker(
|
|
105
|
+
new Error("Mermaid worker restarted after repeated render failures"),
|
|
106
|
+
);
|
|
107
|
+
consecutiveErrors = 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!worker) {
|
|
112
|
+
const result = createWorker();
|
|
113
|
+
worker = result.worker;
|
|
114
|
+
workerReady = result.ready;
|
|
115
|
+
await workerReady;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return worker;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function renderMermaidSvg(code: string): Promise<string> {
|
|
122
|
+
const w = await ensureWorker();
|
|
123
|
+
|
|
124
|
+
const id = `req-${++requestCounter}`;
|
|
125
|
+
const diagramId = `mermaid-ssr-${requestCounter}`;
|
|
126
|
+
|
|
127
|
+
return new Promise<string>((resolve, reject) => {
|
|
128
|
+
const timer = setTimeout(() => {
|
|
129
|
+
pendingRequests.delete(id);
|
|
130
|
+
resetWorker(
|
|
131
|
+
new Error(`Mermaid render timed out after ${RENDER_TIMEOUT_MS}ms`),
|
|
132
|
+
w,
|
|
133
|
+
);
|
|
134
|
+
reject(
|
|
135
|
+
new Error(`Mermaid render timed out after ${RENDER_TIMEOUT_MS}ms`),
|
|
136
|
+
);
|
|
137
|
+
}, RENDER_TIMEOUT_MS);
|
|
138
|
+
|
|
139
|
+
pendingRequests.set(id, { resolve, reject, timer });
|
|
140
|
+
|
|
141
|
+
w.postMessage({ id, code, diagramId });
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function renderMermaidBlocks(
|
|
146
|
+
blocks: string[],
|
|
147
|
+
): Promise<(string | null)[]> {
|
|
148
|
+
const results: (string | null)[] = [];
|
|
149
|
+
for (const code of blocks) {
|
|
150
|
+
try {
|
|
151
|
+
const svg = await renderMermaidSvg(code);
|
|
152
|
+
results.push(svg);
|
|
153
|
+
} catch {
|
|
154
|
+
results.push(null);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return results;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function disposeMermaidWorker(): void {
|
|
161
|
+
resetWorker(new Error("Mermaid worker disposed"));
|
|
162
|
+
}
|