|
3 | 3 | */ |
4 | 4 | import { Editor } from '@tiptap/core' |
5 | 5 | import { afterEach, describe, expect, it } from 'vitest' |
6 | | -import { buildDecorations } from './code-highlight' |
| 6 | +import { buildDecorations, changeTouchesCodeBlock } from './code-highlight' |
7 | 7 | import { createMarkdownContentExtensions } from './extensions' |
8 | 8 |
|
9 | 9 | let editor: Editor | null = null |
10 | 10 |
|
| 11 | +/** Position just inside the first code block in the current editor doc. */ |
| 12 | +function codeBlockPos(ed: Editor): number { |
| 13 | + let pos = -1 |
| 14 | + ed.state.doc.descendants((node, p) => { |
| 15 | + if (pos === -1 && node.type.name === 'codeBlock') pos = p |
| 16 | + return pos === -1 |
| 17 | + }) |
| 18 | + if (pos === -1) throw new Error('no code block') |
| 19 | + return pos |
| 20 | +} |
| 21 | + |
11 | 22 | function decorationClassesFor(markdown: string): string[] { |
12 | 23 | editor = new Editor({ extensions: createMarkdownContentExtensions() }) |
13 | 24 | editor.commands.setContent(markdown, { contentType: 'markdown' }) |
@@ -41,3 +52,30 @@ describe('code block syntax highlighting', () => { |
41 | 52 | expect(decorationClassesFor('```unregistered-lang\n+++ foo\n```')).toHaveLength(0) |
42 | 53 | }) |
43 | 54 | }) |
| 55 | + |
| 56 | +describe('changeTouchesCodeBlock (incremental re-tokenization gate)', () => { |
| 57 | + function mount(markdown: string): Editor { |
| 58 | + editor = new Editor({ extensions: createMarkdownContentExtensions() }) |
| 59 | + editor.commands.setContent(markdown, { contentType: 'markdown' }) |
| 60 | + return editor |
| 61 | + } |
| 62 | + |
| 63 | + it('is false when an edit lands only in prose (decorations are mapped, not rebuilt)', () => { |
| 64 | + const ed = mount('intro text\n\n```js\nconst x = 1\n```') |
| 65 | + const tr = ed.state.tr.insertText('Z', 1) // inside the leading paragraph |
| 66 | + expect(changeTouchesCodeBlock(tr, tr.doc)).toBe(false) |
| 67 | + }) |
| 68 | + |
| 69 | + it('is true when an edit lands inside a code block (forces a re-tokenize)', () => { |
| 70 | + const ed = mount('intro\n\n```js\nconst x = 1\n```') |
| 71 | + const tr = ed.state.tr.insertText('y', codeBlockPos(ed) + 1) |
| 72 | + expect(changeTouchesCodeBlock(tr, tr.doc)).toBe(true) |
| 73 | + }) |
| 74 | + |
| 75 | + it('is true when the code block language changes via setNodeMarkup', () => { |
| 76 | + const ed = mount('```js\nconst x = 1\n```') |
| 77 | + const pos = codeBlockPos(ed) |
| 78 | + const tr = ed.state.tr.setNodeMarkup(pos, undefined, { language: 'python' }) |
| 79 | + expect(changeTouchesCodeBlock(tr, tr.doc)).toBe(true) |
| 80 | + }) |
| 81 | +}) |
0 commit comments