Skip to content

Commit ea17dcc

Browse files
author
Tom Schaible
committed
updated the insertion of embedded tokens so it correctly handles embedded token results returning in any order
fixes #970
1 parent 9e62be9 commit ea17dcc

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/core/render/embed.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import stripIndent from 'strip-indent';
12
import { get } from '../fetch/ajax';
23
import { merge } from '../util/core';
3-
import stripIndent from 'strip-indent';
44

55
const cached = {};
66

@@ -117,17 +117,27 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
117117
}
118118
});
119119

120-
let moveIndex = 0;
120+
// keep track of which tokens have been embedded so far
121+
// so that we know where to insert the embedded tokens as they
122+
// are returned
123+
const moves = [];
121124
walkFetchEmbed({ compile, embedTokens, fetch }, ({ embedToken, token }) => {
122125
if (token) {
123-
const index = token.index + moveIndex;
126+
// iterate through the array of previously inserted tokens
127+
// to determine where the current embedded tokens should be inserted
128+
let index = token.index;
129+
moves.forEach(pos => {
130+
if (index > pos.start) {
131+
index += pos.length;
132+
}
133+
});
124134

125135
merge(links, embedToken.links);
126136

127137
tokens = tokens
128138
.slice(0, index)
129139
.concat(embedToken, tokens.slice(index + 1));
130-
moveIndex += embedToken.length - 1;
140+
moves.push({ start: index, length: embedToken.length - 1 });
131141
} else {
132142
cached[raw] = tokens.concat();
133143
tokens.links = cached[raw].links = links;

0 commit comments

Comments
 (0)