|
| 1 | +import { createHighlighter } from 'shiki' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { transformerNotationDiff, transformerNotationFocus, transformerNotationHighlight } from '../src' |
| 4 | + |
| 5 | +describe('multi-token comment support', () => { |
| 6 | + it('transformerNotationDiff works with rose-pine theme (multi-token comments)', async () => { |
| 7 | + const highlighter = await createHighlighter({ |
| 8 | + themes: ['rose-pine'], |
| 9 | + langs: ['javascript'], |
| 10 | + }) |
| 11 | + |
| 12 | + const code = 'const a = 1 // [!code --]' |
| 13 | + |
| 14 | + const html = highlighter.codeToHtml(code, { |
| 15 | + lang: 'javascript', |
| 16 | + theme: 'rose-pine', |
| 17 | + transformers: [transformerNotationDiff()], |
| 18 | + }) |
| 19 | + |
| 20 | + // Should have the diff classes applied |
| 21 | + expect(html).toContain('has-diff') |
| 22 | + expect(html).toContain('diff remove') |
| 23 | + // Should not contain the comment notation in the output |
| 24 | + expect(html).not.toContain('[!code --]') |
| 25 | + }) |
| 26 | + |
| 27 | + it('transformerNotationDiff still works with dracula theme (single-token comments)', async () => { |
| 28 | + const highlighter = await createHighlighter({ |
| 29 | + themes: ['dracula'], |
| 30 | + langs: ['javascript'], |
| 31 | + }) |
| 32 | + |
| 33 | + const code = 'const a = 1 // [!code --]' |
| 34 | + |
| 35 | + const html = highlighter.codeToHtml(code, { |
| 36 | + lang: 'javascript', |
| 37 | + theme: 'dracula', |
| 38 | + transformers: [transformerNotationDiff()], |
| 39 | + }) |
| 40 | + |
| 41 | + // Should have the diff classes applied |
| 42 | + expect(html).toContain('has-diff') |
| 43 | + expect(html).toContain('diff remove') |
| 44 | + // Should not contain the comment notation in the output |
| 45 | + expect(html).not.toContain('[!code --]') |
| 46 | + }) |
| 47 | + |
| 48 | + it('transformerNotationDiff works with rose-pine theme for add notation', async () => { |
| 49 | + const highlighter = await createHighlighter({ |
| 50 | + themes: ['rose-pine'], |
| 51 | + langs: ['javascript'], |
| 52 | + }) |
| 53 | + |
| 54 | + const code = 'const b = 2 // [!code ++]' |
| 55 | + |
| 56 | + const html = highlighter.codeToHtml(code, { |
| 57 | + lang: 'javascript', |
| 58 | + theme: 'rose-pine', |
| 59 | + transformers: [transformerNotationDiff()], |
| 60 | + }) |
| 61 | + |
| 62 | + expect(html).toContain('has-diff') |
| 63 | + expect(html).toContain('diff add') |
| 64 | + expect(html).not.toContain('[!code ++]') |
| 65 | + }) |
| 66 | + |
| 67 | + it('transformerNotationHighlight works with rose-pine theme', async () => { |
| 68 | + const highlighter = await createHighlighter({ |
| 69 | + themes: ['rose-pine'], |
| 70 | + langs: ['javascript'], |
| 71 | + }) |
| 72 | + |
| 73 | + const code = 'const c = 3 // [!code highlight]' |
| 74 | + |
| 75 | + const html = highlighter.codeToHtml(code, { |
| 76 | + lang: 'javascript', |
| 77 | + theme: 'rose-pine', |
| 78 | + transformers: [transformerNotationHighlight()], |
| 79 | + }) |
| 80 | + |
| 81 | + expect(html).toContain('highlighted') |
| 82 | + expect(html).not.toContain('[!code highlight]') |
| 83 | + }) |
| 84 | + |
| 85 | + it('transformerNotationFocus works with rose-pine theme', async () => { |
| 86 | + const highlighter = await createHighlighter({ |
| 87 | + themes: ['rose-pine'], |
| 88 | + langs: ['javascript'], |
| 89 | + }) |
| 90 | + |
| 91 | + const code = 'const d = 4 // [!code focus]' |
| 92 | + |
| 93 | + const html = highlighter.codeToHtml(code, { |
| 94 | + lang: 'javascript', |
| 95 | + theme: 'rose-pine', |
| 96 | + transformers: [transformerNotationFocus()], |
| 97 | + }) |
| 98 | + |
| 99 | + expect(html).toContain('focused') |
| 100 | + expect(html).not.toContain('[!code focus]') |
| 101 | + }) |
| 102 | + |
| 103 | + it('handles multi-line code with mixed single and multi-token comments', async () => { |
| 104 | + const highlighter = await createHighlighter({ |
| 105 | + themes: ['rose-pine'], |
| 106 | + langs: ['javascript'], |
| 107 | + }) |
| 108 | + |
| 109 | + const code = `const a = 1 // [!code --] |
| 110 | +const b = 2 // [!code ++] |
| 111 | +const c = 3 // [!code highlight]` |
| 112 | + |
| 113 | + const html = highlighter.codeToHtml(code, { |
| 114 | + lang: 'javascript', |
| 115 | + theme: 'rose-pine', |
| 116 | + transformers: [transformerNotationDiff(), transformerNotationHighlight()], |
| 117 | + }) |
| 118 | + |
| 119 | + expect(html).toContain('has-diff') |
| 120 | + expect(html).toContain('diff remove') |
| 121 | + expect(html).toContain('diff add') |
| 122 | + expect(html).toContain('highlighted') |
| 123 | + expect(html).not.toContain('[!code') |
| 124 | + }) |
| 125 | + |
| 126 | + it('handles edge case where comment does not match pattern', async () => { |
| 127 | + const highlighter = await createHighlighter({ |
| 128 | + themes: ['rose-pine'], |
| 129 | + langs: ['javascript'], |
| 130 | + }) |
| 131 | + |
| 132 | + const code = 'const e = 5 // [!invalid notation]' |
| 133 | + |
| 134 | + const html = highlighter.codeToHtml(code, { |
| 135 | + lang: 'javascript', |
| 136 | + theme: 'rose-pine', |
| 137 | + transformers: [transformerNotationDiff()], |
| 138 | + }) |
| 139 | + |
| 140 | + // Should not have diff classes since notation is invalid |
| 141 | + expect(html).not.toContain('has-diff') |
| 142 | + expect(html).not.toContain('diff remove') |
| 143 | + // Should still contain the invalid notation |
| 144 | + expect(html).toContain('[!invalid notation]') |
| 145 | + }) |
| 146 | + |
| 147 | + it('handles single token without multi-token fallback', async () => { |
| 148 | + const highlighter = await createHighlighter({ |
| 149 | + themes: ['rose-pine'], |
| 150 | + langs: ['javascript'], |
| 151 | + }) |
| 152 | + |
| 153 | + const code = 'const f = 6 // regular comment' |
| 154 | + |
| 155 | + const html = highlighter.codeToHtml(code, { |
| 156 | + lang: 'javascript', |
| 157 | + theme: 'rose-pine', |
| 158 | + transformers: [transformerNotationDiff()], |
| 159 | + }) |
| 160 | + |
| 161 | + expect(html).not.toContain('has-diff') |
| 162 | + expect(html).toContain('regular comment') |
| 163 | + }) |
| 164 | + |
| 165 | + it('handles JSX parsing without notation', async () => { |
| 166 | + const highlighter = await createHighlighter({ |
| 167 | + themes: ['rose-pine'], |
| 168 | + langs: ['jsx'], |
| 169 | + }) |
| 170 | + |
| 171 | + const code = 'const g = 7 {/* regular comment */}' |
| 172 | + |
| 173 | + const html = highlighter.codeToHtml(code, { |
| 174 | + lang: 'jsx', |
| 175 | + theme: 'rose-pine', |
| 176 | + transformers: [transformerNotationDiff()], |
| 177 | + }) |
| 178 | + |
| 179 | + expect(html).not.toContain('has-diff') |
| 180 | + expect(html).toContain('regular comment') |
| 181 | + }) |
| 182 | + |
| 183 | + it('handles v1 match algorithm', async () => { |
| 184 | + const highlighter = await createHighlighter({ |
| 185 | + themes: ['rose-pine'], |
| 186 | + langs: ['javascript'], |
| 187 | + }) |
| 188 | + |
| 189 | + const code = 'const h = 8 // [!code ++]' |
| 190 | + |
| 191 | + const html = highlighter.codeToHtml(code, { |
| 192 | + lang: 'javascript', |
| 193 | + theme: 'rose-pine', |
| 194 | + transformers: [transformerNotationDiff({ matchAlgorithm: 'v1' })], |
| 195 | + }) |
| 196 | + |
| 197 | + expect(html).toContain('has-diff') |
| 198 | + expect(html).toContain('diff add') |
| 199 | + expect(html).not.toContain('[!code ++]') |
| 200 | + }) |
| 201 | + |
| 202 | + it('handles actual multi-token comment scenario from rose-pine theme', async () => { |
| 203 | + const highlighter = await createHighlighter({ |
| 204 | + themes: ['rose-pine'], |
| 205 | + langs: ['javascript'], |
| 206 | + }) |
| 207 | + |
| 208 | + // This specifically tests the case where rose-pine theme splits the comment |
| 209 | + // into separate tokens: "//" and " [!code --]" |
| 210 | + const code = 'const i = 9 // [!code --]' |
| 211 | + |
| 212 | + const html = highlighter.codeToHtml(code, { |
| 213 | + lang: 'javascript', |
| 214 | + theme: 'rose-pine', |
| 215 | + transformers: [transformerNotationDiff()], |
| 216 | + }) |
| 217 | + |
| 218 | + // Verify the multi-token handling works |
| 219 | + expect(html).toContain('has-diff') |
| 220 | + expect(html).toContain('diff remove') |
| 221 | + expect(html).not.toContain('[!code --]') |
| 222 | + }) |
| 223 | +}) |
0 commit comments