Skip to content

Commit 63e6da8

Browse files
authored
fix(transformers): handle split # and -- comments in rose-pine theme (#1317)
1 parent b968ed4 commit 63e6da8

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎packages/transformers/src/shared/parse-comments.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { MatchAlgorithm } from './notation-transformer'
44
const RE_SPLIT_COMMENT = /(\s+\/\/)/
55
const RE_V1_END_COMMENT_PREFIX = /(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(\s*)$/
66
const RE_V3_END_COMMENT_PREFIX = /(?:\/\/|#|;{1,2}|%{1,2}|--)(\s*)$/
7+
const RE_LINE_COMMENT_PREFIX = /^(?:\/\/|["'#]|;{1,2}|%{1,2}|--)$/
78

89
export type ParsedComments = {
910
line: Element
@@ -105,14 +106,14 @@ export function parseComments(
105106
let match = matchToken(head.value, isLast)
106107
let additionalTokens: Element[] | undefined
107108

108-
// Handle multi-token comments (e.g., rose-pine theme splits "//" and " [!code --]")
109+
// Handle multi-token comments (e.g., rose-pine theme splits "//" or "#" and " [!code --]")
109110
// Check if current token might be the second part of a split comment
110111
if (!match && i > 0 && head.value.trim().startsWith('[!code')) {
111112
// Look back to see if the previous token contains the comment prefix
112113
const prevToken = elements[i - 1]
113114
if (prevToken?.type === 'element') {
114115
const prevHead = prevToken.children.at(0)
115-
if (prevHead?.type === 'text' && prevHead.value.includes('//')) {
116+
if (prevHead?.type === 'text' && (prevHead.value.includes('//') || RE_LINE_COMMENT_PREFIX.test(prevHead.value.trim()))) {
116117
const combinedValue = prevHead.value + head.value
117118
const combinedMatch = matchToken(combinedValue, isLast)
118119
if (combinedMatch) {

‎packages/transformers/test/notation-diff-rose-pine.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,48 @@ const c = 3 // [!code highlight]`
220220
expect(html).toContain('diff remove')
221221
expect(html).not.toContain('[!code --]')
222222
})
223+
224+
it('transformerNotationDiff works with rose-pine theme for `#` and `--` comments', async () => {
225+
using highlighter = await createHighlighter({
226+
themes: ['rose-pine-dawn'],
227+
langs: ['yaml', 'bash', 'sql'],
228+
})
229+
230+
const cases = [
231+
['yaml', 'foo: bar # [!code ++]'],
232+
['bash', 'echo hi # [!code ++]'],
233+
['sql', 'SELECT 1 -- [!code ++]'],
234+
] as const
235+
236+
for (const [lang, code] of cases) {
237+
const html = highlighter.codeToHtml(code, {
238+
lang,
239+
theme: 'rose-pine-dawn',
240+
transformers: [transformerNotationDiff()],
241+
})
242+
243+
expect(html).toContain('diff add')
244+
expect(html).not.toContain('[!code ++]')
245+
}
246+
})
247+
248+
it('removes comment-only lines with `#` comments in rose-pine theme', async () => {
249+
using highlighter = await createHighlighter({
250+
themes: ['rose-pine'],
251+
langs: ['yaml'],
252+
})
253+
254+
const code = `# [!code highlight:2]
255+
foo: 1
256+
bar: 2`
257+
258+
const html = highlighter.codeToHtml(code, {
259+
lang: 'yaml',
260+
theme: 'rose-pine',
261+
transformers: [transformerNotationHighlight()],
262+
})
263+
264+
expect(html).not.toContain('[!code')
265+
expect(html.match(/class="line highlighted"/g)).toHaveLength(2)
266+
})
223267
})

0 commit comments

Comments
 (0)