@bendyline/squisq-editor-react 1.2.2 → 1.3.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 (95) 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 +101 -2
  6. package/dist/EditorShell.d.ts.map +1 -1
  7. package/dist/EditorShell.js +20 -8
  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.map +1 -1
  13. package/dist/MediaBin.js +16 -1
  14. package/dist/MediaBin.js.map +1 -1
  15. package/dist/MentionExtension.d.ts +22 -0
  16. package/dist/MentionExtension.d.ts.map +1 -0
  17. package/dist/MentionExtension.js +242 -0
  18. package/dist/MentionExtension.js.map +1 -0
  19. package/dist/RawEditor.d.ts +8 -1
  20. package/dist/RawEditor.d.ts.map +1 -1
  21. package/dist/RawEditor.js +167 -30
  22. package/dist/RawEditor.js.map +1 -1
  23. package/dist/TemplateAnnotation.d.ts.map +1 -1
  24. package/dist/TemplateAnnotation.js +4 -2
  25. package/dist/TemplateAnnotation.js.map +1 -1
  26. package/dist/Toolbar.d.ts +7 -1
  27. package/dist/Toolbar.d.ts.map +1 -1
  28. package/dist/Toolbar.js +57 -18
  29. package/dist/Toolbar.js.map +1 -1
  30. package/dist/Tooltip.d.ts +10 -0
  31. package/dist/Tooltip.d.ts.map +1 -0
  32. package/dist/Tooltip.js +104 -0
  33. package/dist/Tooltip.js.map +1 -0
  34. package/dist/ViewSwitcher.d.ts +1 -1
  35. package/dist/ViewSwitcher.d.ts.map +1 -1
  36. package/dist/ViewSwitcher.js +10 -4
  37. package/dist/ViewSwitcher.js.map +1 -1
  38. package/dist/WysiwygEditor.d.ts +13 -2
  39. package/dist/WysiwygEditor.d.ts.map +1 -1
  40. package/dist/WysiwygEditor.js +239 -4
  41. package/dist/WysiwygEditor.js.map +1 -1
  42. package/dist/__tests__/detectMarkdown.test.d.ts +2 -0
  43. package/dist/__tests__/detectMarkdown.test.d.ts.map +1 -0
  44. package/dist/__tests__/detectMarkdown.test.js +69 -0
  45. package/dist/__tests__/detectMarkdown.test.js.map +1 -0
  46. package/dist/__tests__/fileKind.test.d.ts +2 -0
  47. package/dist/__tests__/fileKind.test.d.ts.map +1 -0
  48. package/dist/__tests__/fileKind.test.js +81 -0
  49. package/dist/__tests__/fileKind.test.js.map +1 -0
  50. package/dist/__tests__/tiptapBridge.test.js +36 -0
  51. package/dist/__tests__/tiptapBridge.test.js.map +1 -1
  52. package/dist/detectMarkdown.d.ts +20 -0
  53. package/dist/detectMarkdown.d.ts.map +1 -0
  54. package/dist/detectMarkdown.js +61 -0
  55. package/dist/detectMarkdown.js.map +1 -0
  56. package/dist/fileKind.d.ts +30 -0
  57. package/dist/fileKind.d.ts.map +1 -0
  58. package/dist/fileKind.js +123 -0
  59. package/dist/fileKind.js.map +1 -0
  60. package/dist/hooks/useFileDrop.d.ts.map +1 -1
  61. package/dist/hooks/useFileDrop.js +9 -7
  62. package/dist/hooks/useFileDrop.js.map +1 -1
  63. package/dist/index.d.ts +4 -1
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +4 -0
  66. package/dist/index.js.map +1 -1
  67. package/dist/mediaDragMime.d.ts +17 -0
  68. package/dist/mediaDragMime.d.ts.map +1 -0
  69. package/dist/mediaDragMime.js +22 -0
  70. package/dist/mediaDragMime.js.map +1 -0
  71. package/dist/tiptapBridge.d.ts.map +1 -1
  72. package/dist/tiptapBridge.js +58 -2
  73. package/dist/tiptapBridge.js.map +1 -1
  74. package/package.json +9 -7
  75. package/src/EditorContext.tsx +106 -3
  76. package/src/EditorShell.tsx +195 -15
  77. package/src/ImageNodeView.tsx +15 -2
  78. package/src/MediaBin.tsx +23 -1
  79. package/src/MentionExtension.tsx +258 -0
  80. package/src/RawEditor.tsx +193 -37
  81. package/src/TemplateAnnotation.ts +4 -2
  82. package/src/Toolbar.tsx +111 -48
  83. package/src/Tooltip.tsx +124 -0
  84. package/src/ViewSwitcher.tsx +15 -5
  85. package/src/WysiwygEditor.tsx +270 -5
  86. package/src/__tests__/detectMarkdown.test.ts +88 -0
  87. package/src/__tests__/fileKind.test.ts +96 -0
  88. package/src/__tests__/tiptapBridge.test.ts +44 -0
  89. package/src/detectMarkdown.ts +62 -0
  90. package/src/fileKind.ts +134 -0
  91. package/src/hooks/useFileDrop.ts +10 -6
  92. package/src/index.ts +11 -0
  93. package/src/mediaDragMime.ts +32 -0
  94. package/src/styles/editor.css +214 -8
  95. package/src/tiptapBridge.ts +66 -2
