Skip to content

Commit 4416673

Browse files
committed
added explosion of md includes
1 parent ba36efc commit 4416673

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lib/prepare.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const tomlParser = require('toml')
66
const createMarkdown = require('./markdown')
77
const tempPath = path.resolve(__dirname, 'app/.temp')
88
const { inferTitle, extractHeaders, parseFrontmatter } = require('./util')
9+
const { mdExplodeIncludes } = require('./webpack/util')
910

1011
fs.ensureDirSync(tempPath)
1112

@@ -188,7 +189,10 @@ async function resolveOptions (sourceDir) {
188189
}
189190

190191
// extract yaml frontmatter
191-
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
192+
const { explodedSrc: content } = mdExplodeIncludes({
193+
cwd: path.dirname(path.resolve(sourceDir, file)),
194+
src: await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
195+
})
192196
const frontmatter = parseFrontmatter(content)
193197
// infer title
194198
const title = inferTitle(frontmatter)

lib/webpack/markdownLoader.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,32 @@ const { EventEmitter } = require('events')
55
const { getOptions } = require('loader-utils')
66
const { inferTitle, extractHeaders, parseFrontmatter } = require('../util')
77
const LRU = require('lru-cache')
8+
const { mdExplodeIncludes } = require('./util')
89

910
const cache = LRU({ max: 1000 })
1011
const devCache = LRU({ max: 1000 })
1112

1213
module.exports = function (src) {
14+
const file = this.resourcePath
15+
const cwd = path.dirname(file)
16+
17+
const { explodedSrc, dependencies } = mdExplodeIncludes({ cwd, src })
18+
dependencies.forEach(d => this.addDependency(d))
19+
1320
const isProd = process.env.NODE_ENV === 'production'
1421
const isServer = this.target === 'node'
1522
const { markdown, sourceDir } = getOptions(this)
1623

1724
// we implement a manual cache here because this loader is chained before
1825
// vue-loader, and will be applied on the same file multiple times when
1926
// selecting the individual blocks.
20-
const file = this.resourcePath
21-
const key = hash(file + src)
27+
const key = hash(file + explodedSrc)
2228
const cached = cache.get(key)
2329
if (cached && (isProd || /\?vue/.test(this.resourceQuery))) {
2430
return cached
2531
}
2632

27-
const frontmatter = parseFrontmatter(src)
33+
const frontmatter = parseFrontmatter(explodedSrc)
2834
const content = frontmatter.content
2935

3036
if (!isProd && !isServer) {
@@ -66,9 +72,8 @@ module.exports = function (src) {
6672
const altname = shortname
6773
.replace(/\/$/, '/index.md')
6874
.replace(/^\//, sourceDir + '/')
69-
const dir = path.dirname(this.resourcePath)
70-
const file = path.resolve(dir, filename)
71-
const altfile = altname !== filename ? path.resolve(dir, altname) : null
75+
const file = path.resolve(cwd, filename)
76+
const altfile = altname !== filename ? path.resolve(cwd, altname) : null
7277
if (!fs.existsSync(file) && (altfile && !fs.existsSync(altfile))) {
7378
this.emitWarning(
7479
new Error(

lib/webpack/util.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { readFileSync } = require('fs')
2+
const { resolve, dirname } = require('path')
3+
4+
const mdExplodeIncludes = exports.mdExplodeIncludes = ({ cwd, src }) => {
5+
const deps = []
6+
7+
return {
8+
explodedSrc: src.replace(/<!-- *include +(.*?) *-->/g, (match, path) => {
9+
try {
10+
const absolutePath = resolve(cwd, path)
11+
const content = readFileSync(absolutePath, 'utf8')
12+
13+
// recursively explode the included file
14+
const { explodedSrc, dependencies } = mdExplodeIncludes({
15+
cwd: dirname(absolutePath),
16+
src: content
17+
})
18+
19+
deps.push(absolutePath, ...dependencies)
20+
return explodedSrc
21+
} catch (e) {
22+
return match
23+
}
24+
}),
25+
dependencies: deps
26+
}
27+
}

0 commit comments

Comments
 (0)