@@ -2,12 +2,13 @@ import { get } from '../fetch/ajax';
2
2
import { merge } from '../util/core' ;
3
3
import stripIndent from 'strip-indent' ;
4
4
5
+ const INCLUDE_COMPONENT = '__include__' ;
5
6
const cached = { } ;
6
7
7
8
function walkFetchEmbed ( { embedTokens, compile, fetch } , cb ) {
8
9
let token ;
9
10
let step = 0 ;
10
- let count = 1 ;
11
+ let count = 0 ;
11
12
12
13
if ( ! embedTokens . length ) {
13
14
return cb ( { } ) ;
@@ -65,7 +66,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
65
66
}
66
67
67
68
cb ( { token, embedToken } ) ;
68
- if ( ++ count >= step ) {
69
+ if ( ++ count >= embedTokens . length ) {
69
70
cb ( { } ) ;
70
71
}
71
72
} ;
@@ -83,6 +84,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
83
84
}
84
85
}
85
86
87
+ function expandInclude ( tokens ) {
88
+ if ( ! tokens ) {
89
+ return tokens ;
90
+ }
91
+
92
+ const expandedTokens = [ ] ;
93
+ tokens . forEach ( e => {
94
+ if ( e . type === INCLUDE_COMPONENT && e . components ) {
95
+ e . components . forEach ( c => expandedTokens . push ( c ) ) ;
96
+ } else {
97
+ expandedTokens . push ( e ) ;
98
+ }
99
+ } ) ;
100
+
101
+ return expandedTokens ;
102
+ }
103
+
86
104
export function prerenderEmbed ( { compiler, raw = '' , fetch } , done ) {
87
105
let hit = cached [ raw ] ;
88
106
if ( hit ) {
@@ -117,18 +135,19 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
117
135
}
118
136
} ) ;
119
137
120
- let moveIndex = 0 ;
121
138
walkFetchEmbed ( { compile, embedTokens, fetch } , ( { embedToken, token } ) => {
122
139
if ( token ) {
123
- const index = token . index + moveIndex ;
124
-
125
140
merge ( links , embedToken . links ) ;
126
141
127
- tokens = tokens
128
- . slice ( 0 , index )
129
- . concat ( embedToken , tokens . slice ( index + 1 ) ) ;
130
- moveIndex += embedToken . length - 1 ;
142
+ tokens = tokens . slice ( 0 , token . index ) . concat (
143
+ {
144
+ type : INCLUDE_COMPONENT ,
145
+ components : embedToken ,
146
+ } ,
147
+ tokens . slice ( token . index + 1 )
148
+ ) ;
131
149
} else {
150
+ tokens = expandInclude ( tokens ) ;
132
151
cached [ raw ] = tokens . concat ( ) ;
133
152
tokens . links = cached [ raw ] . links = links ;
134
153
done ( tokens ) ;
0 commit comments