@@ -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 ( { } ) ;
@@ -48,7 +49,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
48
49
}
49
50
50
51
cb ( { token, embedToken } ) ;
51
- if ( ++ count >= step ) {
52
+ if ( ++ count >= embedTokens . length ) {
52
53
cb ( { } ) ;
53
54
}
54
55
} ;
@@ -66,6 +67,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
66
67
}
67
68
}
68
69
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
+
69
87
export function prerenderEmbed ( { compiler, raw = '' , fetch } , done ) {
70
88
let hit = cached [ raw ] ;
71
89
if ( hit ) {
@@ -100,18 +118,19 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
100
118
}
101
119
} ) ;
102
120
103
- let moveIndex = 0 ;
104
121
walkFetchEmbed ( { compile, embedTokens, fetch } , ( { embedToken, token } ) => {
105
122
if ( token ) {
106
- const index = token . index + moveIndex ;
107
-
108
123
merge ( links , embedToken . links ) ;
109
124
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
+ ) ;
114
132
} else {
133
+ tokens = expandInclude ( tokens ) ;
115
134
cached [ raw ] = tokens . concat ( ) ;
116
135
tokens . links = cached [ raw ] . links = links ;
117
136
done ( tokens ) ;
0 commit comments