1
- const { fs } = require ( '@vuepress/shared-utils' )
1
+ const { fs, path } = require ( '@vuepress/shared-utils' )
2
2
3
3
module . exports = function snippet ( md , options = { } ) {
4
+ const fence = md . renderer . rules . fence
4
5
const root = options . root || process . cwd ( )
6
+
7
+ md . renderer . rules . fence = ( ...args ) => {
8
+ const [ tokens , idx , , { loader } ] = args
9
+ const token = tokens [ idx ]
10
+ const { src } = token
11
+ if ( src ) {
12
+ if ( loader ) {
13
+ loader . addDependency ( src )
14
+ }
15
+ if ( fs . existsSync ( src ) ) {
16
+ token . content = fs . readFileSync ( src , 'utf8' )
17
+ } else {
18
+ token . content = 'Not found: ' + src
19
+ token . info = ''
20
+ }
21
+ }
22
+ return fence ( ...args )
23
+ }
24
+
5
25
function parser ( state , startLine , endLine , silent ) {
6
26
const CH = '<' . charCodeAt ( 0 )
7
27
const pos = state . bMarks [ startLine ] + state . tShift [ startLine ]
@@ -25,14 +45,13 @@ module.exports = function snippet (md, options = {}) {
25
45
const end = state . skipSpacesBack ( max , pos )
26
46
const rawPath = state . src . slice ( start , end ) . trim ( ) . replace ( / ^ @ / , root )
27
47
const filename = rawPath . split ( / [ { \s ] / ) . shift ( )
28
- const content = fs . existsSync ( filename ) ? fs . readFileSync ( filename ) . toString ( ) : 'Not found: ' + filename
29
48
const meta = rawPath . replace ( filename , '' )
30
49
31
50
state . line = startLine + 1
32
51
33
52
const token = state . push ( 'fence' , 'code' , 0 )
34
53
token . info = filename . split ( '.' ) . pop ( ) + meta
35
- token . content = content
54
+ token . src = path . resolve ( filename )
36
55
token . markup = '```'
37
56
token . map = [ startLine , startLine + 1 ]
38
57
0 commit comments