@@ -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
+ });
@@ -49,6 +49,31 @@ describe('markdownToTiptap', () => {
49
49
  expect(html).toContain('Example');
50
50
  });
51
51
 
52
+ it('converts mentions to chip spans', () => {
53
+ const html = markdownToTiptap('Hey @[Leo](gezel:leo), take a look.');
54
+ expect(html).toContain('data-mention="true"');
55
+ expect(html).toContain('data-kind="gezel"');
56
+ expect(html).toContain('data-id="leo"');
57
+ expect(html).toContain('data-label="Leo"');
58
+ // "@Leo" appears inside the chip — NOT as a broken link
59
+ expect(html).not.toContain('href="gezel:leo"');
60
+ });
61
+
62
+ it('tolerates the backslash-escaped colon remark emits', () => {
63
+ // remark-stringify sometimes emits `gezel\:leo` to disambiguate
64
+ // from autolink syntax. The bridge should still recognize it.
65
+ const html = markdownToTiptap('Hey @[Leo](gezel\\:leo).');
66
+ expect(html).toContain('data-kind="gezel"');
67
+ expect(html).toContain('data-id="leo"');
68
+ });
69
+
70
+ it('round-trips mentions back to markdown', () => {
71
+ const md = 'Hey @[Leo](gezel:leo), ping @[Tess](gezel:tess) too.';
72
+ const html = markdownToTiptap(md);
73
+ const back = tiptapToMarkdown(html);
74
+ expect(back.trim()).toBe(md);
75
+ });
76
+
52
77
  it('converts images', () => {
53
78
  const html = markdownToTiptap('![Logo](logo.png)');
54
79
  expect(html).toContain('alt="Logo"');
@@ -230,6 +255,25 @@ describe('tiptapToMarkdown', () => {
230
255
  expect(md).toContain('&');
231
256
  expect(md).toContain('"test"');
232
257
  });
258
+
259
+ it('converts <br> to a hard line break (two trailing spaces)', () => {
260
+ const md = tiptapToMarkdown('<p>line one<br>line two</p>');
261
+ expect(md).toContain('line one \nline two');
262
+ });
263
+
264
+ it('preserves paragraph break inside list items', () => {
265
+ const md = tiptapToMarkdown(
266
+ '<ul><li><p><strong>Title</strong></p><p>Description text</p></li></ul>',
267
+ );
268
+ expect(md).toContain('- **Title**');
269
+ expect(md).toContain(' Description text');
270
+ });
271
+
272
+ it('preserves <br> hard break inside list items', () => {
273
+ const md = tiptapToMarkdown('<ul><li><p>First line<br>Second line</p></li></ul>');
274
+ expect(md).toContain('- First line ');
275
+ expect(md).toContain(' Second line');
276
+ });
233
277
  });
234
278
 
235
279
  // ---------------------------------------------------------------------------
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Markdown detection — heuristic for spotting markdown source in pasted text.
3
+ *
4
+ * Used by the WYSIWYG editor's paste handler to decide whether plain-text
5
+ * paste content should be parsed as markdown rather than inserted literally.
6
+ *
7
+ * The heuristic prefers false negatives (treating markdown as plain text)
8
+ * over false positives (mangling plain text that happens to contain a few
9
+ * special characters).
10
+ */
11
+
12
+ /** Block-level patterns that strongly indicate markdown. Any one is enough. */
13
+ const STRONG_BLOCK_PATTERNS: RegExp[] = [
14
+ /^#{1,6}\s+\S/, // # Heading
15
+ /^[-*+]\s+\S/, // - bullet
16
+ /^\d+\.\s+\S/, // 1. ordered
17
+ /^>\s+\S/, // > blockquote
18
+ /^```/, // ``` code fence
19
+ /^\|.+\|\s*$/, // | table | row |
20
+ /^[-*+]\s+\[[ xX]\]\s+/, // - [ ] task
21
+ ];
22
+
23
+ /** Inline patterns that are weaker indicators on their own. */
24
+ const INLINE_PATTERNS: RegExp[] = [
25
+ /\*\*[^*\n]+\*\*/, // **bold**
26
+ /__[^_\n]+__/, // __bold__
27
+ /`[^`\n]+`/, // `code`
28
+ /\[[^\]\n]+\]\([^)\n]+\)/, // [link](url)
29
+ /!\[[^\]\n]*\]\([^)\n]+\)/, // ![image](url)
30
+ /~~[^~\n]+~~/, // ~~strike~~
31
+ ];
32
+
33
+ /**
34
+ * Returns true if the text looks like markdown source.
35
+ *
36
+ * Detection rules:
37
+ * - Any line matching a strong block pattern → yes
38
+ * - Two or more inline pattern matches anywhere in the text → yes
39
+ * - Otherwise → no
40
+ */
41
+ export function looksLikeMarkdown(text: string): boolean {
42
+ if (!text || text.length < 2) return false;
43
+
44
+ const lines = text.split(/\r?\n/);
45
+ for (const line of lines) {
46
+ const trimmed = line.trim();
47
+ if (!trimmed) continue;
48
+ for (const re of STRONG_BLOCK_PATTERNS) {
49
+ if (re.test(trimmed)) return true;
50
+ }
51
+ }
52
+
53
+ let inlineHits = 0;
54
+ for (const re of INLINE_PATTERNS) {
55
+ if (re.test(text)) {
56
+ inlineHits++;
57
+ if (inlineHits >= 2) return true;
58
+ }
59
+ }
60
+
61
+ return false;
62
+ }