@bendyline/squisq-editor-react 1.2.2 → 1.4.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 (106) hide show
  1. package/dist/EditorContext.d.ts +65 -1
  2. package/dist/EditorContext.d.ts.map +1 -1
  3. package/dist/EditorContext.js +31 -4
  4. package/dist/EditorContext.js.map +1 -1
  5. package/dist/EditorShell.d.ts +112 -2
  6. package/dist/EditorShell.d.ts.map +1 -1
  7. package/dist/EditorShell.js +95 -11
  8. package/dist/EditorShell.js.map +1 -1
  9. package/dist/ImageNodeView.d.ts.map +1 -1
  10. package/dist/ImageNodeView.js +12 -2
  11. package/dist/ImageNodeView.js.map +1 -1
  12. package/dist/MediaBin.d.ts +12 -1
  13. package/dist/MediaBin.d.ts.map +1 -1
  14. package/dist/MediaBin.js +29 -4
  15. package/dist/MediaBin.js.map +1 -1
  16. package/dist/MentionExtension.d.ts +22 -0
  17. package/dist/MentionExtension.d.ts.map +1 -0
  18. package/dist/MentionExtension.js +242 -0
  19. package/dist/MentionExtension.js.map +1 -0
  20. package/dist/RawEditor.d.ts +8 -1
  21. package/dist/RawEditor.d.ts.map +1 -1
  22. package/dist/RawEditor.js +167 -30
  23. package/dist/RawEditor.js.map +1 -1
  24. package/dist/TemplateAnnotation.d.ts.map +1 -1
  25. package/dist/TemplateAnnotation.js +4 -2
  26. package/dist/TemplateAnnotation.js.map +1 -1
  27. package/dist/Toolbar.d.ts +7 -1
  28. package/dist/Toolbar.d.ts.map +1 -1
  29. package/dist/Toolbar.js +57 -18
  30. package/dist/Toolbar.js.map +1 -1
  31. package/dist/Tooltip.d.ts +10 -0
  32. package/dist/Tooltip.d.ts.map +1 -0
  33. package/dist/Tooltip.js +104 -0
  34. package/dist/Tooltip.js.map +1 -0
  35. package/dist/ViewSwitcher.d.ts +1 -1
  36. package/dist/ViewSwitcher.d.ts.map +1 -1
  37. package/dist/ViewSwitcher.js +10 -4
  38. package/dist/ViewSwitcher.js.map +1 -1
  39. package/dist/WysiwygEditor.d.ts +13 -2
  40. package/dist/WysiwygEditor.d.ts.map +1 -1
  41. package/dist/WysiwygEditor.js +239 -4
  42. package/dist/WysiwygEditor.js.map +1 -1
  43. package/dist/__tests__/detectMarkdown.test.d.ts +2 -0
  44. package/dist/__tests__/detectMarkdown.test.d.ts.map +1 -0
  45. package/dist/__tests__/detectMarkdown.test.js +69 -0
  46. package/dist/__tests__/detectMarkdown.test.js.map +1 -0
  47. package/dist/__tests__/fileKind.test.d.ts +2 -0
  48. package/dist/__tests__/fileKind.test.d.ts.map +1 -0
  49. package/dist/__tests__/fileKind.test.js +81 -0
  50. package/dist/__tests__/fileKind.test.js.map +1 -0
  51. package/dist/__tests__/mediaAttachmentFlow.test.d.ts +2 -0
  52. package/dist/__tests__/mediaAttachmentFlow.test.d.ts.map +1 -0
  53. package/dist/__tests__/mediaAttachmentFlow.test.js +99 -0
  54. package/dist/__tests__/mediaAttachmentFlow.test.js.map +1 -0
  55. package/dist/__tests__/tiptapBridge.test.js +49 -0
  56. package/dist/__tests__/tiptapBridge.test.js.map +1 -1
  57. package/dist/__tests__/tiptapImageRoundTrip.test.d.ts +2 -0
  58. package/dist/__tests__/tiptapImageRoundTrip.test.d.ts.map +1 -0
  59. package/dist/__tests__/tiptapImageRoundTrip.test.js +68 -0
  60. package/dist/__tests__/tiptapImageRoundTrip.test.js.map +1 -0
  61. package/dist/detectMarkdown.d.ts +20 -0
  62. package/dist/detectMarkdown.d.ts.map +1 -0
  63. package/dist/detectMarkdown.js +61 -0
  64. package/dist/detectMarkdown.js.map +1 -0
  65. package/dist/fileKind.d.ts +30 -0
  66. package/dist/fileKind.d.ts.map +1 -0
  67. package/dist/fileKind.js +123 -0
  68. package/dist/fileKind.js.map +1 -0
  69. package/dist/hooks/useFileDrop.d.ts.map +1 -1
  70. package/dist/hooks/useFileDrop.js +9 -7
  71. package/dist/hooks/useFileDrop.js.map +1 -1
  72. package/dist/index.d.ts +4 -1
  73. package/dist/index.d.ts.map +1 -1
  74. package/dist/index.js +4 -0
  75. package/dist/index.js.map +1 -1
  76. package/dist/mediaDragMime.d.ts +17 -0
  77. package/dist/mediaDragMime.d.ts.map +1 -0
  78. package/dist/mediaDragMime.js +22 -0
  79. package/dist/mediaDragMime.js.map +1 -0
  80. package/dist/tiptapBridge.d.ts.map +1 -1
  81. package/dist/tiptapBridge.js +99 -6
  82. package/dist/tiptapBridge.js.map +1 -1
  83. package/package.json +9 -7
  84. package/src/EditorContext.tsx +106 -3
  85. package/src/EditorShell.tsx +313 -21
  86. package/src/ImageNodeView.tsx +15 -2
  87. package/src/MediaBin.tsx +45 -4
  88. package/src/MentionExtension.tsx +258 -0
  89. package/src/RawEditor.tsx +193 -37
  90. package/src/TemplateAnnotation.ts +4 -2
  91. package/src/Toolbar.tsx +111 -48
  92. package/src/Tooltip.tsx +124 -0
  93. package/src/ViewSwitcher.tsx +15 -5
  94. package/src/WysiwygEditor.tsx +270 -5
  95. package/src/__tests__/detectMarkdown.test.ts +88 -0
  96. package/src/__tests__/fileKind.test.ts +96 -0
  97. package/src/__tests__/mediaAttachmentFlow.test.ts +110 -0
  98. package/src/__tests__/tiptapBridge.test.ts +58 -0
  99. package/src/__tests__/tiptapImageRoundTrip.test.ts +73 -0
  100. package/src/detectMarkdown.ts +62 -0
  101. package/src/fileKind.ts +134 -0
  102. package/src/hooks/useFileDrop.ts +10 -6
  103. package/src/index.ts +11 -0
  104. package/src/mediaDragMime.ts +32 -0
  105. package/src/styles/editor.css +214 -8
  106. package/src/tiptapBridge.ts +107 -6
