wiki-viewer 1.0.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.
Files changed (119) hide show
  1. package/.next/standalone/.github/workflows/publish.yml +37 -0
  2. package/.next/standalone/DESIGN.md +521 -0
  3. package/.next/standalone/README.md +283 -0
  4. package/.next/standalone/bin/wiki-viewer.js +158 -0
  5. package/.next/standalone/next.config.ts +11 -0
  6. package/.next/standalone/package.json +120 -0
  7. package/.next/standalone/pnpm-lock.yaml +5654 -0
  8. package/.next/standalone/pnpm-workspace.yaml +2 -0
  9. package/.next/standalone/postcss.config.mjs +2 -0
  10. package/.next/standalone/public/logo.svg +11 -0
  11. package/.next/standalone/public/public/logo.svg +11 -0
  12. package/.next/standalone/server.js +44 -0
  13. package/.next/standalone/src/app/api/app-proxy/[...path]/route.ts +247 -0
  14. package/.next/standalone/src/app/api/assets/[...path]/route.ts +70 -0
  15. package/.next/standalone/src/app/api/system/browse/route.ts +78 -0
  16. package/.next/standalone/src/app/api/system/clear-root/route.ts +9 -0
  17. package/.next/standalone/src/app/api/system/config/route.ts +10 -0
  18. package/.next/standalone/src/app/api/system/pins/route.ts +31 -0
  19. package/.next/standalone/src/app/api/system/reveal/route.ts +28 -0
  20. package/.next/standalone/src/app/api/system/root-status/route.ts +9 -0
  21. package/.next/standalone/src/app/api/system/set-root/route.ts +23 -0
  22. package/.next/standalone/src/app/api/upload/[...path]/route.ts +80 -0
  23. package/.next/standalone/src/app/api/wiki/app/route.ts +41 -0
  24. package/.next/standalone/src/app/api/wiki/content/route.ts +60 -0
  25. package/.next/standalone/src/app/api/wiki/file/route.ts +52 -0
  26. package/.next/standalone/src/app/api/wiki/folder/route.ts +26 -0
  27. package/.next/standalone/src/app/api/wiki/move/route.ts +45 -0
  28. package/.next/standalone/src/app/api/wiki/page/route.ts +84 -0
  29. package/.next/standalone/src/app/api/wiki/route.ts +98 -0
  30. package/.next/standalone/src/app/api/wiki/slugs/route.ts +50 -0
  31. package/.next/standalone/src/app/api/wiki/upload/route.ts +87 -0
  32. package/.next/standalone/src/app/api/wiki/watch/route.ts +86 -0
  33. package/.next/standalone/src/app/globals.css +716 -0
  34. package/.next/standalone/src/app/icon.svg +11 -0
  35. package/.next/standalone/src/app/layout.tsx +35 -0
  36. package/.next/standalone/src/app/page.tsx +1509 -0
  37. package/.next/standalone/src/components/confirm-dialog.tsx +56 -0
  38. package/.next/standalone/src/components/dir-picker.tsx +393 -0
  39. package/.next/standalone/src/components/editor/bubble-menu.tsx +286 -0
  40. package/.next/standalone/src/components/editor/callout-extension.ts +36 -0
  41. package/.next/standalone/src/components/editor/csv-viewer.tsx +335 -0
  42. package/.next/standalone/src/components/editor/editor-toolbar.tsx +564 -0
  43. package/.next/standalone/src/components/editor/editor.tsx +633 -0
  44. package/.next/standalone/src/components/editor/extensions/drag-handle.ts +325 -0
  45. package/.next/standalone/src/components/editor/extensions/heading-anchors.ts +55 -0
  46. package/.next/standalone/src/components/editor/extensions/resizable-image.tsx +159 -0
  47. package/.next/standalone/src/components/editor/extensions.ts +148 -0
  48. package/.next/standalone/src/components/editor/file-fallback-viewer.tsx +74 -0
  49. package/.next/standalone/src/components/editor/folder-index.tsx +235 -0
  50. package/.next/standalone/src/components/editor/google-doc-viewer.tsx +73 -0
  51. package/.next/standalone/src/components/editor/image-viewer.tsx +56 -0
  52. package/.next/standalone/src/components/editor/link-popover.tsx +111 -0
  53. package/.next/standalone/src/components/editor/media-popover.tsx +211 -0
  54. package/.next/standalone/src/components/editor/media-viewer.tsx +69 -0
  55. package/.next/standalone/src/components/editor/mermaid-viewer.tsx +285 -0
  56. package/.next/standalone/src/components/editor/node-app-viewer.tsx +269 -0
  57. package/.next/standalone/src/components/editor/notebook-viewer.tsx +370 -0
  58. package/.next/standalone/src/components/editor/office/docx-viewer.tsx +92 -0
  59. package/.next/standalone/src/components/editor/office/office-chrome.tsx +82 -0
  60. package/.next/standalone/src/components/editor/office/pptx-viewer.tsx +103 -0
  61. package/.next/standalone/src/components/editor/office/xlsx-viewer.tsx +116 -0
  62. package/.next/standalone/src/components/editor/pdf-viewer.tsx +31 -0
  63. package/.next/standalone/src/components/editor/slash-commands.tsx +454 -0
  64. package/.next/standalone/src/components/editor/source-viewer.tsx +212 -0
  65. package/.next/standalone/src/components/editor/table-menu.tsx +232 -0
  66. package/.next/standalone/src/components/editor/website-viewer.tsx +74 -0
  67. package/.next/standalone/src/components/editor/wiki-link-create-dialog.tsx +188 -0
  68. package/.next/standalone/src/components/editor/wiki-link-decorator.ts +79 -0
  69. package/.next/standalone/src/components/editor/wiki-link-extension.ts +120 -0
  70. package/.next/standalone/src/components/editor/wiki-link-picker.tsx +250 -0
  71. package/.next/standalone/src/components/layout/viewer-toolbar.tsx +54 -0
  72. package/.next/standalone/src/components/theme-provider.tsx +16 -0
  73. package/.next/standalone/src/components/theme-toggle.tsx +24 -0
  74. package/.next/standalone/src/components/ui/badge.tsx +35 -0
  75. package/.next/standalone/src/components/ui/button.tsx +56 -0
  76. package/.next/standalone/src/components/ui/card.tsx +63 -0
  77. package/.next/standalone/src/components/ui/collapsible.tsx +9 -0
  78. package/.next/standalone/src/components/ui/command.tsx +150 -0
  79. package/.next/standalone/src/components/ui/context-menu.tsx +191 -0
  80. package/.next/standalone/src/components/ui/dialog.tsx +119 -0
  81. package/.next/standalone/src/components/ui/dropdown-menu.tsx +194 -0
  82. package/.next/standalone/src/components/ui/input.tsx +22 -0
  83. package/.next/standalone/src/components/ui/label.tsx +25 -0
  84. package/.next/standalone/src/components/ui/popover.tsx +33 -0
  85. package/.next/standalone/src/components/ui/scroll-area.tsx +47 -0
  86. package/.next/standalone/src/components/ui/select.tsx +156 -0
  87. package/.next/standalone/src/components/ui/separator.tsx +30 -0
  88. package/.next/standalone/src/components/ui/sheet.tsx +140 -0
  89. package/.next/standalone/src/components/ui/skeleton.tsx +15 -0
  90. package/.next/standalone/src/components/ui/switch.tsx +28 -0
  91. package/.next/standalone/src/components/ui/tabs.tsx +54 -0
  92. package/.next/standalone/src/components/ui/textarea.tsx +21 -0
  93. package/.next/standalone/src/components/ui/tip.tsx +23 -0
  94. package/.next/standalone/src/components/ui/toggle-group.tsx +41 -0
  95. package/.next/standalone/src/components/ui/tooltip.tsx +27 -0
  96. package/.next/standalone/src/components/wiki/frontmatter-header.tsx +164 -0
  97. package/.next/standalone/src/lib/app-runner.ts +231 -0
  98. package/.next/standalone/src/lib/cabinets/tree.ts +75 -0
  99. package/.next/standalone/src/lib/config.ts +32 -0
  100. package/.next/standalone/src/lib/embeds/detect.ts +142 -0
  101. package/.next/standalone/src/lib/google/detect.ts +107 -0
  102. package/.next/standalone/src/lib/markdown/parse-frontmatter.ts +84 -0
  103. package/.next/standalone/src/lib/markdown/remark-wikilinks.ts +100 -0
  104. package/.next/standalone/src/lib/markdown/to-html.ts +168 -0
  105. package/.next/standalone/src/lib/markdown/to-markdown.ts +238 -0
  106. package/.next/standalone/src/lib/root-dir.ts +35 -0
  107. package/.next/standalone/src/lib/toast.ts +9 -0
  108. package/.next/standalone/src/lib/utils.ts +6 -0
  109. package/.next/standalone/src/lib/wiki-helpers.ts +36 -0
  110. package/.next/standalone/src/stores/ai-panel-store.ts +23 -0
  111. package/.next/standalone/src/stores/editor-store.ts +244 -0
  112. package/.next/standalone/src/stores/tree-store.ts +52 -0
  113. package/.next/standalone/src/stores/wiki-slugs-store.ts +74 -0
  114. package/.next/standalone/src/types/index.ts +47 -0
  115. package/.next/standalone/tailwind.config.ts +201 -0
  116. package/.next/standalone/tsconfig.json +41 -0
  117. package/README.md +283 -0
  118. package/bin/wiki-viewer.js +158 -0
  119. package/package.json +120 -0
