@peaske7/readit 0.2.0 → 0.3.0-rc.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/.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 -2
- package/biome.json +18 -8
- package/bun.lock +426 -568
- package/bunfig.toml +2 -0
- package/docs/perf-baseline.md +56 -1
- 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 +9 -11
- package/e2e/perf/fixtures/generate.ts +1 -5
- package/e2e/perf/screenshot-final.png +0 -0
- package/e2e/perf/utils/metrics.ts +73 -9
- 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 +152 -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 +20 -28
- package/shell/_readit +158 -0
- package/shell/readit.zsh +87 -0
- package/src/App.svelte +890 -0
- package/src/cli.ts +183 -21
- 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 +233 -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/MermaidEnhancer.svelte +218 -0
- package/src/components/MermaidModal.svelte +67 -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.tsx → Button.svelte} +19 -20
- 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.tsx → Text.svelte} +18 -23
- package/src/env.d.ts +6 -0
- package/src/index.css +141 -166
- package/src/lib/__fixtures__/bench-data.ts +0 -13
- package/src/lib/anchor.bench.ts +1 -12
- package/src/lib/anchor.test.ts +0 -8
- package/src/lib/anchor.ts +0 -4
- package/src/lib/comment-storage.bench.ts +49 -0
- package/src/lib/comment-storage.test.ts +103 -33
- package/src/lib/comment-storage.ts +25 -18
- package/src/lib/export.bench.ts +21 -0
- package/src/lib/export.ts +0 -1
- package/src/lib/fetch-or-throw.test.ts +59 -0
- package/src/lib/fetch-or-throw.ts +12 -0
- package/src/{hooks/useHeadings.test.ts → lib/headings.test.ts} +10 -24
- package/src/{hooks/useHeadings.ts → lib/headings.ts} +11 -13
- package/src/lib/highlight/core.test.ts +0 -5
- package/src/lib/highlight/dom.ts +52 -216
- package/src/lib/highlight/highlight-registry.ts +221 -0
- package/src/lib/highlight/highlight.bench.ts +92 -0
- package/src/lib/highlight/highlighter.ts +112 -132
- package/src/lib/highlight/resolver.ts +5 -79
- package/src/lib/highlight/types.ts +0 -5
- package/src/lib/html-text.test.ts +162 -0
- package/src/lib/html-text.ts +161 -0
- package/src/lib/i18n/en.ts +34 -0
- package/src/lib/i18n/ja.ts +34 -0
- package/src/lib/i18n/types.ts +33 -0
- package/src/lib/key-lock.test.ts +104 -0
- package/src/lib/key-lock.ts +23 -0
- package/src/lib/margin-layout.bench.ts +61 -0
- package/src/lib/margin-layout.ts +0 -7
- package/src/lib/markdown-renderer.test.ts +154 -0
- package/src/lib/markdown-renderer.ts +178 -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 +31 -24
- package/src/lib/shortcut-registry.ts +244 -0
- package/src/lib/utils.ts +0 -29
- package/src/main.ts +16 -0
- package/src/schema.ts +16 -5
- package/src/server.ts +355 -95
- 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 +23 -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 -368
- package/src/components/ActionsMenu.tsx +0 -91
- package/src/components/DocumentViewer/CodeBlock.tsx +0 -160
- package/src/components/DocumentViewer/DocumentViewer.tsx +0 -230
- package/src/components/DocumentViewer/MermaidDiagram.tsx +0 -136
- package/src/components/Header.tsx +0 -54
- package/src/components/InlineEditor.tsx +0 -74
- package/src/components/MarginNote.tsx +0 -185
- package/src/components/MarginNotes.tsx +0 -23
- package/src/components/RawModal.tsx +0 -144
- package/src/components/ReanchorConfirm.tsx +0 -36
- package/src/components/SettingsModal.tsx +0 -232
- 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 -86
- package/src/components/comments/CommentListItem.tsx +0 -90
- package/src/components/comments/CommentManager.tsx +0 -129
- package/src/components/comments/CommentNav.tsx +0 -109
- package/src/components/ui/ActionLink.tsx +0 -28
- package/src/components/ui/Dialog.tsx +0 -116
- package/src/components/ui/DropdownMenu.tsx +0 -158
- package/src/contexts/CommentContext.tsx +0 -198
- package/src/contexts/LocaleContext.tsx +0 -76
- package/src/contexts/PositionsContext.tsx +0 -16
- package/src/contexts/SettingsContext.tsx +0 -133
- package/src/hooks/useClickOutside.ts +0 -31
- package/src/hooks/useCommentNavigation.ts +0 -107
- package/src/hooks/useComments.ts +0 -311
- package/src/hooks/useDocument.ts +0 -157
- package/src/hooks/useScrollSpy.ts +0 -77
- package/src/hooks/useTextSelection.ts +0 -86
- package/src/lib/highlight/worker.ts +0 -45
- package/src/main.tsx +0 -13
- package/src/store.ts +0 -222
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import type { Heading } from "../lib/headings";
|
|
2
|
+
import type { Comment, Document, Selection } from "../schema";
|
|
3
|
+
|
|
4
|
+
export interface DocumentState {
|
|
5
|
+
document: Document;
|
|
6
|
+
headings: Heading[];
|
|
7
|
+
comments: Comment[];
|
|
8
|
+
commentsError: string | null;
|
|
9
|
+
sortedComments: Comment[];
|
|
10
|
+
selection: Selection | null;
|
|
11
|
+
pendingSelectionTop: number | undefined;
|
|
12
|
+
scrollY: number;
|
|
13
|
+
reanchorTarget: { commentId: string } | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface InlineData {
|
|
17
|
+
files: { path: string; fileName: string }[];
|
|
18
|
+
activeFile: string;
|
|
19
|
+
clean: boolean;
|
|
20
|
+
workingDirectory: string;
|
|
21
|
+
documents: Record<
|
|
22
|
+
string,
|
|
23
|
+
{
|
|
24
|
+
html: string;
|
|
25
|
+
headings: { id: string; text: string; level: number }[];
|
|
26
|
+
comments: Comment[];
|
|
27
|
+
}
|
|
28
|
+
>;
|
|
29
|
+
settings: { version: number; fontFamily: string };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createInitialDocumentState(doc: Document): DocumentState {
|
|
33
|
+
return {
|
|
34
|
+
document: doc,
|
|
35
|
+
headings: [],
|
|
36
|
+
comments: [],
|
|
37
|
+
commentsError: null,
|
|
38
|
+
sortedComments: [],
|
|
39
|
+
selection: null,
|
|
40
|
+
pendingSelectionTop: undefined,
|
|
41
|
+
scrollY: 0,
|
|
42
|
+
reanchorTarget: null,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function sortComments(comments: Comment[]): Comment[] {
|
|
47
|
+
return [...comments].sort((a, b) => a.startOffset - b.startOffset);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const app = $state({
|
|
51
|
+
documents: new Map<string, DocumentState>(),
|
|
52
|
+
activeDocumentPath: null as string | null,
|
|
53
|
+
documentOrder: [] as string[],
|
|
54
|
+
workingDirectory: null as string | null,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export function getActiveDocumentState(): DocumentState | undefined {
|
|
58
|
+
if (!app.activeDocumentPath) return undefined;
|
|
59
|
+
return app.documents.get(app.activeDocumentPath);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function setWorkingDirectory(dir: string): void {
|
|
63
|
+
app.workingDirectory = dir;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function openDocument(doc: Document, opts?: { active?: boolean }): void {
|
|
67
|
+
const active = opts?.active ?? true;
|
|
68
|
+
const nextActive =
|
|
69
|
+
active || !app.activeDocumentPath ? doc.filePath : app.activeDocumentPath;
|
|
70
|
+
|
|
71
|
+
if (app.documents.has(doc.filePath)) {
|
|
72
|
+
const prevDoc = app.documents.get(doc.filePath)!;
|
|
73
|
+
const newDocs = new Map(app.documents);
|
|
74
|
+
newDocs.set(doc.filePath, {
|
|
75
|
+
...prevDoc,
|
|
76
|
+
document: { ...prevDoc.document, ...doc },
|
|
77
|
+
});
|
|
78
|
+
app.documents = newDocs;
|
|
79
|
+
app.activeDocumentPath = nextActive;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const newDocs = new Map(app.documents);
|
|
84
|
+
newDocs.set(doc.filePath, createInitialDocumentState(doc));
|
|
85
|
+
app.documents = newDocs;
|
|
86
|
+
app.activeDocumentPath = nextActive;
|
|
87
|
+
app.documentOrder = [...app.documentOrder, doc.filePath];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function closeDocument(filePath: string): void {
|
|
91
|
+
const newDocs = new Map(app.documents);
|
|
92
|
+
newDocs.delete(filePath);
|
|
93
|
+
|
|
94
|
+
const newOrder = app.documentOrder.filter((p) => p !== filePath);
|
|
95
|
+
|
|
96
|
+
let newActive = app.activeDocumentPath;
|
|
97
|
+
if (app.activeDocumentPath === filePath) {
|
|
98
|
+
const oldIndex = app.documentOrder.indexOf(filePath);
|
|
99
|
+
newActive = newOrder[oldIndex] ?? newOrder[oldIndex - 1] ?? null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
app.documents = newDocs;
|
|
103
|
+
app.documentOrder = newOrder;
|
|
104
|
+
app.activeDocumentPath = newActive;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function setActiveDocument(filePath: string): void {
|
|
108
|
+
if (app.documents.has(filePath)) {
|
|
109
|
+
app.activeDocumentPath = filePath;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function resolveFilePath(filePath?: string): string | null {
|
|
114
|
+
return filePath ?? app.activeDocumentPath;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function updateDocState(
|
|
118
|
+
filePath: string,
|
|
119
|
+
updater: (state: DocumentState) => Partial<DocumentState>,
|
|
120
|
+
): void {
|
|
121
|
+
const docState = app.documents.get(filePath);
|
|
122
|
+
if (!docState) return;
|
|
123
|
+
|
|
124
|
+
const updates = updater(docState);
|
|
125
|
+
const newDocs = new Map(app.documents);
|
|
126
|
+
newDocs.set(filePath, { ...docState, ...updates });
|
|
127
|
+
app.documents = newDocs;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function setComments(comments: Comment[], filePath?: string): void {
|
|
131
|
+
const path = resolveFilePath(filePath);
|
|
132
|
+
if (!path) return;
|
|
133
|
+
updateDocState(path, () => ({
|
|
134
|
+
comments,
|
|
135
|
+
sortedComments: sortComments(comments),
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function setCommentsError(
|
|
140
|
+
error: string | null,
|
|
141
|
+
filePath?: string,
|
|
142
|
+
): void {
|
|
143
|
+
const path = resolveFilePath(filePath);
|
|
144
|
+
if (!path) return;
|
|
145
|
+
updateDocState(path, () => ({ commentsError: error }));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function setSelection(
|
|
149
|
+
selection: Selection | null,
|
|
150
|
+
filePath?: string,
|
|
151
|
+
): void {
|
|
152
|
+
const path = resolveFilePath(filePath);
|
|
153
|
+
if (!path) return;
|
|
154
|
+
updateDocState(path, () => ({ selection }));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function setPendingSelectionTop(
|
|
158
|
+
top: number | undefined,
|
|
159
|
+
filePath?: string,
|
|
160
|
+
): void {
|
|
161
|
+
const path = resolveFilePath(filePath);
|
|
162
|
+
if (!path) return;
|
|
163
|
+
updateDocState(path, () => ({ pendingSelectionTop: top }));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function setScrollY(y: number, filePath?: string): void {
|
|
167
|
+
const path = resolveFilePath(filePath);
|
|
168
|
+
if (!path) return;
|
|
169
|
+
updateDocState(path, () => ({ scrollY: y }));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function setReanchorTarget(
|
|
173
|
+
target: { commentId: string } | null,
|
|
174
|
+
filePath?: string,
|
|
175
|
+
): void {
|
|
176
|
+
const path = resolveFilePath(filePath);
|
|
177
|
+
if (!path) return;
|
|
178
|
+
updateDocState(path, () => ({ reanchorTarget: target }));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function updateDocumentHtml(html: string, filePath?: string): void {
|
|
182
|
+
const path = resolveFilePath(filePath);
|
|
183
|
+
if (!path) return;
|
|
184
|
+
updateDocState(path, (s) => ({
|
|
185
|
+
document: { ...s.document, html },
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function setHeadings(headings: Heading[], filePath?: string): void {
|
|
190
|
+
const path = resolveFilePath(filePath);
|
|
191
|
+
if (!path) return;
|
|
192
|
+
updateDocState(path, () => ({ headings }));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function hydrateFromInlineData(data: InlineData): void {
|
|
196
|
+
app.workingDirectory = data.workingDirectory;
|
|
197
|
+
|
|
198
|
+
const newDocs = new Map<string, DocumentState>();
|
|
199
|
+
const order: string[] = [];
|
|
200
|
+
|
|
201
|
+
const articleEl =
|
|
202
|
+
typeof document !== "undefined"
|
|
203
|
+
? document.getElementById("document-content")
|
|
204
|
+
: null;
|
|
205
|
+
|
|
206
|
+
for (const file of data.files) {
|
|
207
|
+
const docData = data.documents[file.path];
|
|
208
|
+
const isActiveFile = file.path === data.activeFile;
|
|
209
|
+
const doc: Document = {
|
|
210
|
+
html: docData?.html ?? (isActiveFile ? (articleEl?.innerHTML ?? "") : ""),
|
|
211
|
+
filePath: file.path,
|
|
212
|
+
fileName: file.fileName,
|
|
213
|
+
clean: data.clean,
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const comments = docData?.comments ?? [];
|
|
217
|
+
const headings = (docData?.headings ?? []) as Heading[];
|
|
218
|
+
|
|
219
|
+
newDocs.set(file.path, {
|
|
220
|
+
...createInitialDocumentState(doc),
|
|
221
|
+
comments,
|
|
222
|
+
sortedComments: sortComments(comments),
|
|
223
|
+
headings,
|
|
224
|
+
});
|
|
225
|
+
order.push(file.path);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
app.documents = newDocs;
|
|
229
|
+
app.documentOrder = order;
|
|
230
|
+
app.activeDocumentPath = data.activeFile;
|
|
231
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createT,
|
|
3
|
+
type Locale,
|
|
4
|
+
Locales,
|
|
5
|
+
type TranslationKey,
|
|
6
|
+
} from "../lib/i18n";
|
|
7
|
+
|
|
8
|
+
const STORAGE_KEY = "readit:locale";
|
|
9
|
+
|
|
10
|
+
function detectLocale(): Locale {
|
|
11
|
+
const browserLang = navigator.language.slice(0, 2).toLowerCase();
|
|
12
|
+
if (browserLang === "ja") return Locales.JA;
|
|
13
|
+
return Locales.EN;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getStoredLocale(): Locale {
|
|
17
|
+
try {
|
|
18
|
+
const stored = localStorage.getItem(STORAGE_KEY);
|
|
19
|
+
if (stored === Locales.JA || stored === Locales.EN) {
|
|
20
|
+
return stored;
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
// localStorage may be unavailable
|
|
24
|
+
}
|
|
25
|
+
return detectLocale();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const localeState = $state({
|
|
29
|
+
locale: getStoredLocale() as Locale,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export function setLocale(newLocale: Locale): void {
|
|
33
|
+
localeState.locale = newLocale;
|
|
34
|
+
try {
|
|
35
|
+
localStorage.setItem(STORAGE_KEY, newLocale);
|
|
36
|
+
} catch {
|
|
37
|
+
// localStorage may be unavailable
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function t(
|
|
42
|
+
key: TranslationKey,
|
|
43
|
+
params?: Record<string, string | number>,
|
|
44
|
+
): string {
|
|
45
|
+
return createT(localeState.locale)(key, params);
|
|
46
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FontFamilies,
|
|
3
|
+
type FontFamily,
|
|
4
|
+
type ThemeMode,
|
|
5
|
+
ThemeModes,
|
|
6
|
+
} from "../schema";
|
|
7
|
+
|
|
8
|
+
const THEME_STORAGE_KEY = "readit:theme";
|
|
9
|
+
const DARK_MQ = "(prefers-color-scheme: dark)";
|
|
10
|
+
|
|
11
|
+
function getStoredTheme(): ThemeMode {
|
|
12
|
+
try {
|
|
13
|
+
const stored = localStorage.getItem(THEME_STORAGE_KEY);
|
|
14
|
+
if (
|
|
15
|
+
stored === ThemeModes.LIGHT ||
|
|
16
|
+
stored === ThemeModes.DARK ||
|
|
17
|
+
stored === ThemeModes.SYSTEM
|
|
18
|
+
) {
|
|
19
|
+
return stored;
|
|
20
|
+
}
|
|
21
|
+
} catch {}
|
|
22
|
+
return ThemeModes.SYSTEM;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function applyTheme(mode: ThemeMode): void {
|
|
26
|
+
const isDark =
|
|
27
|
+
mode === ThemeModes.DARK ||
|
|
28
|
+
(mode === ThemeModes.SYSTEM && window.matchMedia(DARK_MQ).matches);
|
|
29
|
+
|
|
30
|
+
document.documentElement.classList.toggle("dark", isDark);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const settings = $state({
|
|
34
|
+
fontFamily: FontFamilies.SERIF as FontFamily,
|
|
35
|
+
themeMode: getStoredTheme() as ThemeMode,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export async function updateFontFamily(font: FontFamily): Promise<void> {
|
|
39
|
+
settings.fontFamily = font;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const response = await fetch("/api/settings", {
|
|
43
|
+
method: "PUT",
|
|
44
|
+
headers: { "Content-Type": "application/json" },
|
|
45
|
+
body: JSON.stringify({ fontFamily: font }),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
throw new Error("Failed to save settings");
|
|
50
|
+
}
|
|
51
|
+
} catch (err) {
|
|
52
|
+
console.error("Failed to save font preference:", err);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function updateThemeMode(mode: ThemeMode): void {
|
|
57
|
+
settings.themeMode = mode;
|
|
58
|
+
applyTheme(mode);
|
|
59
|
+
syncSystemPreference();
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
localStorage.setItem(THEME_STORAGE_KEY, mode);
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function initSettings(data?: { fontFamily?: string }): void {
|
|
67
|
+
if (data?.fontFamily) {
|
|
68
|
+
settings.fontFamily = data.fontFamily as FontFamily;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
applyTheme(settings.themeMode);
|
|
72
|
+
syncSystemPreference();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let mediaCleanup: (() => void) | undefined;
|
|
76
|
+
|
|
77
|
+
function syncSystemPreference(): void {
|
|
78
|
+
if (mediaCleanup) {
|
|
79
|
+
mediaCleanup();
|
|
80
|
+
mediaCleanup = undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (settings.themeMode !== ThemeModes.SYSTEM) return;
|
|
84
|
+
|
|
85
|
+
const mq = window.matchMedia(DARK_MQ);
|
|
86
|
+
const handler = () => applyTheme(ThemeModes.SYSTEM);
|
|
87
|
+
|
|
88
|
+
mq.addEventListener("change", handler);
|
|
89
|
+
mediaCleanup = () => mq.removeEventListener("change", handler);
|
|
90
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bindingsEqual,
|
|
3
|
+
DEFAULT_SHORTCUTS,
|
|
4
|
+
resolveShortcuts,
|
|
5
|
+
type ShortcutDefinition,
|
|
6
|
+
} from "../lib/shortcut-registry";
|
|
7
|
+
import type { KeybindingOverride, ShortcutBinding } from "../schema";
|
|
8
|
+
|
|
9
|
+
export const shortcutState = $state({
|
|
10
|
+
shortcuts: DEFAULT_SHORTCUTS as ShortcutDefinition[],
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export function initShortcuts(overrides: KeybindingOverride[]): void {
|
|
14
|
+
shortcutState.shortcuts = resolveShortcuts(overrides);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function updateBinding(
|
|
18
|
+
id: string,
|
|
19
|
+
binding: ShortcutBinding,
|
|
20
|
+
): Promise<void> {
|
|
21
|
+
const target = shortcutState.shortcuts.find((s) => s.id === id);
|
|
22
|
+
const conflict = shortcutState.shortcuts.find(
|
|
23
|
+
(s) => s.id !== id && s.enabled && bindingsEqual(s.binding, binding),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const updated = shortcutState.shortcuts.map((s) => {
|
|
27
|
+
if (s.id === id) return { ...s, binding };
|
|
28
|
+
if (conflict && s.id === conflict.id) {
|
|
29
|
+
// Only swap bindings when the target is enabled; otherwise
|
|
30
|
+
// just revert the conflicting shortcut to its default binding
|
|
31
|
+
// to avoid creating two enabled shortcuts on the same combo.
|
|
32
|
+
if (target?.enabled) {
|
|
33
|
+
return { ...s, binding: target.binding };
|
|
34
|
+
}
|
|
35
|
+
return { ...s, binding: s.defaultBinding };
|
|
36
|
+
}
|
|
37
|
+
return s;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
shortcutState.shortcuts = updated;
|
|
41
|
+
await persistOverrides(updated);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function toggleEnabled(id: string): Promise<void> {
|
|
45
|
+
const target = shortcutState.shortcuts.find((s) => s.id === id);
|
|
46
|
+
if (!target) return;
|
|
47
|
+
|
|
48
|
+
// When re-enabling, check for binding conflicts with other enabled shortcuts
|
|
49
|
+
if (!target.enabled) {
|
|
50
|
+
const conflict = shortcutState.shortcuts.find(
|
|
51
|
+
(s) =>
|
|
52
|
+
s.id !== id && s.enabled && bindingsEqual(s.binding, target.binding),
|
|
53
|
+
);
|
|
54
|
+
if (conflict) {
|
|
55
|
+
// Disable the conflicting shortcut to resolve the conflict
|
|
56
|
+
shortcutState.shortcuts = shortcutState.shortcuts.map((s) => {
|
|
57
|
+
if (s.id === id) return { ...s, enabled: true };
|
|
58
|
+
if (s.id === conflict.id) return { ...s, enabled: false };
|
|
59
|
+
return s;
|
|
60
|
+
});
|
|
61
|
+
await persistOverrides(shortcutState.shortcuts);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
shortcutState.shortcuts = shortcutState.shortcuts.map((s) =>
|
|
67
|
+
s.id === id ? { ...s, enabled: !s.enabled } : s,
|
|
68
|
+
);
|
|
69
|
+
await persistOverrides(shortcutState.shortcuts);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function resetToDefaults(): Promise<void> {
|
|
73
|
+
shortcutState.shortcuts = DEFAULT_SHORTCUTS.map((s) => ({ ...s }));
|
|
74
|
+
await persistOverrides(shortcutState.shortcuts);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function toOverrides(shortcuts: ShortcutDefinition[]): KeybindingOverride[] {
|
|
78
|
+
return shortcuts
|
|
79
|
+
.filter((s) => !s.enabled || !bindingsEqual(s.binding, s.defaultBinding))
|
|
80
|
+
.map((s) => ({
|
|
81
|
+
id: s.id,
|
|
82
|
+
binding: bindingsEqual(s.binding, s.defaultBinding)
|
|
83
|
+
? undefined
|
|
84
|
+
: s.binding,
|
|
85
|
+
enabled: s.enabled,
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function persistOverrides(
|
|
90
|
+
shortcuts: ShortcutDefinition[],
|
|
91
|
+
): Promise<void> {
|
|
92
|
+
try {
|
|
93
|
+
const response = await fetch("/api/settings", {
|
|
94
|
+
method: "PUT",
|
|
95
|
+
headers: { "Content-Type": "application/json" },
|
|
96
|
+
body: JSON.stringify({ keybindings: toOverrides(shortcuts) }),
|
|
97
|
+
});
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
throw new Error(`Failed to save keybindings: ${response.status}`);
|
|
100
|
+
}
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.error("Failed to save keybindings:", err);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const ui = $state({
|
|
2
|
+
hoveredCommentId: undefined as string | undefined,
|
|
3
|
+
activeCommentId: undefined as string | undefined,
|
|
4
|
+
});
|
|
5
|
+
|
|
6
|
+
export function setHoveredCommentId(id: string | undefined): void {
|
|
7
|
+
ui.hoveredCommentId = id;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function setActiveCommentId(id: string | undefined): void {
|
|
11
|
+
ui.activeCommentId = id;
|
|
12
|
+
}
|
package/src/template.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export interface TemplateOptions {
|
|
2
|
+
title: string;
|
|
3
|
+
cssPath: string;
|
|
4
|
+
jsPath: string;
|
|
5
|
+
documentHtml: string;
|
|
6
|
+
inlineData: object;
|
|
7
|
+
isDev: boolean;
|
|
8
|
+
fontFamily: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function safeJsonStringify(data: object): string {
|
|
12
|
+
return JSON.stringify(data).replace(/</g, "\\u003c");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function escapeHtml(str: string): string {
|
|
16
|
+
return str
|
|
17
|
+
.replace(/&/g, "&")
|
|
18
|
+
.replace(/</g, "<")
|
|
19
|
+
.replace(/>/g, ">")
|
|
20
|
+
.replace(/"/g, """)
|
|
21
|
+
.replace(/'/g, "'");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function escapeAttr(str: string): string {
|
|
25
|
+
return str.replace(/&/g, "&").replace(/"/g, """);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Sanitize server-rendered HTML to prevent XSS from raw HTML in markdown source.
|
|
30
|
+
* While readit is designed for local use with the user's own files, this
|
|
31
|
+
* provides defense-in-depth against untrusted markdown content.
|
|
32
|
+
*
|
|
33
|
+
* Uses a simple tag-stripping approach to remove dangerous elements while
|
|
34
|
+
* preserving the rendered content. The Go server uses bluemonday for the
|
|
35
|
+
* same purpose.
|
|
36
|
+
*/
|
|
37
|
+
function sanitizeHtml(html: string): string {
|
|
38
|
+
// Remove <script> tags and their content
|
|
39
|
+
let sanitized = html.replace(
|
|
40
|
+
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
|
|
41
|
+
"",
|
|
42
|
+
);
|
|
43
|
+
// Remove event handler attributes (onclick, onerror, onload, etc.)
|
|
44
|
+
sanitized = sanitized.replace(
|
|
45
|
+
/\s+on\w+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi,
|
|
46
|
+
"",
|
|
47
|
+
);
|
|
48
|
+
// Remove javascript: URLs
|
|
49
|
+
sanitized = sanitized.replace(
|
|
50
|
+
/\bhref\s*=\s*(?:"javascript:[^"]*"|'javascript:[^']*')/gi,
|
|
51
|
+
"",
|
|
52
|
+
);
|
|
53
|
+
sanitized = sanitized.replace(
|
|
54
|
+
/\bsrc\s*=\s*(?:"javascript:[^"]*"|'javascript:[^']*')/gi,
|
|
55
|
+
"",
|
|
56
|
+
);
|
|
57
|
+
return sanitized;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function renderTemplate(options: TemplateOptions): string {
|
|
61
|
+
const {
|
|
62
|
+
title,
|
|
63
|
+
cssPath,
|
|
64
|
+
jsPath,
|
|
65
|
+
documentHtml,
|
|
66
|
+
inlineData,
|
|
67
|
+
isDev,
|
|
68
|
+
fontFamily,
|
|
69
|
+
} = options;
|
|
70
|
+
|
|
71
|
+
const viteClient = isDev
|
|
72
|
+
? '<script type="module" src="http://127.0.0.1:24678/@vite/client"></script>'
|
|
73
|
+
: "";
|
|
74
|
+
|
|
75
|
+
const cssLink = cssPath
|
|
76
|
+
? `<link rel="stylesheet" href="${escapeAttr(cssPath)}">`
|
|
77
|
+
: "";
|
|
78
|
+
const proseClass = fontFamily === "sans-serif" ? "prose-sans" : "prose-serif";
|
|
79
|
+
|
|
80
|
+
return `<!DOCTYPE html>
|
|
81
|
+
<html lang="en">
|
|
82
|
+
<head>
|
|
83
|
+
<meta charset="UTF-8">
|
|
84
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
85
|
+
<title>readit — ${escapeHtml(title)}</title>
|
|
86
|
+
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📖</text></svg>">
|
|
87
|
+
<script>
|
|
88
|
+
(() => {
|
|
89
|
+
var t = localStorage.getItem("readit:theme");
|
|
90
|
+
var d = t === "dark" || (t !== "light" && matchMedia("(prefers-color-scheme: dark)").matches);
|
|
91
|
+
if (d) document.documentElement.classList.add("dark");
|
|
92
|
+
})();
|
|
93
|
+
</script>
|
|
94
|
+
${viteClient}
|
|
95
|
+
${cssLink}
|
|
96
|
+
</head>
|
|
97
|
+
<body class="min-h-screen">
|
|
98
|
+
<article id="document-content" class="prose ${proseClass}">${sanitizeHtml(documentHtml)}</article>
|
|
99
|
+
<div id="app"></div>
|
|
100
|
+
<script type="application/json" id="__readit">${safeJsonStringify(inlineData)}</script>
|
|
101
|
+
<script type="module" src="${escapeAttr(jsPath)}" defer></script>
|
|
102
|
+
</body>
|
|
103
|
+
</html>`;
|
|
104
|
+
}
|
package/src/test-setup.ts
CHANGED
|
@@ -1 +1,48 @@
|
|
|
1
1
|
import "@testing-library/jest-dom/vitest";
|
|
2
|
+
|
|
3
|
+
// Mock CSS Custom Highlight API for jsdom (not supported natively)
|
|
4
|
+
if (typeof globalThis.Highlight === "undefined") {
|
|
5
|
+
globalThis.Highlight = class Highlight {
|
|
6
|
+
_ranges: AbstractRange[];
|
|
7
|
+
priority = 0;
|
|
8
|
+
type: "highlight" | "spelling-error" | "grammar-error" = "highlight";
|
|
9
|
+
get size() {
|
|
10
|
+
return this._ranges.length;
|
|
11
|
+
}
|
|
12
|
+
constructor(...ranges: AbstractRange[]) {
|
|
13
|
+
this._ranges = ranges;
|
|
14
|
+
}
|
|
15
|
+
add(range: AbstractRange) {
|
|
16
|
+
this._ranges.push(range);
|
|
17
|
+
}
|
|
18
|
+
delete(range: AbstractRange) {
|
|
19
|
+
const idx = this._ranges.indexOf(range);
|
|
20
|
+
if (idx >= 0) {
|
|
21
|
+
this._ranges.splice(idx, 1);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
clear() {
|
|
27
|
+
this._ranges = [];
|
|
28
|
+
}
|
|
29
|
+
has(range: AbstractRange) {
|
|
30
|
+
return this._ranges.includes(range);
|
|
31
|
+
}
|
|
32
|
+
[Symbol.iterator]() {
|
|
33
|
+
return this._ranges[Symbol.iterator]();
|
|
34
|
+
}
|
|
35
|
+
} as unknown as typeof Highlight;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (typeof CSS === "undefined" || !CSS.highlights) {
|
|
39
|
+
const highlightsMap = new Map<string, Highlight>();
|
|
40
|
+
Object.defineProperty(globalThis, "CSS", {
|
|
41
|
+
value: {
|
|
42
|
+
...((globalThis as Record<string, unknown>).CSS ?? {}),
|
|
43
|
+
highlights: highlightsMap,
|
|
44
|
+
},
|
|
45
|
+
writable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
});
|
|
48
|
+
}
|
package/svelte.config.js
ADDED
package/tsconfig.json
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2022",
|
|
4
4
|
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
5
|
+
"types": ["node"],
|
|
5
6
|
"module": "ESNext",
|
|
6
7
|
"moduleResolution": "bundler",
|
|
7
8
|
"strict": true,
|
|
@@ -10,11 +11,10 @@
|
|
|
10
11
|
"resolveJsonModule": true,
|
|
11
12
|
"isolatedModules": true,
|
|
12
13
|
"noEmit": true,
|
|
13
|
-
"jsx": "react-jsx",
|
|
14
14
|
"allowImportingTsExtensions": true,
|
|
15
15
|
"noUnusedLocals": true,
|
|
16
16
|
"noUnusedParameters": true
|
|
17
17
|
},
|
|
18
18
|
"include": ["src"],
|
|
19
|
-
"exclude": ["node_modules", "dist"]
|
|
19
|
+
"exclude": ["node_modules", "dist", "vscode-readit"]
|
|
20
20
|
}
|
package/vite.config.ts
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
|
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
1
2
|
import tailwindcss from "@tailwindcss/vite";
|
|
2
|
-
import
|
|
3
|
-
|
|
3
|
+
import { createLogger, defineConfig } from "vite";
|
|
4
|
+
|
|
5
|
+
// Suppress lightningcss warnings about ::highlight() pseudo-element.
|
|
6
|
+
// The CSS Custom Highlight API is a valid standard; lightningcss doesn't
|
|
7
|
+
// recognise it yet (fix merged upstream, pending release).
|
|
8
|
+
const logger = createLogger();
|
|
9
|
+
const originalWarn = logger.warn.bind(logger);
|
|
10
|
+
logger.warn = (msg, options) => {
|
|
11
|
+
if (msg.includes("::highlight")) return;
|
|
12
|
+
originalWarn(msg, options);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Tailwind CSS v4 calls console.warn directly for lightningcss warnings
|
|
16
|
+
// (bypassing Vite's logger), so we also intercept that.
|
|
17
|
+
const originalConsoleWarn = console.warn.bind(console);
|
|
18
|
+
console.warn = (...args: unknown[]) => {
|
|
19
|
+
if (typeof args[0] === "string" && args[0].includes("::highlight")) return;
|
|
20
|
+
originalConsoleWarn(...args);
|
|
21
|
+
};
|
|
4
22
|
|
|
5
23
|
export default defineConfig({
|
|
6
|
-
|
|
24
|
+
customLogger: logger,
|
|
25
|
+
plugins: [tailwindcss(), svelte()],
|
|
7
26
|
server: {
|
|
8
27
|
port: 24678,
|
|
9
28
|
strictPort: true,
|
|
@@ -23,5 +42,6 @@ export default defineConfig({
|
|
|
23
42
|
build: {
|
|
24
43
|
outDir: "dist",
|
|
25
44
|
emptyOutDir: true,
|
|
45
|
+
manifest: true,
|
|
26
46
|
},
|
|
27
47
|
});
|