|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +// Test the highlight-spans regex behaviour directly. |
| 4 | +// The bug (issue #2): a backtick span at the very start of a line was not |
| 5 | +// recognised because the pattern required a non-backtick character before the |
| 6 | +// opening backtick. The fix adds `^` as an alternative via the `m` flag. |
| 7 | + |
| 8 | +function applyHighlightPattern(html: string): string { |
| 9 | + // Mirrors the fixed pattern in highlightBlockSpans (highlightSpans === true). |
| 10 | + const pattern = /(^|[^`])`([^`]+?)`/gm; |
| 11 | + return html.replace(pattern, (m, e, c) => { |
| 12 | + if (e === '\\') return m.slice(1); |
| 13 | + return e + `<span class="remark-code-span-highlighted">${c}</span>`; |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +describe('highlightSpans regex (issue #2)', () => { |
| 18 | + it('highlights a span preceded by a non-backtick character', () => { |
| 19 | + const result = applyHighlightPattern('call `method`(arg)'); |
| 20 | + expect(result).toContain('<span class="remark-code-span-highlighted">method</span>'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('highlights a span at the very start of a string', () => { |
| 24 | + const result = applyHighlightPattern('`highlightedMethod`(arg1, arg2)'); |
| 25 | + expect(result).toContain('<span class="remark-code-span-highlighted">highlightedMethod</span>'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('highlights a span at the start of a new line', () => { |
| 29 | + const html = 'normalMethod(a)\n`highlightedMethod`(b)'; |
| 30 | + const result = applyHighlightPattern(html); |
| 31 | + expect(result).toContain('<span class="remark-code-span-highlighted">highlightedMethod</span>'); |
| 32 | + expect(result).toContain('normalMethod(a)'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('does not double-highlight adjacent backtick spans', () => { |
| 36 | + const result = applyHighlightPattern('`a` and `b`'); |
| 37 | + expect(result).toContain('<span class="remark-code-span-highlighted">a</span>'); |
| 38 | + expect(result).toContain('<span class="remark-code-span-highlighted">b</span>'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not highlight a backtick span that is escaped', () => { |
| 42 | + const result = applyHighlightPattern('\\`notHighlighted`'); |
| 43 | + expect(result).not.toContain('remark-code-span-highlighted'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not treat fenced code delimiters (```) as highlight spans', () => { |
| 47 | + const result = applyHighlightPattern('```js\ncode\n```'); |
| 48 | + expect(result).not.toContain('remark-code-span-highlighted'); |
| 49 | + }); |
| 50 | +}); |
0 commit comments