1
1
import { get } from '../fetch/ajax'
2
2
import { merge } from '../util/core'
3
3
4
+ const INCLUDE_COMPONENT = '__include__'
4
5
const cached = { }
5
6
6
7
function walkFetchEmbed ( { embedTokens, compile, fetch} , cb ) {
7
8
let token
8
9
let step = 0
9
- let count = 1
10
+ let count = 0
10
11
11
12
if ( ! embedTokens . length ) {
12
13
return cb ( { } )
@@ -43,7 +44,7 @@ function walkFetchEmbed({embedTokens, compile, fetch}, cb) {
43
44
}
44
45
}
45
46
cb ( { token, embedToken} )
46
- if ( ++ count >= step ) {
47
+ if ( ++ count >= embedTokens . length ) {
47
48
cb ( { } )
48
49
}
49
50
}
@@ -61,6 +62,23 @@ function walkFetchEmbed({embedTokens, compile, fetch}, cb) {
61
62
}
62
63
}
63
64
65
+ function expandInclude ( tokens ) {
66
+ if ( ! tokens ) {
67
+ return tokens
68
+ }
69
+
70
+ const expandedTokens = [ ]
71
+ tokens . forEach ( e => {
72
+ if ( e . type === INCLUDE_COMPONENT && e . components ) {
73
+ e . components . forEach ( c => expandedTokens . push ( c ) )
74
+ } else {
75
+ expandedTokens . push ( e )
76
+ }
77
+ } )
78
+
79
+ return expandedTokens
80
+ }
81
+
64
82
export function prerenderEmbed ( { compiler, raw = '' , fetch} , done ) {
65
83
let hit = cached [ raw ]
66
84
if ( hit ) {
@@ -95,18 +113,19 @@ export function prerenderEmbed({compiler, raw = '', fetch}, done) {
95
113
}
96
114
} )
97
115
98
- let moveIndex = 0
99
116
walkFetchEmbed ( { compile, embedTokens, fetch} , ( { embedToken, token} ) => {
100
117
if ( token ) {
101
- const index = token . index + moveIndex
102
-
103
118
merge ( links , embedToken . links )
104
119
105
- tokens = tokens
106
- . slice ( 0 , index )
107
- . concat ( embedToken , tokens . slice ( index + 1 ) )
108
- moveIndex += embedToken . length - 1
120
+ tokens = tokens . slice ( 0 , token . index ) . concat (
121
+ {
122
+ type : INCLUDE_COMPONENT ,
123
+ components : embedToken
124
+ } ,
125
+ tokens . slice ( token . index + 1 )
126
+ )
109
127
} else {
128
+ tokens = expandInclude ( tokens )
110
129
cached [ raw ] = tokens . concat ( )
111
130
tokens . links = cached [ raw ] . links = links
112
131
done ( tokens )
0 commit comments