3
3
const { readFileSync, existsSync} = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
5
const { parser} = require ( 'posthtml-parser' ) ;
6
- const { match} = require ( 'posthtml/lib/api' ) ;
6
+ const { match, walk } = require ( 'posthtml/lib/api' ) ;
7
7
const expressions = require ( 'posthtml-expressions' ) ;
8
8
const findPathFromTag = require ( './find-path' ) ;
9
9
const processProps = require ( './process-props' ) ;
10
10
const processAttributes = require ( './process-attributes' ) ;
11
11
const { processPushes, processStacks} = require ( './process-stacks' ) ;
12
12
const { setFilledSlots, processSlotContent, processFillContent} = require ( './process-slots' ) ;
13
+ const log = require ( './log' ) ;
13
14
const each = require ( 'lodash/each' ) ;
14
15
const defaults = require ( 'lodash/defaults' ) ;
15
16
const assignWith = require ( 'lodash/assignWith' ) ;
@@ -24,14 +25,9 @@ const isBoolean = require('lodash/isBoolean');
24
25
const isUndefined = require ( 'lodash/isUndefined' ) ; // value === undefined
25
26
const isNull = require ( 'lodash/isNull' ) ; // value === null
26
27
const isNil = require ( 'lodash/isNil' ) ; // value == null
27
-
28
- // const {inspect} = require('util');
29
- // const debug = true;
30
- // const log = (object, what, method) => {
31
- // if (debug === true || method === debug) {
32
- // console.log(what, inspect(object, false, null, true));
33
- // }
34
- // };
28
+ const uniqueId = require ( 'lodash/uniqueId' ) ;
29
+ const transform = require ( 'lodash/transform' ) ;
30
+ const assign = require ( 'lodash/assign' ) ;
35
31
36
32
/* eslint-disable complexity */
37
33
module . exports = ( options = { } ) => tree => {
@@ -71,7 +67,9 @@ module.exports = (options = {}) => tree => {
71
67
isBoolean,
72
68
isUndefined,
73
69
isNull,
74
- isNil
70
+ isNil,
71
+ uniqueId,
72
+ isEnabled : prop => prop === true || prop === ''
75
73
} ;
76
74
77
75
// Merge customizer callback passed to lodash mergeWith
@@ -120,9 +118,12 @@ module.exports = (options = {}) => tree => {
120
118
} ) ;
121
119
122
120
options . props = { ...options . expressions . locals } ;
121
+ options . aware = { } ;
123
122
124
123
const pushedContent = { } ;
125
124
125
+ log ( 'Start of processing..' , 'init' , 'success' ) ;
126
+
126
127
return processStacks (
127
128
processPushes (
128
129
processTree ( options ) (
@@ -137,21 +138,27 @@ module.exports = (options = {}) => tree => {
137
138
} ;
138
139
/* eslint-enable complexity */
139
140
141
+ // Used for reset aware props
142
+ let processCounter = 0 ;
143
+
140
144
/**
141
145
* @param {Object } options Plugin options
142
146
* @return {Object } PostHTML tree
143
147
*/
144
- let processCounter = 0 ;
145
148
146
149
function processTree ( options ) {
147
150
const filledSlots = { } ;
148
151
149
152
return function ( tree ) {
153
+ log ( `Processing tree number ${ processCounter } ..` , 'processTree' ) ;
154
+
150
155
if ( options . plugins . length > 0 ) {
151
156
tree = applyPluginsToTree ( tree , options . plugins ) ;
152
157
}
153
158
154
159
match . call ( tree , options . matcher , currentNode => {
160
+ log ( `Match found for tag "${ currentNode . tag } "..` , 'processTree' ) ;
161
+
155
162
if ( ! currentNode . attrs ) {
156
163
currentNode . attrs = { } ;
157
164
}
@@ -162,24 +169,21 @@ function processTree(options) {
162
169
return currentNode ;
163
170
}
164
171
165
- console . log ( `${ ++ processCounter } ) Processing "${ currentNode . tag } " component from "${ componentPath } "` ) ;
166
-
167
- // log(currentNode, 'currentNode');
172
+ log ( `${ ++ processCounter } ) Processing "${ currentNode . tag } " from "${ componentPath } "` , 'processTree' ) ;
168
173
169
174
let nextNode = parser ( readFileSync ( componentPath , 'utf8' ) ) ;
170
175
171
176
// Set filled slots
172
177
setFilledSlots ( currentNode , filledSlots , options ) ;
173
178
174
- // Reset options.aware when new nested start
175
- if ( processCounter === 1 ) {
176
- options . aware = { } ;
177
- }
179
+ const aware = transform ( options . aware , ( result , value ) => {
180
+ assign ( result , value ) ;
181
+ } , { } ) ;
178
182
179
183
// Reset options.expressions.locals and keep aware locals
180
- options . expressions . locals = { ...options . props , ...options . aware } ;
184
+ options . expressions . locals = { ...options . props , ...aware } ;
181
185
182
- const { attributes, props} = processProps ( currentNode , nextNode , filledSlots , options , componentPath ) ;
186
+ const { attributes, props} = processProps ( currentNode , nextNode , filledSlots , options , componentPath , processCounter ) ;
183
187
184
188
options . expressions . locals = attributes ;
185
189
options . expressions . locals . $slots = filledSlots ;
@@ -208,14 +212,37 @@ function processTree(options) {
208
212
currentNode . tag = false ;
209
213
currentNode . content = content ;
210
214
211
- processAttributes ( currentNode , attributes , props , options ) ;
215
+ processAttributes ( currentNode , attributes , props , options , aware ) ;
216
+
217
+ // Remove attributes when value is 'null' or 'undefined'
218
+ // so we can conditionally add an attribute by setting value to 'undefined' or 'null'.
219
+ walk . call ( currentNode , node => {
220
+ if ( node && node . attrs ) {
221
+ each ( node . attrs , ( value , key ) => {
222
+ if ( [ 'undefined' , 'null' ] . includes ( value ) ) {
223
+ delete node . attrs [ key ] ;
224
+ }
225
+ } ) ;
226
+ }
227
+
228
+ return node ;
229
+ } ) ;
230
+
231
+ log ( `Done processing number ${ processCounter } .` , 'processTree' , 'success' ) ;
232
+
233
+ // Reset options.aware for current processCounter
234
+ delete options . aware [ processCounter ] ;
212
235
213
- // Reset counter
214
- processCounter = 0 ;
236
+ // Decrement counter
237
+ processCounter -- ;
215
238
216
239
return currentNode ;
217
240
} ) ;
218
241
242
+ if ( processCounter === 0 ) {
243
+ log ( 'End of processing' , 'processTree' , 'success' ) ;
244
+ }
245
+
219
246
return tree ;
220
247
} ;
221
248
}
0 commit comments