Skip to content

Commit

Permalink
feat: support adding extra attributes to snippet imports (useful for …
Browse files Browse the repository at this point in the history
…twoslash) (#4100)
  • Loading branch information
Thy3634 authored Sep 1, 2024
1 parent 315c220 commit e8f7dd1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/node/markdown/plugins/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { MarkdownEnv } from '../../shared'
* captures: ['/path/to/file.extension', 'extension', '#region', '{meta}', '[title]']
*/
export const rawPathRegexp =
/^(.+?(?:(?:\.([a-z0-9]+))?))(?:(#[\w-]+))?(?: ?(?:{(\d+(?:[,-]\d+)*)? ?(\S+)?}))? ?(?:\[(.+)\])?$/
/^(.+?(?:(?:\.([a-z0-9]+))?))(?:(#[\w-]+))?(?: ?(?:{(\d+(?:[,-]\d+)*)? ?(\S+)? ?(\S+)?}))? ?(?:\[(.+)\])?$/

export function rawPathToToken(rawPath: string) {
const [
Expand All @@ -25,12 +25,13 @@ export function rawPathToToken(rawPath: string) {
region = '',
lines = '',
lang = '',
attrs = '',
rawTitle = ''
] = (rawPathRegexp.exec(rawPath) || []).slice(1)

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

return { filepath, extension, region, lines, lang, title }
return { filepath, extension, region, lines, lang, attrs, title }
}

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

const { filepath, extension, region, lines, lang, title } =
const { filepath, extension, region, lines, lang, attrs, title } =
rawPathToToken(rawPath)

state.line = startLine + 1

const token = state.push('fence', 'code', 0)
token.info = `${lang || extension}${lines ? `{${lines}}` : ''}${
title ? `[${title}]` : ''
}`
} ${attrs ?? ''}`

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

0 comments on commit e8f7dd1

Please sign in to comment.