@@ -2,12 +2,13 @@ import { get } from '../fetch/ajax';
22import { merge } from '../util/core' ;
33import stripIndent from 'strip-indent' ;
44
5+ const INCLUDE_COMPONENT = '__include__' ;
56const cached = { } ;
67
78function walkFetchEmbed ( { embedTokens, compile, fetch } , cb ) {
89 let token ;
910 let step = 0 ;
10- let count = 1 ;
11+ let count = 0 ;
1112
1213 if ( ! embedTokens . length ) {
1314 return cb ( { } ) ;
@@ -48,7 +49,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
4849 }
4950
5051 cb ( { token, embedToken } ) ;
51- if ( ++ count >= step ) {
52+ if ( ++ count >= embedTokens . length ) {
5253 cb ( { } ) ;
5354 }
5455 } ;
@@ -66,6 +67,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
6667 }
6768}
6869
70+ function expandInclude ( tokens ) {
71+ if ( ! tokens ) {
72+ return tokens ;
73+ }
74+
75+ const expandedTokens = [ ] ;
76+ tokens . forEach ( e => {
77+ if ( e . type === INCLUDE_COMPONENT && e . components ) {
78+ e . components . forEach ( c => expandedTokens . push ( c ) ) ;
79+ } else {
80+ expandedTokens . push ( e ) ;
81+ }
82+ } ) ;
83+
84+ return expandedTokens ;
85+ }
86+
6987export function prerenderEmbed ( { compiler, raw = '' , fetch } , done ) {
7088 let hit = cached [ raw ] ;
7189 if ( hit ) {
@@ -100,18 +118,19 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
100118 }
101119 } ) ;
102120
103- let moveIndex = 0 ;
104121 walkFetchEmbed ( { compile, embedTokens, fetch } , ( { embedToken, token } ) => {
105122 if ( token ) {
106- const index = token . index + moveIndex ;
107-
108123 merge ( links , embedToken . links ) ;
109124
110- tokens = tokens
111- . slice ( 0 , index )
112- . concat ( embedToken , tokens . slice ( index + 1 ) ) ;
113- moveIndex += embedToken . length - 1 ;
125+ tokens = tokens . slice ( 0 , token . index ) . concat (
126+ {
127+ type : INCLUDE_COMPONENT ,
128+ components : embedToken ,
129+ } ,
130+ tokens . slice ( token . index + 1 )
131+ ) ;
114132 } else {
133+ tokens = expandInclude ( tokens ) ;
115134 cached [ raw ] = tokens . concat ( ) ;
116135 tokens . links = cached [ raw ] . links = links ;
117136 done ( tokens ) ;
0 commit comments