File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ /* eslint-disable */
2+ // External URL (Protocol URL)
3+ const TEST_URL = / ^ \w + : \/ \/ / ;
4+
5+ // TODO(michael-ciniawsky)
6+ // add filter method for urls (e.g `options.import`) (#158)
7+ const filter = ( url , options ) => {
8+ return TEST_URL . test ( url ) || url . startsWith ( '//' ) ;
9+ }
10+
11+ export default function ( options = { } ) {
12+ return function ( tree ) {
13+ let idx = 0 ;
14+ const imports = { } ;
15+
16+ tree . match ( [ { tag : 'import' } , { tag : 'include' } ] , ( node ) => {
17+ if ( node . attrs && node . attrs . src ) {
18+ // Remove <import>/<include> tag
19+ node . tag = false ;
20+
21+ // TODO(michael-ciniawky)
22+ // add warning about invalid use of external urls within <import> (#?)
23+
24+ // Ignore external && filtered urls
25+ if ( filter ( node . attrs . src , options ) ) return ;
26+ // Add url to messages.imports
27+ imports [ `HTML__IMPORT__${ idx } ` ] = node . attrs . src ;
28+ // Add content placeholders to HTML
29+ node . content = options . template
30+ ? '${' + `HTML__IMPORT__${ idx } (${ options . template } )` + '}'
31+ : '${' + `HTML__IMPORT__${ idx } ` + '}' ;
32+
33+ idx ++ ;
34+ }
35+
36+ return node ;
37+ } ) ;
38+
39+ // Add imports to result.messages
40+ tree . messages . push ( imports )
41+
42+ return tree ;
43+ } ;
44+ }
You can’t perform that action at this time.
0 commit comments