1- import type { ResolvedSlidevOptions } from '@slidev/types'
1+ import type { ResolvedSlidevOptions , SlideInfo } from '@slidev/types'
22import type { MarkdownExit } from 'markdown-exit'
33import fs from 'node:fs'
44import path from 'node:path'
@@ -105,7 +105,46 @@ function findRegion(lines: Array<string>, regionName: string) {
105105}
106106
107107// eslint-disable-next-line regexp/no-super-linear-backtracking
108- const RE_SNIPPET_IMPORT = / ^ < < < [ \t ] * ( \S .* ?) ( # [ \w - ] + ) ? [ \t ] * (?: [ \t ] ( \S + ?) ) ? [ \t ] * ( \{ .* ) ? $ /
108+ export const RE_SNIPPET_IMPORT = / ^ < < < [ \t ] * ( \S .* ?) ( # [ \w - ] + ) ? [ \t ] * (?: [ \t ] ( \S + ?) ) ? [ \t ] * ( \{ .* ) ? $ /
109+
110+ export function resolveSnippetImport ( lineText : string , userRoot : string , slide : SlideInfo ) {
111+ const match = lineText . trimStart ( ) . match ( RE_SNIPPET_IMPORT )
112+ if ( ! match )
113+ return null
114+
115+ let [ , filepath = '' , regionName = '' , lang = '' , meta = '' ] = match
116+ const dir = path . dirname ( slide . source . filepath )
117+ const src = slash (
118+ filepath . startsWith ( '@/' )
119+ ? path . resolve ( userRoot , filepath . slice ( 2 ) )
120+ : path . resolve ( dir , filepath ) ,
121+ )
122+
123+ lang = lang . trim ( ) || path . extname ( filepath ) . slice ( 1 )
124+ meta = meta . trim ( )
125+
126+ const isAFile = fs . existsSync ( src ) && fs . statSync ( src ) . isFile ( )
127+ if ( ! isAFile ) {
128+ throw new Error ( `Code snippet path not found: ${ src } ` )
129+ }
130+
131+ let content = fs . readFileSync ( src , 'utf8' )
132+
133+ if ( regionName ) {
134+ const lines = content . split ( RE_NEWLINE )
135+ const region = findRegion ( lines , regionName . slice ( 1 ) )
136+ if ( region ) {
137+ content = dedent (
138+ lines
139+ . slice ( region . start , region . end )
140+ . filter ( l => ! ( region . re . start . test ( l ) || region . re . end . test ( l ) ) )
141+ . join ( '\n' ) ,
142+ )
143+ }
144+ }
145+
146+ return { content, filepath, lang, meta, src }
147+ }
109148
110149export default function MarkdownItSnippet ( md : MarkdownExit , { userRoot, data : { watchFiles, slides } } : ResolvedSlidevOptions ) {
111150 md . block . ruler . before ( 'fence' , 'snippet_import' , ( state , startLine , _endLine , silent ) => {
@@ -120,8 +159,6 @@ export default function MarkdownItSnippet(md: MarkdownExit, { userRoot, data: {
120159 if ( silent )
121160 return true
122161
123- let [ , filepath = '' , regionName = '' , lang = '' , meta = '' ] = match
124-
125162 const slideNo = state . env . id ?. match ( regexSlideSourceId )
126163 const slide = slideNo ? slides [ slideNo [ 1 ] - 1 ] : null
127164
@@ -130,35 +167,12 @@ export default function MarkdownItSnippet(md: MarkdownExit, { userRoot, data: {
130167 return false
131168 }
132169
133- const dir = path . dirname ( slide . source . filepath )
134- const src = slash (
135- filepath . startsWith ( '@/' )
136- ? path . resolve ( userRoot , filepath . slice ( 2 ) )
137- : path . resolve ( dir , filepath ) ,
138- )
139-
140- lang = lang . trim ( ) || path . extname ( filepath ) . slice ( 1 )
141- meta = meta . trim ( )
142-
143- const isAFile = fs . existsSync ( src ) && fs . statSync ( src ) . isFile ( )
144- if ( ! isAFile ) {
145- throw new Error ( `Code snippet path not found: ${ src } ` )
146- }
170+ const snippet = resolveSnippetImport ( lineText , userRoot , slide )
171+ if ( ! snippet )
172+ return false
147173
148- let content = fs . readFileSync ( src , 'utf8' )
149-
150- if ( regionName ) {
151- const lines = content . split ( RE_NEWLINE )
152- const region = findRegion ( lines , regionName . slice ( 1 ) )
153- if ( region ) {
154- content = dedent (
155- lines
156- . slice ( region . start , region . end )
157- . filter ( l => ! ( region . re . start . test ( l ) || region . re . end . test ( l ) ) )
158- . join ( '\n' ) ,
159- )
160- }
161- }
174+ const { content, filepath, src } = snippet
175+ let { lang, meta } = snippet
162176
163177 if ( meta . includes ( '{monaco-write}' ) ) {
164178 monacoWriterWhitelist . add ( filepath )
0 commit comments