Merge pull request #1102 from tschaible/bugfix/multiple-embeds

fix: updated the insertion of embedded tokens for async returns (fixes #970)
This commit is contained in:
Anix 2020-05-22 15:29:10 +05:30 committed by GitHub
commit 5ea28c465a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -124,17 +124,27 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
}
});
let moveIndex = 0;
// keep track of which tokens have been embedded so far
// so that we know where to insert the embedded tokens as they
// are returned
const moves = [];
walkFetchEmbed({ compile, embedTokens, fetch }, ({ embedToken, token }) => {
if (token) {
const index = token.index + moveIndex;
// iterate through the array of previously inserted tokens
// to determine where the current embedded tokens should be inserted
let index = token.index;
moves.forEach(pos => {
if (index > pos.start) {
index += pos.length;
}
});
merge(links, embedToken.links);
tokens = tokens
.slice(0, index)
.concat(embedToken, tokens.slice(index + 1));
moveIndex += embedToken.length - 1;
moves.push({ start: index, length: embedToken.length - 1 });
} else {
cached[raw] = tokens.concat();
tokens.links = cached[raw].links = links;