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