@@ -10,7 +10,7 @@
10
10
  * and code blocks.
11
11
  */
12
12
 
13
- import { useEffect, useRef } from 'react';
13
+ import { useEffect, useMemo, useRef } from 'react';
14
14
  import { useEditor, EditorContent } from '@tiptap/react';
15
15
  import StarterKit from '@tiptap/starter-kit';
16
16
  import Table from '@tiptap/extension-table';
@@ -23,7 +23,10 @@ import Placeholder from '@tiptap/extension-placeholder';
23
23
  import { HeadingWithTemplate } from './TemplateAnnotation';
24
24
  import { ImageWithMediaProvider } from './ImageNodeView';
25
25
  import { useEditorContext } from './EditorContext';
26
+ import { buildMentionExtension } from './MentionExtension';
26
27
  import { markdownToTiptap, tiptapToMarkdown } from './tiptapBridge';
28
+ import { looksLikeMarkdown } from './detectMarkdown';
29
+ import { SQUISQ_MEDIA_MIME, parseSquisqMediaPayload } from './mediaDragMime';
27
30
 
28
31
  // ── Frontmatter helpers ────────────────────────────────────────────
29
32
 
@@ -37,11 +40,44 @@ function stripFrontmatter(md: string): { body: string; frontmatter: string } {
37
40
  return { body: md.slice(m[0].length), frontmatter: m[0] };
38
41
  }
39
42
 
43
+ /**
44
+ * Rotating placeholder prompts shown when the editor is empty. One is
45
+ * picked at random per editor mount. Hosts can override by passing the
46
+ * `placeholder` prop with a fixed string.
47
+ */
48
+ const EMPTY_PROMPTS = [
49
+ 'Start typing your content, or drop images on top of me…',
50
+ 'Write anything — paste markdown, drag in images, or just start typing…',
51
+ 'Type away. Markdown syntax works too…',
52
+ 'Chapter 1 begins here…',
53
+ 'Once upon a time…',
54
+ 'A blank page. Exciting, isn\u2019t it?',
55
+ 'The first word is always the hardest…',
56
+ 'Plot twist: this is where it all starts…',
57
+ 'Write something the future you will thank you for…',
58
+ 'Begin at the beginning…',
59
+ ];
60
+
61
+ function pickEmptyPrompt(): string {
62
+ return EMPTY_PROMPTS[Math.floor(Math.random() * EMPTY_PROMPTS.length)];
63
+ }
64
+
40
65
  export interface WysiwygEditorProps {
41
- /** Placeholder text when editor is empty */
66
+ /**
67
+ * Placeholder text when the editor is empty. If omitted, one of several
68
+ * rotating prompts is picked at random on mount. Pass a fixed string to
69
+ * override with a host-specific call to action.
70
+ */
42
71
  placeholder?: string;
43
72
  /** Additional class name for the container */
44
73
  className?: string;
74
+ /**
75
+ * If set, a plain Enter keypress fires this callback instead of inserting
76
+ * a newline, and Cmd/Ctrl+Enter inserts a soft break. Chat-composer UX.
77
+ */
78
+ submitOnEnter?: () => void;
79
+ /** Disable Tiptap editing — renders content but blocks input. */
80
+ readOnly?: boolean;
45
81
  }
