@@ -126,22 +126,22 @@ class Parser {
126
126
return _ . clone ( this . missingIncludeSrc ) ;
127
127
}
128
128
129
- _renderIncludeFile ( filePath , element , context , config , asIfAt = filePath ) {
129
+ _renderIncludeFile ( filePath , node , context , config , asIfAt = filePath ) {
130
130
try {
131
131
this . _fileCache [ filePath ] = this . _fileCache [ filePath ]
132
132
? this . _fileCache [ filePath ] : fs . readFileSync ( filePath , 'utf8' ) ;
133
133
} catch ( e ) {
134
134
// Read file fail
135
- const missingReferenceErrorMessage = `Missing reference in: ${ element . attribs [ ATTRIB_CWF ] } ` ;
135
+ const missingReferenceErrorMessage = `Missing reference in: ${ node . attribs [ ATTRIB_CWF ] } ` ;
136
136
e . message += `\n${ missingReferenceErrorMessage } ` ;
137
137
this . _onError ( e ) ;
138
- return utils . createErrorNode ( element , e ) ;
138
+ return utils . createErrorNode ( node , e ) ;
139
139
}
140
140
const fileContent = this . _fileCache [ filePath ] ; // cache the file contents to save some I/O
141
141
const { parent, relative } = urlUtils . calculateNewBaseUrls ( asIfAt , config . rootPath , config . baseUrlMap ) ;
142
142
const userDefinedVariables = config . userDefinedVariablesMap [ path . resolve ( parent , relative ) ] ;
143
143
// Extract included variables from the PARENT file
144
- const includeVariables = Parser . extractIncludeVariables ( element , context . variables ) ;
144
+ const includeVariables = Parser . extractIncludeVariables ( node , context . variables ) ;
145
145
// Extract page variables from the CHILD file
146
146
const pageVariables
147
147
= this . extractPageVariables ( asIfAt , fileContent , userDefinedVariables , includeVariables ) ;
@@ -162,22 +162,22 @@ class Parser {
162
162
} ) ;
163
163
const aliases = new Map ( ) ;
164
164
Parser . FILE_ALIASES . set ( cwf , aliases ) ;
165
- $ ( 'import[from]' ) . each ( ( index , element ) => {
166
- const filePath = path . resolve ( path . dirname ( cwf ) , element . attribs . from ) ;
167
- const variableNames = Object . keys ( element . attribs )
165
+ $ ( 'import[from]' ) . each ( ( index , node ) => {
166
+ const filePath = path . resolve ( path . dirname ( cwf ) , node . attribs . from ) ;
167
+ const variableNames = Object . keys ( node . attribs )
168
168
. filter ( name => name !== 'from' && name !== 'as' ) ;
169
169
// If no namespace is provided, we use the smallest name as one
170
170
const largestName = variableNames . sort ( ) [ 0 ] ;
171
171
// ... and prepend it with $__MARKBIND__ to reduce collisions.
172
172
const generatedAlias = IMPORTED_VARIABLE_PREFIX + largestName ;
173
- const alias = _ . hasIn ( element . attribs , 'as' )
174
- ? element . attribs . as
173
+ const alias = _ . hasIn ( node . attribs , 'as' )
174
+ ? node . attribs . as
175
175
: generatedAlias ;
176
176
aliases . set ( alias , filePath ) ;
177
177
this . staticIncludeSrc . push ( { from : context . cwf , to : filePath } ) ;
178
178
// Render inner file content
179
179
const { content : renderedContent , childContext, userDefinedVariables }
180
- = this . _renderIncludeFile ( filePath , element , context , config ) ;
180
+ = this . _renderIncludeFile ( filePath , node , context , config ) ;
181
181
this . extractInnerVariablesIfNotProcessed ( renderedContent , childContext , config , filePath ) ;
182
182
const innerVariables = this . getImportedVariableMap ( filePath ) ;
183
183
Parser . VARIABLE_LOOKUP . get ( filePath ) . forEach ( ( value , variableName , map ) => {
@@ -194,17 +194,23 @@ class Parser {
194
194
}
195
195
196
196
processDynamicResources ( context , html ) {
197
- const self = this ;
198
197
const $ = cheerio . load ( html , {
199
198
xmlMode : false ,
200
199
decodeEntities : false ,
201
200
} ) ;
201
+
202
+ const { rootPath } = this ;
203
+ function getAbsoluteResourcePath ( elem , relativeResourcePath ) {
204
+ const firstParent = elem . closest ( 'div[data-included-from], span[data-included-from]' ) ;
205
+ const originalSrc = utils . ensurePosix ( firstParent . attr ( 'data-included-from' ) || context ) ;
206
+ const originalSrcFolder = path . posix . dirname ( originalSrc ) ;
207
+ const fullResourcePath = path . posix . join ( originalSrcFolder , relativeResourcePath ) ;
208
+ const resolvedResourcePath = path . posix . relative ( utils . ensurePosix ( rootPath ) , fullResourcePath ) ;
209
+ return path . posix . join ( '{{hostBaseUrl}}' , resolvedResourcePath ) ;
210
+ }
211
+
202
212
$ ( 'img, pic, thumbnail' ) . each ( function ( ) {
203
213
const elem = $ ( this ) ;
204
- if ( elem [ 0 ] . name === 'thumbnail' && elem . attr ( 'src' ) === undefined ) {
205
- // Thumbnail tag without src
206
- return ;
207
- }
208
214
const resourcePath = utils . ensurePosix ( elem . attr ( 'src' ) ) ;
209
215
if ( resourcePath === undefined || resourcePath === '' ) {
210
216
// Found empty img/pic resource in resourcePath
@@ -214,12 +220,7 @@ class Parser {
214
220
// Do not rewrite.
215
221
return ;
216
222
}
217
- const firstParent = elem . closest ( 'div[data-included-from], span[data-included-from]' ) ;
218
- const originalSrc = utils . ensurePosix ( firstParent . attr ( 'data-included-from' ) || context ) ;
219
- const originalSrcFolder = path . posix . dirname ( originalSrc ) ;
220
- const fullResourcePath = path . posix . join ( originalSrcFolder , resourcePath ) ;
221
- const resolvedResourcePath = path . posix . relative ( utils . ensurePosix ( self . rootPath ) , fullResourcePath ) ;
222
- const absoluteResourcePath = path . posix . join ( '{{hostBaseUrl}}' , resolvedResourcePath ) ;
223
+ const absoluteResourcePath = getAbsoluteResourcePath ( elem , resourcePath ) ;
223
224
$ ( this ) . attr ( 'src' , absoluteResourcePath ) ;
224
225
} ) ;
225
226
$ ( 'a, link' ) . each ( function ( ) {
@@ -233,12 +234,7 @@ class Parser {
233
234
// Do not rewrite.
234
235
return ;
235
236
}
236
- const firstParent = elem . closest ( 'div[data-included-from], span[data-included-from]' ) ;
237
- const originalSrc = utils . ensurePosix ( firstParent . attr ( 'data-included-from' ) || context ) ;
238
- const originalSrcFolder = path . posix . dirname ( originalSrc ) ;
239
- const fullResourcePath = path . posix . join ( originalSrcFolder , resourcePath ) ;
240
- const resolvedResourcePath = path . posix . relative ( utils . ensurePosix ( self . rootPath ) , fullResourcePath ) ;
241
- const absoluteResourcePath = path . posix . join ( '{{hostBaseUrl}}' , resolvedResourcePath ) ;
237
+ const absoluteResourcePath = getAbsoluteResourcePath ( elem , resourcePath ) ;
242
238
$ ( this ) . attr ( 'href' , absoluteResourcePath ) ;
243
239
} ) ;
244
240
return $ . html ( ) ;
@@ -256,20 +252,18 @@ class Parser {
256
252
}
257
253
258
254
_parse ( node , context , config ) {
259
- const element = node ;
260
- const self = this ;
261
- if ( _ . isArray ( element ) ) {
262
- return element . map ( el => self . _parse ( el , context , config ) ) ;
255
+ if ( _ . isArray ( node ) ) {
256
+ return node . map ( el => this . _parse ( el , context , config ) ) ;
263
257
}
264
- if ( Parser . isText ( element ) ) {
265
- return element ;
258
+ if ( Parser . isText ( node ) ) {
259
+ return node ;
266
260
}
267
- if ( element . name ) {
268
- element . name = element . name . toLowerCase ( ) ;
261
+ if ( node . name ) {
262
+ node . name = node . name . toLowerCase ( ) ;
269
263
}
270
264
271
- if ( ( / ^ h [ 1 - 6 ] $ / ) . test ( element . name ) && ! element . attribs . id ) {
272
- const textContent = utils . getTextContent ( element ) ;
265
+ if ( ( / ^ h [ 1 - 6 ] $ / ) . test ( node . name ) && ! node . attribs . id ) {
266
+ const textContent = utils . getTextContent ( node ) ;
273
267
const slugifiedHeading = slugify ( textContent , { decamelize : false } ) ;
274
268
275
269
let headerId = slugifiedHeading ;
@@ -281,59 +275,58 @@ class Parser {
281
275
headerIdMap [ slugifiedHeading ] = 2 ;
282
276
}
283
277
284
- element . attribs . id = headerId ;
278
+ node . attribs . id = headerId ;
285
279
}
286
280
287
- switch ( element . name ) {
281
+ switch ( node . name ) {
288
282
case 'md' :
289
- element . name = 'span' ;
283
+ node . name = 'span' ;
290
284
cheerio . prototype . options . xmlMode = false ;
291
- element . children = cheerio . parseHTML ( md . renderInline ( cheerio . html ( element . children ) ) , true ) ;
285
+ node . children = cheerio . parseHTML ( md . renderInline ( cheerio . html ( node . children ) ) , true ) ;
292
286
cheerio . prototype . options . xmlMode = true ;
293
287
break ;
294
288
case 'markdown' :
295
- element . name = 'div' ;
289
+ node . name = 'div' ;
296
290
cheerio . prototype . options . xmlMode = false ;
297
- element . children = cheerio . parseHTML ( md . render ( cheerio . html ( element . children ) ) , true ) ;
291
+ node . children = cheerio . parseHTML ( md . render ( cheerio . html ( node . children ) ) , true ) ;
298
292
cheerio . prototype . options . xmlMode = true ;
299
293
break ;
300
294
case 'panel' : {
301
- if ( ! _ . hasIn ( element . attribs , 'src' ) ) { // dynamic panel
295
+ if ( ! _ . hasIn ( node . attribs , 'src' ) ) { // dynamic panel
302
296
break ;
303
297
}
304
- const fileExists = utils . fileExists ( element . attribs . src )
298
+ const fileExists = utils . fileExists ( node . attribs . src )
305
299
|| utils . fileExists (
306
300
urlUtils . calculateBoilerplateFilePath (
307
- element . attribs . boilerplate ,
308
- element . attribs . src , config ) ) ;
301
+ node . attribs . boilerplate ,
302
+ node . attribs . src , config ) ) ;
309
303
if ( fileExists ) {
310
- const { src, fragment } = element . attribs ;
304
+ const { src, fragment } = node . attribs ;
311
305
const resultDir = path . dirname ( path . join ( '{{hostBaseUrl}}' , path . relative ( config . rootPath , src ) ) ) ;
312
306
const resultPath = path . join ( resultDir , utils . setExtension ( path . basename ( src ) , '._include_.html' ) ) ;
313
- element . attribs . src = utils . ensurePosix ( fragment ? `${ resultPath } #${ fragment } ` : resultPath ) ;
307
+ node . attribs . src = utils . ensurePosix ( fragment ? `${ resultPath } #${ fragment } ` : resultPath ) ;
314
308
}
315
- delete element . attribs . boilerplate ;
309
+ delete node . attribs . boilerplate ;
316
310
break ;
317
311
}
318
312
default :
319
313
break ;
320
314
}
321
315
322
- componentParser . parseComponents ( element , this . _onError ) ;
316
+ componentParser . parseComponents ( node , this . _onError ) ;
323
317
324
- if ( element . children ) {
325
- element . children . forEach ( ( child ) => {
326
- self . _parse ( child , context , config ) ;
318
+ if ( node . children ) {
319
+ node . children . forEach ( ( child ) => {
320
+ this . _parse ( child , context , config ) ;
327
321
} ) ;
328
322
}
329
323
330
- componentParser . postParseComponents ( element , this . _onError ) ;
324
+ componentParser . postParseComponents ( node , this . _onError ) ;
331
325
332
- return element ;
326
+ return node ;
333
327
}
334
328
335
329
_trimNodes ( node ) {
336
- const self = this ;
337
330
if ( node . name === 'pre' || node . name === 'code' ) {
338
331
return ;
339
332
}
@@ -346,7 +339,7 @@ class Parser {
346
339
node . children . splice ( n , 1 ) ;
347
340
n -- ;
348
341
} else if ( child . type === 'tag' ) {
349
- self . _trimNodes ( child ) ;
342
+ this . _trimNodes ( child ) ;
350
343
}
351
344
}
352
345
/* eslint-enable no-plusplus */
@@ -589,21 +582,20 @@ class Parser {
589
582
}
590
583
591
584
_rebaseReference ( node , foundBase ) {
592
- const element = node ;
593
- if ( _ . isArray ( element ) ) {
594
- return element . map ( el => this . _rebaseReference ( el , foundBase ) ) ;
585
+ if ( _ . isArray ( node ) ) {
586
+ return node . map ( el => this . _rebaseReference ( el , foundBase ) ) ;
595
587
}
596
- if ( Parser . isText ( element ) ) {
597
- return element ;
588
+ if ( Parser . isText ( node ) ) {
589
+ return node ;
598
590
}
599
591
// Rebase children element
600
592
const childrenBase = { } ;
601
- element . children . forEach ( ( el ) => {
593
+ node . children . forEach ( ( el ) => {
602
594
this . _rebaseReference ( el , childrenBase ) ;
603
595
} ) ;
604
596
// rebase current element
605
- if ( element . attribs [ ATTRIB_INCLUDE_PATH ] ) {
606
- const filePath = element . attribs [ ATTRIB_INCLUDE_PATH ] ;
597
+ if ( node . attribs [ ATTRIB_INCLUDE_PATH ] ) {
598
+ const filePath = node . attribs [ ATTRIB_INCLUDE_PATH ] ;
607
599
let newBaseUrl = urlUtils . calculateNewBaseUrls ( filePath , this . rootPath , this . baseUrlMap ) ;
608
600
if ( newBaseUrl ) {
609
601
const { relative, parent } = newBaseUrl ;
@@ -616,27 +608,27 @@ class Parser {
616
608
if ( bases . length !== 0 ) {
617
609
// need to rebase
618
610
newBaseUrl = combinedBases [ bases [ 0 ] ] ;
619
- if ( element . children ) {
611
+ if ( node . children ) {
620
612
// ATTRIB_CWF is where the element was preprocessed
621
- const currentBase = urlUtils . calculateNewBaseUrls ( element . attribs [ ATTRIB_CWF ] ,
613
+ const currentBase = urlUtils . calculateNewBaseUrls ( node . attribs [ ATTRIB_CWF ] ,
622
614
this . rootPath , this . baseUrlMap ) ;
623
615
if ( currentBase && currentBase . relative !== newBaseUrl ) {
624
616
cheerio . prototype . options . xmlMode = false ;
625
- const rendered = nunjucks . renderString ( cheerio . html ( element . children ) , {
617
+ const rendered = nunjucks . renderString ( cheerio . html ( node . children ) , {
626
618
// This is to prevent the nunjuck call from converting {{hostBaseUrl}} to an empty string
627
619
// and let the hostBaseUrl value be injected later.
628
620
hostBaseUrl : '{{hostBaseUrl}}' ,
629
621
baseUrl : `{{hostBaseUrl}}/${ newBaseUrl } ` ,
630
622
} , { path : filePath } ) ;
631
- element . children = cheerio . parseHTML ( rendered , true ) ;
623
+ node . children = cheerio . parseHTML ( rendered , true ) ;
632
624
cheerio . prototype . options . xmlMode = true ;
633
625
}
634
626
}
635
627
}
636
- delete element . attribs [ ATTRIB_INCLUDE_PATH ] ;
628
+ delete node . attribs [ ATTRIB_INCLUDE_PATH ] ;
637
629
}
638
- delete element . attribs [ ATTRIB_CWF ] ;
639
- return element ;
630
+ delete node . attribs [ ATTRIB_CWF ] ;
631
+ return node ;
640
632
}
641
633
642
634
static resetVariables ( ) {
@@ -645,8 +637,8 @@ class Parser {
645
637
Parser . PROCESSED_INNER_VARIABLES . clear ( ) ;
646
638
}
647
639
648
- static isText ( element ) {
649
- return element . type === 'text' || element . type === 'comment' ;
640
+ static isText ( node ) {
641
+ return node . type === 'text' || node . type === 'comment' ;
650
642
}
651
643
652
644
/**
0 commit comments