@@ -0,0 +1,325 @@
1
+ import { Extension } from "@tiptap/core";
2
+ import {
3
+ NodeSelection,
4
+ Plugin,
5
+ PluginKey,
6
+ TextSelection,
7
+ type Transaction,
8
+ } from "@tiptap/pm/state";
9
+ import type { EditorView } from "@tiptap/pm/view";
10
+
11
+ /**
12
+ * Move the top-level block containing the current selection up or down
13
+ * by one sibling. Returns true if the doc was mutated. Used for keyboard
14
+ * reorder (Alt+Shift+Up/Down) so non-mouse users get parity with the
15
+ * drag handle (audit #102).
16
+ */
17
+ function moveCurrentBlock(
18
+ state: EditorView["state"],
19
+ dispatch: ((tr: Transaction) => void) | undefined,
20
+ direction: "up" | "down",
21
+ ): boolean {
22
+ const { selection, doc } = state;
23
+ // Resolve the top-level block that holds the current selection. We walk
24
+ // up to depth 1 because doc → top-level-block → … is the layout we care
25
+ // about; nested list items still move as siblings of their parent block.
26
+ let $pos = doc.resolve(selection.from);
27
+ while ($pos.depth > 1) {
28
+ $pos = doc.resolve($pos.before($pos.depth));
29
+ }
30
+ if ($pos.depth === 0) return false;
31
+
32
+ const blockPos = $pos.before(1);
33
+ const block = doc.nodeAt(blockPos);
34
+ if (!block) return false;
35
+
36
+ const parent = doc;
37
+ const indexInParent = $pos.index(0);
38
+ const siblingIndex =
39
+ direction === "up" ? indexInParent - 1 : indexInParent + 1;
40
+ if (siblingIndex < 0 || siblingIndex >= parent.childCount) return false;
41
+
42
+ const sibling = parent.child(siblingIndex);
43
+ let tr = state.tr;
44
+
45
+ // Remove the block, then re-insert it on the other side of the sibling.
46
+ // Computing the insertion target *before* the cut keeps positions stable.
47
+ const blockEnd = blockPos + block.nodeSize;
48
+ const siblingStart =
49
+ direction === "up" ? blockPos - sibling.nodeSize : blockEnd;
50
+ const siblingEnd =
51
+ direction === "up" ? blockPos : blockEnd + sibling.nodeSize;
52
+
53
+ if (direction === "up") {
54
+ tr = tr.delete(blockPos, blockEnd);
55
+ tr = tr.insert(siblingStart, block);
56
+ // After re-insert the block lives at siblingStart; restore selection on it.
57
+ tr = tr.setSelection(NodeSelection.create(tr.doc, siblingStart));
58
+ } else {
59
+ tr = tr.delete(blockPos, blockEnd);
60
+ // After deletion the sibling shifts left by block.nodeSize, so the
61
+ // insertion target is siblingEnd - block.nodeSize.
62
+ const insertAt = siblingEnd - block.nodeSize;
63
+ tr = tr.insert(insertAt, block);
64
+ tr = tr.setSelection(NodeSelection.create(tr.doc, insertAt));
65
+ }
66
+
67
+ if (dispatch) dispatch(tr.scrollIntoView());
68
+ return true;
69
+ }
70
+
71
+ const HANDLE_ID = "cabinet-drag-handle";
72
+ const ADD_BTN_ID = "cabinet-gutter-add";
73
+
74
+ function getOrCreateAddButton(): HTMLButtonElement {
75
+ let el = document.getElementById(ADD_BTN_ID) as HTMLButtonElement | null;
76
+ if (!el) {
77
+ el = document.createElement("button");
78
+ el.id = ADD_BTN_ID;
79
+ el.type = "button";
80
+ el.setAttribute("aria-label", "Add block");
81
+ el.title = "Add block";
82
+ el.innerHTML = `<svg width="10" height="10" viewBox="0 0 10 10" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 1V9M1 5H9" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>`;
83
+ Object.assign(el.style, {
84
+ position: "absolute",
85
+ display: "none",
86
+ alignItems: "center",
87
+ justifyContent: "center",
88
+ width: "18px",
89
+ height: "18px",
90
+ cursor: "pointer",
91
+ borderRadius: "4px",
92
+ color: "var(--muted-foreground)",
93
+ opacity: "0.55",
94
+ zIndex: "40",
95
+ transition: "opacity 120ms ease, background 120ms ease",
96
+ background: "transparent",
97
+ border: "none",
98
+ padding: "0",
99
+ } as Partial<CSSStyleDeclaration>);
100
+ el.addEventListener("mouseenter", () => {
101
+ if (!el) return;
102
+ el.style.opacity = "1";
103
+ el.style.background = "var(--muted)";
104
+ });
105
+ el.addEventListener("mouseleave", () => {
106
+ if (!el) return;
107
+ el.style.opacity = "0.55";
108
+ el.style.background = "transparent";
109
+ });
110
+ document.body.appendChild(el);
111
+ }
112
+ return el;
113
+ }
114
+
115
+ function findBlockAt(view: EditorView, coords: { left: number; top: number }) {
116
+ const pos = view.posAtCoords(coords);
117
+ if (!pos) return null;
118
+ let $pos = view.state.doc.resolve(pos.inside >= 0 ? pos.inside : pos.pos);
119
+ while ($pos.depth > 0 && !$pos.parent.type.isBlock) {
120
+ $pos = view.state.doc.resolve($pos.before());
121
+ }
122
+ // Walk up until we find a top-level child of the doc
123
+ let depth = $pos.depth;
124
+ while (depth > 1) {
125
+ const parent = view.state.doc.resolve($pos.before(depth)).parent;
126
+ if (parent.type.name === "doc") break;
127
+ depth -= 1;
128
+ }
129
+ const nodePos = depth === 0 ? 0 : $pos.before(Math.max(depth, 1));
130
+ const node = view.state.doc.nodeAt(nodePos);
131
+ if (!node) return null;
132
+ const dom = view.nodeDOM(nodePos) as HTMLElement | null;
133
+ return { pos: nodePos, node, dom };
134
+ }
135
+
136
+ function getOrCreateHandle(): HTMLDivElement {
137
+ let el = document.getElementById(HANDLE_ID) as HTMLDivElement | null;
138
+ if (!el) {
139
+ el = document.createElement("div");
140
+ el.id = HANDLE_ID;
141
+ el.setAttribute("data-drag-handle", "true");
142
+ el.draggable = true;
143
+ el.innerHTML = `<svg width="10" height="16" viewBox="0 0 10 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><circle cx="2.5" cy="3" r="1.2"/><circle cx="2.5" cy="8" r="1.2"/><circle cx="2.5" cy="13" r="1.2"/><circle cx="7.5" cy="3" r="1.2"/><circle cx="7.5" cy="8" r="1.2"/><circle cx="7.5" cy="13" r="1.2"/></svg>`;
144
+ Object.assign(el.style, {
145
+ position: "absolute",
146
+ display: "none",
147
+ cursor: "grab",
148
+ padding: "2px 4px",
149
+ borderRadius: "4px",
150
+ color: "var(--muted-foreground)",
151
+ opacity: "0.55",
152
+ zIndex: "40",
153
+ userSelect: "none",
154
+ transition: "opacity 120ms ease",
155
+ } as CSSStyleDeclaration);
156
+ el.addEventListener("mouseenter", () => {
157
+ if (el) el.style.opacity = "1";
158
+ });
159
+ el.addEventListener("mouseleave", () => {
160
+ if (el) el.style.opacity = "0.55";
161
+ });
162
+ document.body.appendChild(el);
163
+ }
164
+ return el;
165
+ }
166
+
167
+ export const DragHandle = Extension.create({
168
+ name: "dragHandle",
169
+
170
+ addKeyboardShortcuts() {
171
+ // Audit #102: drag handle is mouse-only. Add Alt+Shift+ArrowUp /
172
+ // Alt+Shift+ArrowDown so keyboard users can reorder blocks too.
173
+ return {
174
+ "Mod-Alt-ArrowUp": ({ editor }) =>
175
+ moveCurrentBlock(editor.state, editor.view.dispatch, "up"),
176
+ "Mod-Alt-ArrowDown": ({ editor }) =>
177
+ moveCurrentBlock(editor.state, editor.view.dispatch, "down"),
178
+ "Alt-Shift-ArrowUp": ({ editor }) =>
179
+ moveCurrentBlock(editor.state, editor.view.dispatch, "up"),
180
+ "Alt-Shift-ArrowDown": ({ editor }) =>
181
+ moveCurrentBlock(editor.state, editor.view.dispatch, "down"),
182
+ };
183
+ },
184
+
185
+ addProseMirrorPlugins() {
186
+ let currentBlock: {
187
+ pos: number;
188
+ node: { nodeSize: number };
189
+ dom: HTMLElement;
190
+ } | null = null;
191
+
192
+ const handle = typeof document !== "undefined" ? getOrCreateHandle() : null;
193
+ const addBtn =
194
+ typeof document !== "undefined" ? getOrCreateAddButton() : null;
195
+
196
+ const hide = () => {
197
+ if (handle) handle.style.display = "none";
198
+ if (addBtn) addBtn.style.display = "none";
199
+ currentBlock = null;
200
+ };
201
+
202
+ return [
203
+ new Plugin({
204
+ key: new PluginKey("cabinetDragHandle"),
205
+ view: (view) => {
206
+ if (!handle) return { destroy: () => {} };
207
+
208
+ const onMouseMove = (event: MouseEvent) => {
209
+ if (!view.editable) return;
210
+ const rect = view.dom.getBoundingClientRect();
211
+ if (
212
+ event.clientX < rect.left - 60 ||
213
+ event.clientX > rect.right + 60 ||
214
+ event.clientY < rect.top ||
215
+ event.clientY > rect.bottom
216
+ ) {
217
+ hide();
218
+ return;
219
+ }
220
+ // Probe inside the editor with clientX clamped to content
221
+ const probeX = Math.max(
222
+ rect.left + 20,
223
+ Math.min(rect.right - 20, event.clientX),
224
+ );
225
+ const block = findBlockAt(view, {
226
+ left: probeX,
227
+ top: event.clientY,
228
+ });
229
+ if (!block?.dom || !(block.dom instanceof HTMLElement)) {
230
+ hide();
231
+ return;
232
+ }
233
+ currentBlock = block as typeof currentBlock;
234
+ const domRect = block.dom.getBoundingClientRect();
235
+ handle.style.display = "flex";
236
+ handle.style.top = `${window.scrollY + domRect.top + 4}px`;
237
+ handle.style.left = `${window.scrollX + domRect.left - 22}px`;
238
+ if (addBtn) {
239
+ addBtn.style.display = "flex";
240
+ addBtn.style.top = `${window.scrollY + domRect.top + 4}px`;
241
+ addBtn.style.left = `${window.scrollX + domRect.left - 44}px`;
242
+ }
243
+ };
244
+
245
+ const onMouseLeave = () => hide();
246
+
247
+ const onAddClick = () => {
248
+ if (!currentBlock) return;
249
+ // Insert a new empty paragraph after the current block, then open slash menu
250
+ const afterPos = currentBlock.pos + currentBlock.node.nodeSize;
251
+ const insertable = afterPos <= view.state.doc.content.size;
252
+ const tr = view.state.tr;
253
+ if (insertable) {
254
+ // Place cursor at end of current block content (before node closing mark)
255
+ const endContent = afterPos - 1;
256
+ const sel = TextSelection.create(
257
+ view.state.doc,
258
+ Math.min(endContent, view.state.doc.content.size),
259
+ );
260
+ view.dispatch(tr.setSelection(sel));
261
+ }
262
+ view.focus();
263
+ // Split the block to create a new paragraph, then trigger slash menu
264
+ const splitTr = view.state.tr.split(view.state.selection.from);
265
+ view.dispatch(splitTr);
266
+ // Dispatch on view.dom so event.target is the ProseMirror element;
267
+ // this lets the global "/" hotkey guard (isEditableTarget) skip it
268
+ // while the slash-commands capture listener on window still fires.
269
+ view.dom.dispatchEvent(
270
+ new KeyboardEvent("keydown", {
271
+ key: "/",
272
+ bubbles: true,
273
+ cancelable: true,
274
+ }),
275
+ );
276
+ };
277
+
278
+ const onDragStart = (event: DragEvent) => {
279
+ if (!currentBlock || !event.dataTransfer) return;
280
+ const { pos, dom } = currentBlock;
281
+
282
+ // Select the block so PM treats it as the drag source
283
+ const tr = view.state.tr.setSelection(
284
+ NodeSelection.create(view.state.doc, pos),
285
+ );
286
+ view.dispatch(tr);
287
+
288
+ const slice = view.state.selection.content();
289
+ // Serialize slice content to HTML for external drop targets
290
+ const tmp = document.createElement("div");
291
+ tmp.appendChild(
292
+ view
293
+ .someProp("clipboardSerializer")
294
+ ?.serializeFragment(slice.content) ??
295
+ document.createElement("div"),
296
+ );
297
+ event.dataTransfer.clearData();
298
+ event.dataTransfer.setData("text/html", tmp.innerHTML);
299
+ event.dataTransfer.setData("text/plain", dom.textContent ?? "");
300
+ event.dataTransfer.effectAllowed = "copyMove";
301
+ event.dataTransfer.setDragImage(dom, 0, 0);
302
+
303
+ // Hand PM the slice so its built-in drop handler performs the move
304
+ view.dragging = { slice, move: true };
305
+ };
306
+
307
+ window.addEventListener("mousemove", onMouseMove);
308
+ view.dom.addEventListener("mouseleave", onMouseLeave);
309
+ handle.addEventListener("dragstart", onDragStart);
310
+ if (addBtn) addBtn.addEventListener("click", onAddClick);
311
+
312
+ return {
313
+ destroy() {
314
+ window.removeEventListener("mousemove", onMouseMove);
315
+ view.dom.removeEventListener("mouseleave", onMouseLeave);
316
+ handle.removeEventListener("dragstart", onDragStart);
317
+ if (addBtn) addBtn.removeEventListener("click", onAddClick);
318
+ hide();
319
+ },
320
+ };
321
+ },
322
+ }),
323
+ ];
324
+ },
325
+ });
@@ -0,0 +1,55 @@
1
+ import { Extension } from "@tiptap/core";
2
+ import type { Node } from "@tiptap/pm/model";
3
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
4
+ import { Decoration, DecorationSet } from "@tiptap/pm/view";
5
+
6
+ function slugify(text: string): string {
7
+ return text
8
+ .toLowerCase()
9
+ .replace(/[^\w\s-]/g, "")
10
+ .trim()
11
+ .replace(/\s+/g, "-")
12
+ .replace(/-+/g, "-");
13
+ }
14
+
15
+ const HEADING_ANCHOR_KEY = new PluginKey("headingAnchors");
16
+
17
+ function buildDecorations(doc: Node): DecorationSet {
18
+ const decos: Decoration[] = [];
19
+ const seen = new Map<string, number>();
20
+ doc.descendants((node, pos) => {
21
+ if (node.type.name !== "heading") return;
22
+ const base = slugify(node.textContent);
23
+ if (!base) return;
24
+ const count = seen.get(base) ?? 0;
25
+ seen.set(base, count + 1);
26
+ const id = count === 0 ? base : `${base}-${count}`;
27
+ decos.push(Decoration.node(pos, pos + node.nodeSize, { id }));
28
+ });
29
+ return DecorationSet.create(doc, decos);
30
+ }
31
+
32
+ export const HeadingAnchors = Extension.create({
33
+ name: "headingAnchors",
34
+
35
+ addProseMirrorPlugins() {
36
+ return [
37
+ new Plugin({
38
+ key: HEADING_ANCHOR_KEY,
39
+ state: {
40
+ init(_, { doc }) {
41
+ return buildDecorations(doc);
42
+ },
43
+ apply(tr, old) {
44
+ return tr.docChanged ? buildDecorations(tr.doc) : old;
45
+ },
46
+ },
47
+ props: {
48
+ decorations(state) {
49
+ return HEADING_ANCHOR_KEY.getState(state) as DecorationSet;
50
+ },
51
+ },
52
+ }),
53
+ ];
54
+ },
55
+ });
@@ -0,0 +1,159 @@
1
+ "use client";
2
+
3
+ import Image from "@tiptap/extension-image";
4
+ import {
5
+ type NodeViewProps,
6
+ NodeViewWrapper,
7
+ ReactNodeViewRenderer,
8
+ } from "@tiptap/react";
9
+ import { useCallback, useRef, useState } from "react";
10
+
11
+ interface ImageAttrs {
12
+ src: string;
13
+ alt?: string | null;
14
+ title?: string | null;
15
+ width?: string | number | null;
16
+ align?: "left" | "center" | "right" | null;
17
+ }
18
+
19
+ function ResizableImageComponent(props: NodeViewProps) {
20
+ const { node, updateAttributes, selected, editor } = props;
21
+ const attrs = node.attrs as ImageAttrs;
22
+ const imgRef = useRef<HTMLImageElement | null>(null);
23
+ const wrapperRef = useRef<HTMLDivElement | null>(null);
24
+ const [liveWidth, setLiveWidth] = useState<number | null>(null);
25
+
26
+ const align = attrs.align ?? "center";
27
+
28
+ const beginResize = useCallback(
29
+ (e: React.PointerEvent<HTMLDivElement>, anchor: "left" | "right") => {
30
+ if (!editor.isEditable) return;
31
+ e.preventDefault();
32
+ e.stopPropagation();
33
+ const img = imgRef.current;
34
+ const wrap = wrapperRef.current;
35
+ if (!img || !wrap) return;
36
+
37
+ const startX = e.clientX;
38
+ const startWidth = img.getBoundingClientRect().width;
39
+ const containerWidth =
40
+ wrap.parentElement?.getBoundingClientRect().width ?? 800;
41
+
42
+ const onMove = (ev: PointerEvent) => {
43
+ const delta =
44
+ anchor === "right" ? ev.clientX - startX : startX - ev.clientX;
45
+ const next = Math.max(80, Math.min(containerWidth, startWidth + delta));
46
+ setLiveWidth(next);
47
+ };
48
+ const onUp = () => {
49
+ window.removeEventListener("pointermove", onMove);
50
+ window.removeEventListener("pointerup", onUp);
51
+ setLiveWidth((curr) => {
52
+ if (curr != null) {
53
+ updateAttributes({ width: Math.round(curr) });
54
+ }
55
+ return null;
56
+ });
57
+ };
58
+ window.addEventListener("pointermove", onMove);
59
+ window.addEventListener("pointerup", onUp);
60
+ },
61
+ [editor.isEditable, updateAttributes],
62
+ );
63
+
64
+ const widthStyle = (() => {
65
+ if (liveWidth != null) return `${Math.round(liveWidth)}px`;
66
+ if (typeof attrs.width === "number") return `${attrs.width}px`;
67
+ if (typeof attrs.width === "string" && attrs.width) return attrs.width;
68
+ return undefined;
69
+ })();
70
+
71
+ const alignClass =
72
+ align === "left" ? "mr-auto" : align === "right" ? "ml-auto" : "mx-auto";
73
+
74
+ return (
75
+ <NodeViewWrapper
76
+ as="div"
77
+ className={`resizable-image my-3 ${alignClass}`}
78
+ data-align={align}
79
+ style={{ width: widthStyle ?? "fit-content", maxWidth: "100%" }}
80
+ >
81
+ <div
82
+ ref={wrapperRef}
83
+ className={`relative group inline-block max-w-full ${selected ? "ring-2 ring-primary rounded-md" : ""}`}
84
+ contentEditable={false}
85
+ >
86
+ <img
87
+ ref={imgRef}
88
+ src={attrs.src}
89
+ alt={attrs.alt ?? ""}
90
+ title={attrs.title ?? undefined}
91
+ className="block rounded-md max-w-full h-auto"
92
+ style={{ width: widthStyle }}
93
+ draggable={false}
94
+ />
95
+ {editor.isEditable && (
96
+ <>
97
+ <div
98
+ aria-label="Resize from left"
99
+ onPointerDown={(e) => beginResize(e, "left")}
100
+ className="absolute left-0 top-0 bottom-0 w-2 cursor-ew-resize opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center"
101
+ >
102
+ <div className="w-1 h-8 bg-card border border-border rounded-full shadow" />
103
+ </div>
104
+ <div
105
+ aria-label="Resize from right"
106
+ onPointerDown={(e) => beginResize(e, "right")}
107
+ className="absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center"
108
+ >
109
+ <div className="w-1 h-8 bg-card border border-border rounded-full shadow" />
110
+ </div>
111
+ {liveWidth != null && (
112
+ <div className="absolute top-1 right-1 text-[10px] px-1.5 py-0.5 rounded bg-primary/80 text-primary-foreground font-mono">
113
+ {Math.round(liveWidth)}px
114
+ </div>
115
+ )}
116
+ </>
117
+ )}
118
+ </div>
119
+ </NodeViewWrapper>
120
+ );
121
+ }
122
+
123
+ export const ResizableImage = Image.extend({
124
+ name: "image",
125
+ draggable: true,
126
+ addAttributes() {
127
+ return {
128
+ ...this.parent?.(),
129
+ width: {
130
+ default: null,
131
+ parseHTML: (element) => {
132
+ const w = element.getAttribute("width") ?? element.style.width;
133
+ if (!w) return null;
134
+ const match = /^(\d+)(px)?$/.exec(w);
135
+ return match ? Number(match[1]) : w;
136
+ },
137
+ renderHTML: (attributes: ImageAttrs) => {
138
+ if (!attributes.width) return {};
139
+ const w =
140
+ typeof attributes.width === "number"
141
+ ? `${attributes.width}px`
142
+ : attributes.width;
143
+ return { style: `width: ${w}` };
144
+ },
145
+ },
146
+ align: {
147
+ default: null,
148
+ parseHTML: (element) => element.getAttribute("data-align"),
149
+ renderHTML: (attributes: ImageAttrs) => {
150
+ if (!attributes.align) return {};
151
+ return { "data-align": attributes.align };
152
+ },
153
+ },
154
+ };
155
+ },
156
+ addNodeView() {
157
+ return ReactNodeViewRenderer(ResizableImageComponent);
158
+ },
159
+ });
@@ -0,0 +1,148 @@
1
+ import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
2
+ import Link from "@tiptap/extension-link";
3
+ import Placeholder from "@tiptap/extension-placeholder";
4
+ import Subscript from "@tiptap/extension-subscript";
5
+ import Superscript from "@tiptap/extension-superscript";
6
+ import { Table } from "@tiptap/extension-table";
7
+ import { TableCell } from "@tiptap/extension-table-cell";
8
+ import { TableHeader } from "@tiptap/extension-table-header";
9
+ import { TableRow } from "@tiptap/extension-table-row";
10
+ import TaskItem from "@tiptap/extension-task-item";
11
+ import TaskList from "@tiptap/extension-task-list";
12
+ import TextAlign from "@tiptap/extension-text-align";
13
+ import StarterKit from "@tiptap/starter-kit";
14
+ import bash from "highlight.js/lib/languages/bash";
15
+ import css from "highlight.js/lib/languages/css";
16
+ import go from "highlight.js/lib/languages/go";
17
+ import javascript from "highlight.js/lib/languages/javascript";
18
+ import json from "highlight.js/lib/languages/json";
19
+ import markdown from "highlight.js/lib/languages/markdown";
20
+ import python from "highlight.js/lib/languages/python";
21
+ import rust from "highlight.js/lib/languages/rust";
22
+ import shell from "highlight.js/lib/languages/shell";
23
+ import sql from "highlight.js/lib/languages/sql";
24
+ import typescript from "highlight.js/lib/languages/typescript";
25
+ import xml from "highlight.js/lib/languages/xml";
26
+ import yaml from "highlight.js/lib/languages/yaml";
27
+ import { createLowlight } from "lowlight";
28
+ import { CalloutExtension } from "./callout-extension";
29
+ import { DragHandle } from "./extensions/drag-handle";
30
+ import { HeadingAnchors } from "./extensions/heading-anchors";
31
+ import { ResizableImage } from "./extensions/resizable-image";
32
+ import { WikiLink } from "./wiki-link-extension";
33
+
34
+ // Curated language set: covers ~95% of real-world snippets. The full `common`
35
+ // import bundles 35+ language parsers (~70 kB gzipped) that users
36
+ // rarely touch — swapping it for this 13-language list cuts the editor chunk
37
+ // meaningfully. Add languages as demand grows.
38
+ const lowlight = createLowlight({
39
+ bash,
40
+ css,
41
+ go,
42
+ javascript,
43
+ json,
44
+ markdown,
45
+ python,
46
+ rust,
47
+ shell,
48
+ sql,
49
+ typescript,
50
+ xml,
51
+ yaml,
52
+ });
53
+
54
+ export const editorExtensions = [
55
+ StarterKit.configure({
56
+ heading: { levels: [1, 2, 3, 4] },
57
+ codeBlock: false, // replaced by CodeBlockLowlight
58
+ // StarterKit v3 bundles Link + Underline — we register our own customized
59
+ // versions below, so disable the bundled copies to avoid duplicate-extension warnings.
60
+ link: false,
61
+ underline: false,
62
+ }),
63
+ CodeBlockLowlight.configure({
64
+ lowlight,
65
+ HTMLAttributes: {
66
+ class: "rounded-md bg-muted p-4 font-mono text-sm",
67
+ },
68
+ }),
69
+ Placeholder.configure({
70
+ placeholder: "Start writing, or press '/' for commands...",
71
+ }),
72
+ ResizableImage.configure({
73
+ HTMLAttributes: {
74
+ class: "rounded-lg max-w-full",
75
+ },
76
+ allowBase64: false,
77
+ }),
78
+ Table.configure({
79
+ resizable: true,
80
+ lastColumnResizable: false,
81
+ HTMLAttributes: {
82
+ class: "border-collapse w-full",
83
+ },
84
+ }),
85
+ TableRow,
86
+ TableCell,
87
+ TableHeader,
88
+ TaskList.configure({
89
+ HTMLAttributes: {
90
+ class: "task-list",
91
+ },
92
+ }),
93
+ TaskItem.configure({
94
+ nested: true,
95
+ }),
96
+ Link.configure({
97
+ openOnClick: false, // we handle clicks ourselves in the editor
98
+ HTMLAttributes: {
99
+ class: "text-primary underline cursor-pointer",
100
+ },
101
+ }).extend({
102
+ parseHTML() {
103
+ return [
104
+ {
105
+ tag: 'a[href]:not([data-wiki-link="true"])',
106
+ },
107
+ ];
108
+ },
109
+ // Move the link shortcut off Mod-K — that key is owned by the global
110
+ // search palette everywhere in the app, including inside the editor.
111
+ addKeyboardShortcuts() {
112
+ return {
113
+ "Mod-e": () => {
114
+ const { state } = this.editor;
115
+ const { from, to } = state.selection;
116
+ if (from === to) return false;
117
+ const prevUrl = this.editor.getAttributes("link").href ?? "";
118
+ const url =
119
+ typeof window !== "undefined"
120
+ ? window.prompt("Link URL", prevUrl)
121
+ : null;
122
+ if (url === null) return false;
123
+ if (url === "") {
124
+ return this.editor
125
+ .chain()
126
+ .focus()
127
+ .extendMarkRange("link")
128
+ .unsetLink()
129
+ .run();
130
+ }
131
+ return this.editor
132
+ .chain()
133
+ .focus()
134
+ .extendMarkRange("link")
135
+ .setLink({ href: url })
136
+ .run();
137
+ },
138
+ };
139
+ },
140
+ }),
141
+ CalloutExtension,
142
+ TextAlign.configure({ types: ["heading", "paragraph"] }),
143
+ Subscript,
144
+ Superscript,
145
+ DragHandle,
146
+ HeadingAnchors,
147
+ WikiLink,
148
+ ];