fix: loading embed files synchronously, fixed #525, fixed #527 (#544)

* fix: loading embed files synchronously, fixed #525, fixed #527

* fix
This commit is contained in:
cinwell.li 2018-06-29 21:27:07 +08:00 committed by GitHub
commit feea7f9b48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,36 +3,44 @@ import {merge} from '../util/core'
const cached = {} const cached = {}
function walkFetchEmbed({step = 0, embedTokens, compile, fetch}, cb) { function walkFetchEmbed({embedTokens, compile, fetch}, cb) {
const token = embedTokens[step] let token
let step = 0
let count = 1
if (!token) { if (!embedTokens.length) {
return cb({}) return cb({})
} }
const next = text => { while ((token = embedTokens[step++])) {
let embedToken const next = (function (token) {
if (text) { return text => {
if (token.embed.type === 'markdown') { let embedToken
embedToken = compile.lexer(text) if (text) {
} else if (token.embed.type === 'code') { if (token.embed.type === 'markdown') {
embedToken = compile.lexer( embedToken = compile.lexer(text)
'```' + } else if (token.embed.type === 'code') {
token.embed.lang + embedToken = compile.lexer(
'\n' + '```' +
text.replace(/`/g, '@DOCSIFY_QM@') + token.embed.lang +
'\n```\n' '\n' +
) text.replace(/`/g, '@DOCSIFY_QM@') +
'\n```\n'
)
}
}
cb({token, embedToken})
if (++count >= step) {
cb({})
}
} }
} })(token)
cb({token, embedToken})
walkFetchEmbed({step: ++step, compile, embedTokens, fetch}, cb)
}
if (process.env.SSR) { if (process.env.SSR) {
fetch(token.embed.url).then(next) fetch(token.embed.url).then(next)
} else { } else {
get(token.embed.url).then(next) get(token.embed.url).then(next)
}
} }
} }