-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code snippet hmr, fix #1309 #1316
Conversation
Something to be discussed before mergeWe use cache to speed up const parse = md.parse
const cache = new LRUCache({ max: 1000 })
md.parse = (src, env) => {
const key = hash(src + env.relPath)
const cached = cache.get(key)
if (cached) {
return cached
} else {
const tokens = parse.call(md, src, env)
cache.set(key, tokens)
return tokens
}
} And these will lead to a problem: if someone changes one of those Here is an example (markdown/lib/link.js): - return Object.assign({}, token, {
- tag: 'router-link'
+ return Object.create(token, {
+ tag: { value: 'router-link' }
}) What is the difference? The former use const hrefIndex = token.attrIndex('href')
// Error: token.attrIndex is not a function How can we fix it?By copy all the tokens before md.parse = (src, env) => {
const key = hash(src + env.relPath)
const cached = cache.get(key)
if (cached) {
return copyTokens(cached)
} else {
const tokens = parse.call(md, src, env)
cache.set(key, copyTokens(tokens))
return tokens
}
}
// I actually don't know whether we should
// prevent modification to cached tokens
function copyTokens (tokens) {
return tokens.map((token) => {
if (token.children) {
token.children = copyTokens(token.children)
}
return Object.create(token)
})
} However, I still don't know whether we should do such thing, because I think users should not change those tokens during rendering process. |
@BuptStEve Can you check if this pr fix #1309? And what do you think about the question above? |
It works. |
I think it will not work. Because |
It may be better splitted into some smaller PRs. Closed. |
Summary
What kind of change does this PR introduce? (check at least one)
If changing the UI of default theme, please provide the before/after screenshot:
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
fix #xxx[,#xxx]
, where "xxx" is the issue number)You have tested in the following browsers: (Providing a detailed version will be better.)
If adding a new feature, the PR's description includes:
To avoid wasting your time, it's best to open a feature request issue first and wait for approval before working on it.
Other information:
md.render
but use cache onmd.parse
md.renderer.render
is currently working in progress on this fork (I think it may be better using another pull request)