@@ -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 ( { } )
@@ -46,7 +47,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
46
47
}
47
48
48
49
cb ( { token, embedToken } )
49
- if ( ++ count >= step ) {
50
+ if ( ++ count >= embedTokens . length ) {
50
51
cb ( { } )
51
52
}
52
53
}
@@ -64,6 +65,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
64
65
}
65
66
}
66
67
68
+ function expandInclude ( tokens ) {
69
+ if ( ! tokens ) {
70
+ return tokens
71
+ }
72
+
73
+ const expandedTokens = [ ]
74
+ tokens . forEach ( e => {
75
+ if ( e . type === INCLUDE_COMPONENT && e . components ) {
76
+ e . components . forEach ( c => expandedTokens . push ( c ) )
77
+ } else {
78
+ expandedTokens . push ( e )
79
+ }
80
+ } )
81
+
82
+ return expandedTokens
83
+ }
84
+
67
85
export function prerenderEmbed ( { compiler, raw = '' , fetch } , done ) {
68
86
let hit = cached [ raw ]
69
87
if ( hit ) {
@@ -98,18 +116,19 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
98
116
}
99
117
} )
100
118
101
- let moveIndex = 0
102
119
walkFetchEmbed ( { compile, embedTokens, fetch } , ( { embedToken, token } ) => {
103
120
if ( token ) {
104
- const index = token . index + moveIndex
105
-
106
121
merge ( links , embedToken . links )
107
122
108
- tokens = tokens
109
- . slice ( 0 , index )
110
- . concat ( embedToken , tokens . slice ( index + 1 ) )
111
- moveIndex += embedToken . length - 1
123
+ tokens = tokens . slice ( 0 , token . index ) . concat (
124
+ {
125
+ type : INCLUDE_COMPONENT ,
126
+ components : embedToken
127
+ } ,
128
+ tokens . slice ( token . index + 1 )
129
+ )
112
130
} else {
131
+ tokens = expandInclude ( tokens )
113
132
cached [ raw ] = tokens . concat ( )
114
133
tokens . links = cached [ raw ] . links = links
115
134
done ( tokens )
0 commit comments