updated the insertion of embedded tokens so it correctly handles embedded token results returning in any order

fixes #970
This commit is contained in:
Tom Schaible 2020-04-02 08:27:03 -04:00
parent 9e62be9d00
commit ea17dcc66c

View File

@ -1,6 +1,6 @@
import stripIndent from 'strip-indent';
import { get } from '../fetch/ajax';
import { merge } from '../util/core';
import stripIndent from 'strip-indent';
const cached = {};
@ -117,17 +117,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;