({ compiler, raw = '', fetch }, done)
| 92 | } |
| 93 | |
| 94 | export function prerenderEmbed({ compiler, raw = '', fetch }, done) { |
| 95 | let hit = cached[raw]; |
| 96 | if (hit) { |
| 97 | const copy = hit.slice(); |
| 98 | copy.links = hit.links; |
| 99 | return done(copy); |
| 100 | } |
| 101 | |
| 102 | const compile = compiler._marked; |
| 103 | let tokens = compile.lexer(raw); |
| 104 | const embedTokens = []; |
| 105 | const linkRE = compile.Lexer.rules.inline.link; |
| 106 | const links = tokens.links; |
| 107 | |
| 108 | tokens.forEach((token, index) => { |
| 109 | if (token.type === 'paragraph') { |
| 110 | token.text = token.text.replace( |
| 111 | new RegExp(linkRE.source, 'g'), |
| 112 | (src, filename, href, title) => { |
| 113 | const embed = compiler.compileEmbed(href, title); |
| 114 | |
| 115 | if (embed) { |
| 116 | embedTokens.push({ |
| 117 | index, |
| 118 | embed, |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | return src; |
| 123 | } |
| 124 | ); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | // keep track of which tokens have been embedded so far |
| 129 | // so that we know where to insert the embedded tokens as they |
| 130 | // are returned |
| 131 | const moves = []; |
| 132 | walkFetchEmbed({ compile, embedTokens, fetch }, ({ embedToken, token }) => { |
| 133 | if (token) { |
| 134 | // iterate through the array of previously inserted tokens |
| 135 | // to determine where the current embedded tokens should be inserted |
| 136 | let index = token.index; |
| 137 | moves.forEach(pos => { |
| 138 | if (index > pos.start) { |
| 139 | index += pos.length; |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | merge(links, embedToken.links); |
| 144 | |
| 145 | tokens = tokens |
| 146 | .slice(0, index) |
| 147 | .concat(embedToken, tokens.slice(index + 1)); |
| 148 | moves.push({ start: index, length: embedToken.length - 1 }); |
| 149 | } else { |
| 150 | cached[raw] = tokens.concat(); |
| 151 | tokens.links = cached[raw].links = links; |
no test coverage detected
searching dependent graphs…