Skip to content

Commit 0599626

Browse files
authored
Prevent removal of Twoslash // @filename directives (#428)
1 parent e9d2218 commit 0599626

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

.changeset/light-aliens-cross.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@expressive-code/plugin-frames': patch
3+
'astro-expressive-code': patch
4+
'expressive-code': patch
5+
'rehype-expressive-code': patch
6+
---
7+
8+
Prevents the frames plugin from treating Twoslash `// @filename` directives as filename comments. This keeps multi-file Twoslash code blocks intact. Thank you for the report, @Adammatthiesen!

packages/@expressive-code/plugin-frames/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ export function getFileNameFromComment(line: string, lang: string): string | und
117117
const possibleFileName = matches?.[2]
118118
if (!possibleFileName) return
119119

120+
// Keep Twoslash VFS directives in place so downstream plugins can process them.
121+
if (textBeforeFileName.trim().toLowerCase() === '@filename') return
122+
120123
// Ignore strings that only consist of special characters (dots, path separators, etc.)
121124
if (!possibleFileName.match(/[^.:/\\~]/)) return
122125

packages/@expressive-code/plugin-frames/test/preprocess-code.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,24 @@ npm install expressive-code
302302
})
303303

304304
describe('Leaves comments unlikely to be file names untouched', () => {
305+
test('Twoslash virtual file directives', async () => {
306+
const code = `
307+
// @filename: module.ts
308+
export const testValue = 'ok'
309+
310+
// @filename: index.ts
311+
import { testValue } from "module"
312+
`
313+
await expectCodeResult({
314+
code,
315+
language: 'ts',
316+
expected: {
317+
title: undefined,
318+
code: code.trim(),
319+
},
320+
})
321+
})
322+
305323
test('Comments after line 4', async () => {
306324
const code = `
307325
Line 1

0 commit comments

Comments
 (0)