Skip to content

Commit f04adbf

Browse files
shigmaulivz
authored andcommitted
feat($markdown): cache parser (#1359)
1 parent 8f83a17 commit f04adbf

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

packages/@vuepress/markdown-loader/index.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
const { EventEmitter } = require('events')
88
const { getOptions } = require('loader-utils')
9-
const { fs, path, hash, parseFrontmatter, inferTitle, extractHeaders } = require('@vuepress/shared-utils')
9+
const { fs, path, parseFrontmatter, inferTitle, extractHeaders } = require('@vuepress/shared-utils')
1010
const LRU = require('lru-cache')
1111
const md = require('@vuepress/markdown')
1212

13-
const cache = new LRU({ max: 1000 })
1413
const devCache = new LRU({ max: 1000 })
1514

1615
/**
@@ -32,26 +31,18 @@ module.exports = function (src) {
3231
// vue-loader, and will be applied on the same file multiple times when
3332
// selecting the individual blocks.
3433
const file = this.resourcePath
35-
const key = hash(file + src)
36-
const cached = cache.get(key)
37-
if (cached && (isProd || /\?vue/.test(this.resourceQuery))) {
38-
return cached
39-
}
40-
41-
const frontmatter = parseFrontmatter(src)
42-
const content = frontmatter.content
34+
const { content, data } = parseFrontmatter(src)
4335

4436
if (!isProd && !isServer) {
45-
const inferredTitle = inferTitle(frontmatter.data, frontmatter.content)
37+
const inferredTitle = inferTitle(data, content)
4638
const headers = extractHeaders(content, ['h2', 'h3'], markdown)
47-
delete frontmatter.content
4839

4940
// diff frontmatter and title, since they are not going to be part of the
5041
// returned component, changes in frontmatter do not trigger proper updates
5142
const cachedData = devCache.get(file)
5243
if (cachedData && (
5344
cachedData.inferredTitle !== inferredTitle
54-
|| JSON.stringify(cachedData.frontmatterData) !== JSON.stringify(frontmatter.data)
45+
|| JSON.stringify(cachedData.frontmatterData) !== JSON.stringify(data)
5546
|| headersChanged(cachedData.headers, headers)
5647
)) {
5748
// frontmatter changed... need to do a full reload
@@ -60,7 +51,7 @@ module.exports = function (src) {
6051

6152
devCache.set(file, {
6253
headers,
63-
frontmatterData: frontmatter.data,
54+
frontmatterData: data,
6455
inferredTitle
6556
})
6657
}
@@ -73,7 +64,7 @@ module.exports = function (src) {
7364
dataBlockString
7465
} = markdown.render(content, {
7566
loader,
76-
frontmatter: frontmatter.data,
67+
frontmatter: data,
7768
relPath: path.relative(sourceDir, file)
7869
})
7970

@@ -114,7 +105,6 @@ module.exports = function (src) {
114105
+ (hoistedTags || []).join('\n')
115106
+ `\n${dataBlockString}\n`
116107
)
117-
cache.set(key, res)
118108
return res
119109
}
120110

packages/@vuepress/markdown-loader/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
],
2020
"dependencies": {
2121
"@vuepress/markdown": "^1.0.0-alpha.39",
22-
"loader-utils": "^1.1.0"
22+
"loader-utils": "^1.1.0",
23+
"lru-cache": "^5.1.1"
2324
},
2425
"author": "Evan You",
2526
"maintainers": [

packages/@vuepress/markdown/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
const Config = require('markdown-it-chain')
8+
const LRUCache = require('lru-cache')
89
const highlight = require('./lib/highlight')
910
const { PLUGINS, REQUIRED_PLUGINS } = require('./lib/constant')
1011
const highlightLinesPlugin = require('./lib/highlightLines')
@@ -19,7 +20,7 @@ const snippetPlugin = require('./lib/snippet')
1920
const emojiPlugin = require('markdown-it-emoji')
2021
const anchorPlugin = require('markdown-it-anchor')
2122
const tocPlugin = require('markdown-it-table-of-contents')
22-
const { parseHeaders, slugify: _slugify, logger, chalk } = require('@vuepress/shared-utils')
23+
const { parseHeaders, slugify: _slugify, logger, chalk, hash } = require('@vuepress/shared-utils')
2324

2425
/**
2526
* Create markdown by config.
@@ -115,6 +116,21 @@ module.exports = (markdown = {}) => {
115116

116117
afterInstantiate && afterInstantiate(md)
117118

119+
// override parse to allow cache
120+
const parse = md.parse
121+
const cache = new LRUCache({ max: 1000 })
122+
md.parse = (src, env) => {
123+
const key = hash(src + env.relPath)
124+
const cached = cache.get(key)
125+
if (cached) {
126+
return cached
127+
} else {
128+
const tokens = parse.call(md, src, env)
129+
cache.set(key, tokens)
130+
return tokens
131+
}
132+
}
133+
118134
module.exports.dataReturnable(md)
119135

120136
// expose slugify

packages/@vuepress/markdown/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@vuepress/shared-utils": "^1.0.0-alpha.39",
23+
"lru-cache": "^5.1.1",
2324
"markdown-it": "^8.4.1",
2425
"markdown-it-anchor": "^5.0.2",
2526
"markdown-it-chain": "^1.3.0",

0 commit comments

Comments
 (0)