Skip to content

Commit e8f7dd1

Browse files
authored
feat: support adding extra attributes to snippet imports (useful for twoslash) (#4100)
1 parent 315c220 commit e8f7dd1

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/node/markdown/plugins/snippet.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { MarkdownEnv } from '../../shared'
1616
* captures: ['/path/to/file.extension', 'extension', '#region', '{meta}', '[title]']
1717
*/
1818
export const rawPathRegexp =
19-
/^(.+?(?:(?:\.([a-z0-9]+))?))(?:(#[\w-]+))?(?: ?(?:{(\d+(?:[,-]\d+)*)? ?(\S+)?}))? ?(?:\[(.+)\])?$/
19+
/^(.+?(?:(?:\.([a-z0-9]+))?))(?:(#[\w-]+))?(?: ?(?:{(\d+(?:[,-]\d+)*)? ?(\S+)? ?(\S+)?}))? ?(?:\[(.+)\])?$/
2020

2121
export function rawPathToToken(rawPath: string) {
2222
const [
@@ -25,12 +25,13 @@ export function rawPathToToken(rawPath: string) {
2525
region = '',
2626
lines = '',
2727
lang = '',
28+
attrs = '',
2829
rawTitle = ''
2930
] = (rawPathRegexp.exec(rawPath) || []).slice(1)
3031

3132
const title = rawTitle || filepath.split('/').pop() || ''
3233

33-
return { filepath, extension, region, lines, lang, title }
34+
return { filepath, extension, region, lines, lang, attrs, title }
3435
}
3536

3637
export function dedent(text: string): string {
@@ -126,15 +127,15 @@ export const snippetPlugin = (md: MarkdownIt, srcDir: string) => {
126127
.replace(/^@/, srcDir)
127128
.trim()
128129

129-
const { filepath, extension, region, lines, lang, title } =
130+
const { filepath, extension, region, lines, lang, attrs, title } =
130131
rawPathToToken(rawPath)
131132

132133
state.line = startLine + 1
133134

134135
const token = state.push('fence', 'code', 0)
135136
token.info = `${lang || extension}${lines ? `{${lines}}` : ''}${
136137
title ? `[${title}]` : ''
137-
}`
138+
} ${attrs ?? ''}`
138139

139140
const { realPath, path: _path } = state.env as MarkdownEnv
140141
const resolvedPath = path.resolve(path.dirname(realPath ?? _path), filepath)

0 commit comments

Comments
 (0)