@@ -10,88 +10,93 @@ const { mdExplodeIncludes } = require('./util')
10
10
const cache = LRU ( { max : 1000 } )
11
11
const devCache = LRU ( { max : 1000 } )
12
12
13
- module . exports = function ( src ) {
14
- const file = this . resourcePath
15
- const dir = path . dirname ( file )
13
+ module . exports = async function ( src ) {
14
+ const cb = this . async ( )
15
+ try {
16
+ const file = this . resourcePath
17
+ const dir = path . dirname ( file )
16
18
17
- const { explodedSrc, dependencies } = mdExplodeIncludes ( { dir, src } )
18
- dependencies . forEach ( d => this . addDependency ( d ) )
19
+ const { explodedSrc, dependencies } = await mdExplodeIncludes ( { cwd : dir , src } )
20
+ dependencies . forEach ( d => this . addDependency ( d ) )
19
21
20
- const isProd = process . env . NODE_ENV === 'production'
21
- const isServer = this . target === 'node'
22
- const { markdown, sourceDir } = getOptions ( this )
22
+ const isProd = process . env . NODE_ENV === 'production'
23
+ const isServer = this . target === 'node'
24
+ const { markdown, sourceDir } = getOptions ( this )
23
25
24
- // we implement a manual cache here because this loader is chained before
25
- // vue-loader, and will be applied on the same file multiple times when
26
- // selecting the individual blocks.
27
- const key = hash ( file + explodedSrc )
28
- const cached = cache . get ( key )
29
- if ( cached && ( isProd || / \? v u e / . test ( this . resourceQuery ) ) ) {
30
- return cached
31
- }
26
+ // we implement a manual cache here because this loader is chained before
27
+ // vue-loader, and will be applied on the same file multiple times when
28
+ // selecting the individual blocks.
29
+ const key = hash ( file + explodedSrc )
30
+ const cached = cache . get ( key )
31
+ if ( cached && ( isProd || / \? v u e / . test ( this . resourceQuery ) ) ) {
32
+ return cb ( null , cached )
33
+ }
32
34
33
- const frontmatter = parseFrontmatter ( explodedSrc )
34
- const content = frontmatter . content
35
+ const frontmatter = parseFrontmatter ( explodedSrc )
36
+ const content = frontmatter . content
35
37
36
- if ( ! isProd && ! isServer ) {
37
- const inferredTitle = inferTitle ( frontmatter )
38
- const headers = extractHeaders ( content , [ 'h2' , 'h3' ] , markdown )
39
- delete frontmatter . content
38
+ if ( ! isProd && ! isServer ) {
39
+ const inferredTitle = inferTitle ( frontmatter )
40
+ const headers = extractHeaders ( content , [ 'h2' , 'h3' ] , markdown )
41
+ delete frontmatter . content
40
42
41
- // diff frontmatter and title, since they are not going to be part of the
42
- // returned component, changes in frontmatter do not trigger proper updates
43
- const cachedData = devCache . get ( file )
44
- if ( cachedData && (
45
- cachedData . inferredTitle !== inferredTitle ||
46
- JSON . stringify ( cachedData . frontmatter ) !== JSON . stringify ( frontmatter ) ||
47
- headersChanged ( cachedData . headers , headers )
48
- ) ) {
49
- // frontmatter changed... need to do a full reload
50
- module . exports . frontmatterEmitter . emit ( 'update' )
51
- }
43
+ // diff frontmatter and title, since they are not going to be part of the
44
+ // returned component, changes in frontmatter do not trigger proper updates
45
+ const cachedData = devCache . get ( file )
46
+ if ( cachedData && (
47
+ cachedData . inferredTitle !== inferredTitle ||
48
+ JSON . stringify ( cachedData . frontmatter ) !== JSON . stringify ( frontmatter ) ||
49
+ headersChanged ( cachedData . headers , headers )
50
+ ) ) {
51
+ // frontmatter changed... need to do a full reload
52
+ module . exports . frontmatterEmitter . emit ( 'update' )
53
+ }
52
54
53
- devCache . set ( file , {
54
- headers,
55
- frontmatter,
56
- inferredTitle
57
- } )
58
- }
55
+ devCache . set ( file , {
56
+ headers,
57
+ frontmatter,
58
+ inferredTitle
59
+ } )
60
+ }
59
61
60
- // the render method has been augmented to allow plugins to
61
- // register data during render
62
- const { html, data : { hoistedTags, links } } = markdown . render ( content )
62
+ // the render method has been augmented to allow plugins to
63
+ // register data during render
64
+ const { html, data : { hoistedTags, links } } = markdown . render ( content )
63
65
64
- // check if relative links are valid
65
- links && links . forEach ( link => {
66
- const shortname = link
67
- . replace ( / # .* $ / , '' )
68
- . replace ( / \. h t m l $ / , '.md' )
69
- const filename = shortname
70
- . replace ( / \/ $ / , '/README.md' )
71
- . replace ( / ^ \/ / , sourceDir + '/' )
72
- const altname = shortname
73
- . replace ( / \/ $ / , '/index.md' )
74
- . replace ( / ^ \/ / , sourceDir + '/' )
75
- const file = path . resolve ( dir , filename )
76
- const altfile = altname !== filename ? path . resolve ( dir , altname ) : null
77
- if ( ! fs . existsSync ( file ) && ( ! altfile || ! fs . existsSync ( altfile ) ) ) {
78
- this . emitWarning (
79
- new Error (
80
- `\nFile for relative link "${ link } " does not exist.\n` +
81
- `(Resolved file: ${ file } )\n`
66
+ // check if relative links are valid
67
+ links && links . forEach ( link => {
68
+ const shortname = link
69
+ . replace ( / # .* $ / , '' )
70
+ . replace ( / \. h t m l $ / , '.md' )
71
+ const filename = shortname
72
+ . replace ( / \/ $ / , '/README.md' )
73
+ . replace ( / ^ \/ / , sourceDir + '/' )
74
+ const altname = shortname
75
+ . replace ( / \/ $ / , '/index.md' )
76
+ . replace ( / ^ \/ / , sourceDir + '/' )
77
+ const file = path . resolve ( dir , filename )
78
+ const altfile = altname !== filename ? path . resolve ( dir , altname ) : null
79
+ if ( ! fs . existsSync ( file ) && ( ! altfile || ! fs . existsSync ( altfile ) ) ) {
80
+ this . emitWarning (
81
+ new Error (
82
+ `\nFile for relative link "${ link } " does not exist.\n` +
83
+ `(Resolved file: ${ file } )\n`
84
+ )
82
85
)
83
- )
84
- }
85
- } )
86
+ }
87
+ } )
86
88
87
- const res = (
88
- `<template>\n` +
89
- `<div class="content">${ html } </div>\n` +
90
- `</template>\n` +
91
- ( hoistedTags || [ ] ) . join ( '\n' )
92
- )
93
- cache . set ( key , res )
94
- return res
89
+ const res = (
90
+ `<template>\n` +
91
+ `<div class="content">${ html } </div>\n` +
92
+ `</template>\n` +
93
+ ( hoistedTags || [ ] ) . join ( '\n' )
94
+ )
95
+ cache . set ( key , res )
96
+ return cb ( null , res )
97
+ } catch ( e ) {
98
+ return cb ( e )
99
+ }
95
100
}
96
101
97
102
function headersChanged ( a , b ) {
0 commit comments