@@ -42,6 +42,7 @@ module.exports = function snippet (md, options = {}) {
4242 }
4343
4444 function parser ( state , startLine , endLine , silent ) {
45+ const { $relativePath } = state . md || { } ;
4546 const CH = '<' . charCodeAt ( 0 )
4647 const pos = state . bMarks [ startLine ] + state . tShift [ startLine ]
4748 const max = state . eMarks [ startLine ]
@@ -76,14 +77,24 @@ module.exports = function snippet (md, options = {}) {
7677 */
7778 const rawPathRegexp = / ^ ( .+ ?(?: \. ( [ a - z ] + ) ) ? ) (?: ( # [ \w - ] + ) ) ? (?: ? ( { \d + (?: [ , - ] \d + ) * } ) ) ? $ /
7879
79- const rawPath = state . src . slice ( start , end ) . trim ( )
80+ // 通过 @ 识别根目录
81+ const rawPath = state . src . slice ( start , end ) . trim ( ) ;
8082 const [ filename = '' , extension = '' , region = '' , meta = '' ] = ( rawPathRegexp . exec ( rawPath ) || [ ] ) . slice ( 1 )
83+ let _filename = filename ;
84+ try {
85+ _filename = require . resolve ( _filename ) ;
86+ } catch ( error ) {
87+ _filename = _filename . replace ( / ^ @ / , root ) . trim ( ) ;
88+ }
8189
8290 state . line = startLine + 1
8391
8492 const token = state . push ( 'fence' , 'code' , 0 )
8593 token . info = extension + meta
86- token . src = path . resolve ( root , filename ) + region
94+ token . src = ( $relativePath
95+ ? path . resolve ( root , path . dirname ( $relativePath ) , _filename ) // 相对路径
96+ : path . resolve ( root , _filename ) ) + region
97+ // token.src = path.resolve(root, filename) + region
8798 token . markup = '```'
8899 token . map = [ startLine , startLine + 1 ]
89100
0 commit comments