46
82
 
47
83
  /**
@@ -49,16 +85,42 @@ export interface WysiwygEditorProps {
49
85
  * Binds to the shared EditorContext for source synchronization.
50
86
  */
51
87
  export function WysiwygEditor({
52
- placeholder = 'Start typing your markdown…',
88
+ placeholder,
53
89
  className,
90
+ submitOnEnter,
91
+ readOnly = false,
54
92
  }: WysiwygEditorProps) {
55
- const { markdownSource, setMarkdownSource, setTiptapEditor } = useEditorContext();
93
+ const { markdownSource, setMarkdownSource, setTiptapEditor, mediaProvider, mentionProvider } =
94
+ useEditorContext();
95
+ // Keep a ref so the mention extension — created once at editor mount —
96
+ // always sees the latest provider. Swapping projects or gezels changes
97
+ // the provider without remounting the editor.
98
+ const mentionProviderRef = useRef(mentionProvider);
99
+ useEffect(() => {
100
+ mentionProviderRef.current = mentionProvider;
101
+ }, [mentionProvider]);
102
+ // Stable per mount: either the host-supplied string, or a random pick
103
+ // from EMPTY_PROMPTS. Re-renders don't reshuffle.
104
+ const resolvedPlaceholder = useMemo(() => placeholder ?? pickEmptyPrompt(), [placeholder]);
56
105
  const isExternalUpdate = useRef(false);
57
106
  const lastSourceRef = useRef(markdownSource);
107
+ // Keep a ref so the editor's drop/paste handlers (created once) always
108
+ // see the current MediaProvider without needing to recreate the editor.
109
+ const mediaProviderRef = useRef(mediaProvider);
110
+ useEffect(() => {
111
+ mediaProviderRef.current = mediaProvider;
112
+ }, [mediaProvider]);
58
113
  // Preserve frontmatter across edits — hidden from WYSIWYG but prepended on save
59
114
  const frontmatterRef = useRef(stripFrontmatter(markdownSource).frontmatter);
115
+ // Stash the latest submit callback so the editor's handleKeyDown (bound
116
+ // once at creation) always sees the current value.
117
+ const submitOnEnterRef = useRef(submitOnEnter);
118
+ useEffect(() => {
119
+ submitOnEnterRef.current = submitOnEnter;
120
+ }, [submitOnEnter]);
60
121
 
61
122
  const editor = useEditor({
123
+ editable: !readOnly,
62
124
  extensions: [
63
125
  StarterKit.configure({
64
126
  // Disable built-in heading; we use HeadingWithTemplate instead
@@ -75,7 +137,8 @@ export function WysiwygEditor({
75
137
  TaskList,
76
138
  TaskItem.configure({ nested: true }),
77
139
  ImageWithMediaProvider.configure({ inline: false }),
78
- Placeholder.configure({ placeholder }),
140
+ Placeholder.configure({ placeholder: resolvedPlaceholder }),
141
+ buildMentionExtension(() => mentionProviderRef.current),
79
142
  ],
80
143
  content: markdownToTiptap(stripFrontmatter(markdownSource).body),
81
144
  onUpdate: ({ editor: ed }) => {
@@ -91,6 +154,85 @@ export function WysiwygEditor({
91
154
  class: 'squisq-wysiwyg-editor',
92
155
  'data-testid': 'wysiwyg-editor',
93
156
  },
157
+ // Chat-composer mode: Enter commits via submitOnEnter(), Cmd/Ctrl+Enter
158
+ // inserts a soft break. When no callback is set, fall through to Tiptap's
159
+ // normal behavior (Enter = paragraph break, Shift+Enter = soft break).
160
+ handleKeyDown: (view, event) => {
161
+ if (event.key !== 'Enter' || !submitOnEnterRef.current) return false;
162
+ if (event.metaKey || event.ctrlKey) {
163
+ // User wants a newline. Insert a hard-break and stop propagation so
164
+ // we don't also create a new paragraph.
165
+ event.preventDefault();
166
+ view.dispatch(
167
+ view.state.tr.replaceSelectionWith(view.state.schema.nodes.hardBreak.create()),
168
+ );
169
+ return true;
170
+ }
171
+ if (event.shiftKey) {
172
+ // Preserve the conventional Shift+Enter soft break.
173
+ return false;
174
+ }
175
+ // Plain Enter — submit.
176
+ event.preventDefault();
177
+ submitOnEnterRef.current();
178
+ return true;
179
+ },
180
+ // When the clipboard's plain-text payload looks like markdown source,
181
+ // convert it via tiptapBridge before pasting. This applies even when
182
+ // the clipboard also contains HTML (most rich-text sources do), since
183
+ // the markdown-looking text is usually what the user actually wants.
184
+ // Without this, pasted markdown shows up as literal "# Heading" text
185
+ // instead of becoming a real heading.
186
+ handlePaste: (view, event) => {
187
+ const clipboard = event.clipboardData;
188
+ if (!clipboard) return false;
189
+
190
+ // Image files in the clipboard → upload via MediaProvider and insert
191
+ const imageFiles = filesFromClipboard(clipboard);
192
+ if (imageFiles.length > 0 && mediaProviderRef.current) {
193
+ event.preventDefault();
194
+ uploadAndInsertImages(view, imageFiles, mediaProviderRef.current);
195
+ return true;
196
+ }
197
+
198
+ const text = clipboard.getData('text/plain');
199
+ if (!text || !looksLikeMarkdown(text)) return false;
200
+ const html = markdownToTiptap(text);
201
+ if (!html) return false;
202
+ event.preventDefault();
203
+ view.pasteHTML(html);
204
+ return true;
205
+ },
206
+ // When image files are dropped onto the editor, upload them via the
207
+ // MediaProvider and insert <img> nodes referencing the relative paths.
208
+ // Also handles drags from the MediaBin, which reference existing
209
+ // entries via a custom MIME type and skip the upload step.
210
+ // Falls through to default handling for non-image drops or when no
211
+ // MediaProvider is available.
212
+ handleDrop: (view, event, _slice, _moved) => {
213
+ const dt = event.dataTransfer;
214
+ if (!dt) return false;
215
+
216
+ // In-app drag from the MediaBin — insert without uploading
217
+ const squisqRaw = dt.getData(SQUISQ_MEDIA_MIME);
218
+ if (squisqRaw) {
219
+ const payload = parseSquisqMediaPayload(squisqRaw);
220
+ if (payload && payload.mimeType.startsWith('image/')) {
221
+ event.preventDefault();
222
+ moveSelectionToDropPoint(view, event);
223
+ insertImageNode(view, payload.name, payload.alt);
224
+ return true;
225
+ }
226
+ }
227
+
228
+ const imageFiles = filesFromDataTransfer(dt);
229
+ if (imageFiles.length === 0 || !mediaProviderRef.current) return false;
230
+
231
+ event.preventDefault();
232
+ moveSelectionToDropPoint(view, event);
233
+ uploadAndInsertImages(view, imageFiles, mediaProviderRef.current);
234
+ return true;
235
+ },
94
236
  },
95
237
  });
96
238
 
@@ -102,6 +244,13 @@ export function WysiwygEditor({
102
244
  return () => setTiptapEditor(null);
103
245
  }, [editor, setTiptapEditor]);
104
246
 
247
+ // Tiptap reads `editable` only at creation; mirror later changes via
248
+ // setEditable so flipping readOnly from the host takes effect without
249
+ // remounting the editor.
250
+ useEffect(() => {
251
+ if (editor) editor.setEditable(!readOnly);
252
+ }, [editor, readOnly]);
253
+
105
254
  // Sync external changes into Tiptap
106
255
  useEffect(() => {
107
256
  if (!editor) return;
@@ -128,6 +277,122 @@ export function WysiwygEditor({
128
277
  );
129
278
  }
130
279
 
280
+ // ── Image drop / paste helpers ─────────────────────────────────────
281
+
282
+ /** Extract image File objects from a DataTransfer (drop event). */
283
+ function filesFromDataTransfer(dt: DataTransfer): File[] {
284
+ const files: File[] = [];
285
+ for (let i = 0; i < dt.files.length; i++) {
286
+ const file = dt.files[i];
287
+ if (file.type.startsWith('image/')) files.push(file);
288
+ }
289
+ return files;
290
+ }
291
+
292
+ /** Extract image File objects from a clipboard's items (paste event). */
293
+ function filesFromClipboard(clipboard: DataTransfer): File[] {
294
+ const files: File[] = [];
295
+ // clipboardData.items is the most reliable source for pasted images
296
+ if (clipboard.items) {
297
+ for (let i = 0; i < clipboard.items.length; i++) {
298
+ const item = clipboard.items[i];
299
+ if (item.kind === 'file' && item.type.startsWith('image/')) {
300
+ const file = item.getAsFile();
301
+ if (file) files.push(file);
302
+ }
303
+ }
304
+ }
305
+ return files;
306
+ }
307
+
308
+ /**
309
+ * Upload image files to the MediaProvider and insert <img> nodes at the
310
+ * current selection. Inserts a placeholder name when files lack one
311
+ * (e.g., screenshots from the system clipboard).
312
+ */
313
+ async function uploadAndInsertImages(
314
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
315
+ view: any,
316
+ files: File[],
317
+ mediaProvider: import('@bendyline/squisq/schemas').MediaProvider,
318
+ ): Promise<void> {
319
+ for (const file of files) {
320
+ try {
321
+ const buffer = await file.arrayBuffer();
322
+ const mimeType = file.type || 'image/png';
323
+ const name =
324
+ file.name && file.name !== 'image.png'
325
+ ? file.name
326
+ : `pasted-${uniquePasteToken()}.${extFromMime(mimeType)}`;
327
+ const relativePath = await mediaProvider.addMedia(name, buffer, mimeType);
328
+ const altText = name.replace(/\.[^.]+$/, '').replace(/[-_]/g, ' ');
329
+ insertImageNode(view, relativePath, altText);
330
+ } catch (err) {
331
+ console.error('Failed to upload dropped image:', err);
332
+ }
333
+ }
334
+ }
335
+
336
+ /** Insert an image node at the current selection using the schema image type. */
337
+ function insertImageNode(
338
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
339
+ view: any,
340
+ src: string,
341
+ alt: string,
342
+ ): void {
343
+ const { schema } = view.state;
344
+ const imageType = schema.nodes.image;
345
+ if (!imageType) return;
346
+ const node = imageType.create({ src, alt });
347
+ const tr = view.state.tr.replaceSelectionWith(node);
348
+ view.dispatch(tr);
349
+ }
350
+
351
+ /** Move the selection to the document position under the drop event's coordinates. */
352
+ function moveSelectionToDropPoint(
353
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
354
+ view: any,
355
+ event: DragEvent,
356
+ ): void {
357
+ const coords = view.posAtCoords({ left: event.clientX, top: event.clientY });
358
+ if (!coords) return;
359
+ const tr = view.state.tr.setSelection(
360
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
361
+ (view.state.selection.constructor as any).near(view.state.doc.resolve(coords.pos)),
362
+ );
363
+ view.dispatch(tr);
364
+ }
365
+
366
+ /**
367
+ * Produce a unique token for a pasted-file name. `Date.now()` alone can
368
+ * collide when a user pastes several clipboard images in the same tick
369
+ * (multi-image paste from a screenshot grid, for example), which would make
370
+ * `MediaProvider.addMedia` overwrite or reject later entries. Prefer
371
+ * `crypto.randomUUID()` when available and fall back to a counter so the
372
+ * helper stays pure-JS-everywhere.
373
+ */
374
+ let pasteCounter = 0;
375
+ function uniquePasteToken(): string {
376
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
377
+ return crypto.randomUUID();
378
+ }
379
+ pasteCounter = (pasteCounter + 1) % 1_000_000;
380
+ return `${Date.now()}-${pasteCounter.toString(36)}`;
381
+ }
382
+
383
+ function extFromMime(mime: string): string {
384
+ const map: Record<string, string> = {
385
+ 'image/png': 'png',
386
+ 'image/jpeg': 'jpg',
387
+ 'image/jpg': 'jpg',
388
+ 'image/gif': 'gif',
389
+ 'image/webp': 'webp',
390
+ 'image/svg+xml': 'svg',
391
+ 'image/avif': 'avif',
392
+ };
393
+ return map[mime.toLowerCase()] ?? 'png';
394
+ }
395
+
131
396
  /**
132
397
  * Hook to access the Tiptap editor instance for toolbar commands.
133
398
  * The WysiwygEditor must be mounted as a sibling or descendant.
@@ -0,0 +1,88 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { looksLikeMarkdown } from '../detectMarkdown';
3
+
4
+ describe('looksLikeMarkdown', () => {
5
+ it('returns false for empty / trivial text', () => {
6
+ expect(looksLikeMarkdown('')).toBe(false);
7
+ expect(looksLikeMarkdown(' ')).toBe(false);
8
+ expect(looksLikeMarkdown('Hello')).toBe(false);
9
+ });
10
+
11
+ it('returns false for plain prose', () => {
12
+ expect(looksLikeMarkdown('Hello world. This is just a sentence.')).toBe(false);
13
+ expect(looksLikeMarkdown('Multiple\nlines of\nplain text without any markdown syntax.')).toBe(
14
+ false,
15
+ );
16
+ });
17
+
18
+ it('detects ATX headings', () => {
19
+ expect(looksLikeMarkdown('# Heading')).toBe(true);
20
+ expect(looksLikeMarkdown('## Subheading')).toBe(true);
21
+ expect(looksLikeMarkdown('###### Tiny heading')).toBe(true);
22
+ expect(looksLikeMarkdown('Some intro\n\n## A heading\n\nBody text')).toBe(true);
23
+ });
24
+
25
+ it('does not treat hash in middle of line as a heading', () => {
26
+ expect(looksLikeMarkdown('See issue #123 for details.')).toBe(false);
27
+ });
28
+
29
+ it('detects bullet lists', () => {
30
+ expect(looksLikeMarkdown('- first\n- second\n- third')).toBe(true);
31
+ expect(looksLikeMarkdown('* item one\n* item two')).toBe(true);
32
+ expect(looksLikeMarkdown('+ plus item')).toBe(true);
33
+ });
34
+
35
+ it('detects ordered lists', () => {
36
+ expect(looksLikeMarkdown('1. step one\n2. step two')).toBe(true);
37
+ });
38
+
39
+ it('detects blockquotes', () => {
40
+ expect(looksLikeMarkdown('> A quoted line')).toBe(true);
41
+ });
42
+
43
+ it('detects code fences', () => {
44
+ expect(looksLikeMarkdown('```\nconst x = 1;\n```')).toBe(true);
45
+ expect(looksLikeMarkdown('```ts\nlet y = 2;\n```')).toBe(true);
46
+ });
47
+
48
+ it('detects GFM tables', () => {
49
+ expect(looksLikeMarkdown('| Col1 | Col2 |\n| --- | --- |\n| a | b |')).toBe(true);
50
+ });
51
+
52
+ it('detects task lists', () => {
53
+ expect(looksLikeMarkdown('- [ ] todo\n- [x] done')).toBe(true);
54
+ });
55
+
56
+ it('detects mixed inline patterns (2+ hits)', () => {
57
+ expect(looksLikeMarkdown('**Important:** see [the docs](http://example.com) for more')).toBe(
58
+ true,
59
+ );
60
+ expect(looksLikeMarkdown('Use `foo()` and read [the page](http://x.com)')).toBe(true);
61
+ });
62
+
63
+ it('does not trigger on a single inline pattern in plain prose', () => {
64
+ expect(looksLikeMarkdown('Visit https://example.com or [the docs](http://docs.com)')).toBe(
65
+ false,
66
+ );
67
+ expect(looksLikeMarkdown('She said `hello` to him')).toBe(false);
68
+ });
69
+
70
+ it('detects markdown with windows line endings', () => {
71
+ expect(looksLikeMarkdown('# Heading\r\n\r\nBody text')).toBe(true);
72
+ });
73
+
74
+ it('detects a full resume-style document', () => {
75
+ const text = `# Mike Ammerlaan
76
+
77
+ ## Projects
78
+
79
+ Qualla (qualla.com) - Designed and built a map-driven storytelling platform.
80
+
81
+ ## **Experience**
82
+
83
+ **Principal Product Manager, Minecraft**
84
+ Jan 2021 - Present
85
+ Driving the creator platform.`;
86
+ expect(looksLikeMarkdown(text)).toBe(true);
87
+ });
88
+ });
@@ -0,0 +1,96 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { detectLanguageFromFileName, resolveFileKind } from '../fileKind';
3
+
4
+ describe('detectLanguageFromFileName', () => {
5
+ it('detects common extensions', () => {
6
+ expect(detectLanguageFromFileName('foo.ts')).toBe('typescript');
7
+ expect(detectLanguageFromFileName('app.py')).toBe('python');
8
+ expect(detectLanguageFromFileName('data.json')).toBe('json');
9
+ expect(detectLanguageFromFileName('index.html')).toBe('html');
10
+ expect(detectLanguageFromFileName('styles.css')).toBe('css');
11
+ expect(detectLanguageFromFileName('README.md')).toBe('markdown');
12
+ });
13
+
14
+ it('uses the final extension for multi-dot names', () => {
15
+ expect(detectLanguageFromFileName('archive.tar.gz')).toBe(null);
16
+ expect(detectLanguageFromFileName('component.test.ts')).toBe('typescript');
17
+ });
18
+
19
+ it('accepts bare extensions with or without a leading dot', () => {
20
+ expect(detectLanguageFromFileName('.ts')).toBe('typescript');
21
+ expect(detectLanguageFromFileName('ts')).toBe('typescript');
22
+ });
23
+
24
+ it('matches extensionless names like Dockerfile', () => {
25
+ expect(detectLanguageFromFileName('Dockerfile')).toBe('dockerfile');
26
+ expect(detectLanguageFromFileName('dockerfile')).toBe('dockerfile');
27
+ });
28
+
29
+ it('is case-insensitive for extensions', () => {
30
+ expect(detectLanguageFromFileName('foo.TS')).toBe('typescript');
31
+ expect(detectLanguageFromFileName('App.JSX')).toBe('javascript');
32
+ });
33
+
34
+ it('strips leading paths', () => {
35
+ expect(detectLanguageFromFileName('src/lib/foo.ts')).toBe('typescript');
36
+ expect(detectLanguageFromFileName('C:\\Users\\me\\app.py')).toBe('python');
37
+ });
38
+
39
+ it('returns null for unknown extensions', () => {
40
+ expect(detectLanguageFromFileName('foo.xyz')).toBe(null);
41
+ expect(detectLanguageFromFileName('mystery.blob')).toBe(null);
42
+ });
43
+
44
+ it('returns null for empty or trailing-dot inputs', () => {
45
+ expect(detectLanguageFromFileName('')).toBe(null);
46
+ expect(detectLanguageFromFileName(' ')).toBe(null);
47
+ expect(detectLanguageFromFileName('foo.')).toBe(null);
48
+ });
49
+ });
50
+
51
+ describe('resolveFileKind', () => {
52
+ it('defaults to markdown mode when nothing is supplied', () => {
53
+ expect(resolveFileKind()).toEqual({ mode: 'markdown', language: 'markdown' });
54
+ });
55
+
56
+ it('returns code mode for recognized code extensions', () => {
57
+ expect(resolveFileKind('foo.ts')).toEqual({ mode: 'code', language: 'typescript' });
58
+ expect(resolveFileKind('app.py')).toEqual({ mode: 'code', language: 'python' });
59
+ expect(resolveFileKind('data.json')).toEqual({ mode: 'code', language: 'json' });
60
+ });
61
+
62
+ it('keeps markdown extensions in markdown mode', () => {
63
+ expect(resolveFileKind('README.md')).toEqual({ mode: 'markdown', language: 'markdown' });
64
+ expect(resolveFileKind('doc.markdown')).toEqual({ mode: 'markdown', language: 'markdown' });
65
+ });
66
+
67
+ it('keeps .txt in markdown mode with plaintext language', () => {
68
+ expect(resolveFileKind('notes.txt')).toEqual({ mode: 'markdown', language: 'plaintext' });
69
+ });
70
+
71
+ it('falls back to markdown mode for unknown extensions', () => {
72
+ expect(resolveFileKind('foo.xyz')).toEqual({ mode: 'markdown', language: 'markdown' });
73
+ });
74
+
75
+ it('lets the explicit language override fileName detection', () => {
76
+ expect(resolveFileKind('foo.md', 'typescript')).toEqual({
77
+ mode: 'code',
78
+ language: 'typescript',
79
+ });
80
+ });
81
+
82
+ it('stays in markdown mode when language override is markdown or plaintext', () => {
83
+ expect(resolveFileKind('foo.ts', 'markdown')).toEqual({
84
+ mode: 'markdown',
85
+ language: 'markdown',
86
+ });
87
+ expect(resolveFileKind(undefined, 'plaintext')).toEqual({
88
+ mode: 'markdown',
89
+ language: 'plaintext',
90
+ });
91
+ });
92
+
93
+ it('accepts a language with no fileName', () => {
94
+ expect(resolveFileKind(undefined, 'rust')).toEqual({ mode: 'code', language: 'rust' });
95
+ });
96
+ });
@@ -0,0 +1,110 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { MediaProvider } from '@bendyline/squisq/schemas';
3
+ import { markdownToTiptap, tiptapToMarkdown } from '../tiptapBridge';
4
+
5
+ /**
6
+ * Attachment-flow regression: earlier versions of MediaBin dropped
7
+ * uploaded files into the bin without inserting a markdown ref into
8
+ * the editor body. A user would upload an image, hit Send in the
9
+ * downstream chat composer, and the outgoing markdown would have no
10
+ * image reference — the gezel would reply "nothing came through."
11
+ *
12
+ * The fix: after `mediaProvider.addMedia(...)` succeeds, MediaBin
13
+ * fires `onMediaUploaded(relativePath, name, mimeType)`. The
14
+ * EditorShell wires this to an `insertAtCursor` that emits
15
+ * `![alt](attachments/<filename>)` so the file actually participates
16
+ * in the outgoing markdown.
17
+ *
18
+ * These tests exercise the contract directly: the markdown snippet
19
+ * produced by the upload callback, once round-tripped through the
20
+ * editor's markdown↔HTML bridge, must round-trip back to a form
21
+ * the gezel service's image-extraction regex can see.
22
+ */
23
+
24
+ function fakeMediaProvider(records: string[]): MediaProvider {
25
+ let counter = 0;
26
+ return {
27
+ async addMedia(name: string, _data: ArrayBuffer | Blob | Uint8Array, _mime: string) {
28
+ counter += 1;
29
+ const relative = `attachments/${counter}-${name}`;
30
+ records.push(relative);
31
+ return relative;
32
+ },
33
+ async resolveUrl(relPath: string) {
34
+ return relPath;
35
+ },
36
+ async listMedia() {
37
+ return [];
38
+ },
39
+ async removeMedia(_relPath: string) {
40
+ /* no-op */
41
+ },
42
+ dispose() {
43
+ /* no-op */
44
+ },
45
+ };
46
+ }
47
+
48
+ /**
49
+ * Re-implements the exact snippet EditorShell's `insertMediaRef`
50
+ * builds. Keeping this aligned with the real impl would ordinarily
51
+ * rely on directly importing the helper; since it's currently inline
52
+ * in EditorShell, mirror the logic here and lean on the test to
53
+ * alert us if we drift apart.
54
+ */
55
+ function buildAttachmentSnippet(relativePath: string, name: string, mimeType: string): string {
56
+ const alt = name.replace(/\.[^.]+$/, '').replace(/[-_]/g, ' ');
57
+ return mimeType.startsWith('image/') ? `![${alt}](${relativePath})` : `[${alt}](${relativePath})`;
58
+ }
59
+
60
+ describe('media attachment flow', () => {
61
+ it('addMedia → buildAttachmentSnippet → markdown round-trip keeps the ref', async () => {
62
+ const records: string[] = [];
63
+ const provider = fakeMediaProvider(records);
64
+
65
+ // Simulate MediaBin.handleFileChange for a single PNG drop.
66
+ const pngBytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
67
+ const relative = await provider.addMedia('my_screenshot.png', pngBytes, 'image/png');
68
+
69
+ expect(records).toEqual([relative]);
70
+ const snippet = buildAttachmentSnippet(relative, 'my_screenshot.png', 'image/png');
71
+ expect(snippet).toBe(`![my screenshot](${relative})`);
72
+
73
+ // Insert snippet into the editor: markdown → HTML → markdown.
74
+ // This is the path a real insertAtCursor + tiptap onUpdate goes
75
+ // through. The outbound markdown must still contain the ref.
76
+ const html = markdownToTiptap(snippet);
77
+ expect(html).toMatch(/<img\b/);
78
+ expect(html).toContain(`src="${relative}"`);
79
+
80
+ const back = tiptapToMarkdown(html);
81
+ expect(back).toContain(`![my screenshot](${relative})`);
82
+ });
83
+
84
+ it('handles empty-alt (most common pasted-image shape)', async () => {
85
+ const records: string[] = [];
86
+ const provider = fakeMediaProvider(records);
87
+ const relative = await provider.addMedia('pasted.png', new Uint8Array([0]), 'image/png');
88
+
89
+ // Simulate what happens when alt is empty — common for bare pastes
90
+ // where the user hasn't typed a caption.
91
+ const snippet = `![](${relative})`;
92
+ const html = markdownToTiptap(snippet);
93
+ expect(html).toMatch(/<img\b/);
94
+ expect(html).toContain(`src="${relative}"`);
95
+ const back = tiptapToMarkdown(html);
96
+ expect(back).toContain(`![](${relative})`);
97
+ });
98
+
99
+ it('non-image files fall back to a plain link, still preserving the ref', async () => {
100
+ const records: string[] = [];
101
+ const provider = fakeMediaProvider(records);
102
+ const relative = await provider.addMedia('design.pdf', new Uint8Array([0]), 'application/pdf');
103
+ const snippet = buildAttachmentSnippet(relative, 'design.pdf', 'application/pdf');
104
+ expect(snippet).toBe(`[design](${relative})`);
105
+ // Non-images don't go through the `<img>` regex — they stay as
106
+ // plain markdown links, which the service-side extractor ignores
107
+ // but the UI renders as normal hyperlinks.
108
+ expect(snippet).not.toContain('!');
109
+ });
110
+ });