Skip to content

Commit fda5476

Browse files
authored
Revert "feat($markdown): cache parser (#1359)" (#1596)
This reverts commit f04adbf.
1 parent fbbd677 commit fda5476

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

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

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

77
const { EventEmitter } = require('events')
88
const { getOptions } = require('loader-utils')
9-
const { fs, path, parseFrontmatter, inferTitle, extractHeaders } = require('@vuepress/shared-utils')
9+
const { fs, path, hash, 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 })
1314
const devCache = new LRU({ max: 1000 })
1415

1516
/**
@@ -31,18 +32,26 @@ module.exports = function (src) {
3132
// vue-loader, and will be applied on the same file multiple times when
3233
// selecting the individual blocks.
3334
const file = this.resourcePath
34-
const { content, data } = parseFrontmatter(src)
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
3543

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

4049
// diff frontmatter and title, since they are not going to be part of the
4150
// returned component, changes in frontmatter do not trigger proper updates
4251
const cachedData = devCache.get(file)
4352
if (cachedData && (
4453
cachedData.inferredTitle !== inferredTitle
45-
|| JSON.stringify(cachedData.frontmatterData) !== JSON.stringify(data)
54+
|| JSON.stringify(cachedData.frontmatterData) !== JSON.stringify(frontmatter.data)
4655
|| headersChanged(cachedData.headers, headers)
4756
)) {
4857
// frontmatter changed... need to do a full reload
@@ -51,7 +60,7 @@ module.exports = function (src) {
5160

5261
devCache.set(file, {
5362
headers,
54-
frontmatterData: data,
63+
frontmatterData: frontmatter.data,
5564
inferredTitle
5665
})
5766
}
@@ -64,7 +73,7 @@ module.exports = function (src) {
6473
dataBlockString
6574
} = markdown.render(content, {
6675
loader,
67-
frontmatter: data,
76+
frontmatter: frontmatter.data,
6877
relativePath: path.relative(sourceDir, file).replace(/\\/g, '/')
6978
})
7079

@@ -105,6 +114,7 @@ module.exports = function (src) {
105114
+ (hoistedTags || []).join('\n')
106115
+ `\n${dataBlockString}\n`
107116
)
117+
cache.set(key, res)
108118
return res
109119
}
110120

packages/@vuepress/markdown/index.js

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

77
const Config = require('markdown-it-chain')
8-
const LRUCache = require('lru-cache')
98
const highlight = require('./lib/highlight')
109
const { PLUGINS, REQUIRED_PLUGINS } = require('./lib/constant')
1110
const highlightLinesPlugin = require('./lib/highlightLines')
@@ -21,7 +20,7 @@ const tocPlugin = require('markdown-it-table-of-contents')
2120
const {
2221
slugify: _slugify,
2322
parseHeaders,
24-
logger, chalk, hash, normalizeConfig,
23+
logger, chalk, normalizeConfig,
2524
moduleResolver: { getMarkdownItResolver }
2625
} = require('@vuepress/shared-utils')
2726

@@ -124,21 +123,6 @@ module.exports = (markdown = {}) => {
124123

125124
afterInstantiate && afterInstantiate(md)
126125

127-
// override parse to allow cache
128-
const parse = md.parse
129-
const cache = new LRUCache({ max: 1000 })
130-
md.parse = (src, env) => {
131-
const key = hash(src + env.relativePath)
132-
const cached = cache.get(key)
133-
if (cached) {
134-
return cached
135-
} else {
136-
const tokens = parse.call(md, src, env)
137-
cache.set(key, tokens)
138-
return tokens
139-
}
140-
}
141-
142126
module.exports.dataReturnable(md)
143127

144128
// expose slugify

packages/@vuepress/markdown/package.json

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

0 commit comments

Comments
 (0)