2
2
3
3
const fs = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
+ const { inspect} = require ( 'util' ) ;
6
+ const { sha256} = require ( 'js-sha256' ) ;
5
7
const expressions = require ( 'posthtml-expressions' ) ;
6
8
const scriptDataLocals = require ( 'posthtml-expressions/lib/locals' ) ;
7
9
const { parser : parseToPostHtml } = require ( 'posthtml-parser' ) ;
@@ -11,8 +13,13 @@ const {match} = require('posthtml/lib/api');
11
13
const merge = require ( 'deepmerge' ) ;
12
14
const findPathFromTagName = require ( './find-path' ) ;
13
15
14
- // Used for slot without name
15
- const defaultSlotName = '__default-slot' ;
16
+ const debug = true ;
17
+
18
+ const log = ( object , what ) => {
19
+ if ( debug ) {
20
+ console . log ( what , inspect ( object , false , null , true ) ) ;
21
+ }
22
+ } ;
16
23
17
24
const defaultSlotType = 'replace' ;
18
25
@@ -55,7 +62,8 @@ function processNodes(tree, options, messages) {
55
62
56
63
options . expressions . locals = { ...options . locals , ...options . aware } ;
57
64
58
- const slotsLocals = parseSlotsLocals ( options . slotTagName , html , node . content ) ;
65
+ const defaultSlotName = sha256 ( filePath ) ;
66
+ const slotsLocals = parseSlotsLocals ( options . slotTagName , html , node . content , defaultSlotName ) ;
59
67
const { attributes, locals} = parseLocals ( options , slotsLocals , node , html ) ;
60
68
61
69
options . expressions . locals = attributes ;
@@ -66,7 +74,7 @@ function processNodes(tree, options, messages) {
66
74
const layoutTree = processNodes ( applyPluginsToTree ( html , plugins ) , options , messages ) ;
67
75
68
76
node . tag = false ;
69
- node . content = mergeSlots ( layoutTree , node , options . strict , options ) ;
77
+ node . content = mergeSlots ( layoutTree , node , options . strict , options , defaultSlotName ) ;
70
78
71
79
const index = node . content . findIndex ( content => typeof content === 'object' ) ;
72
80
@@ -199,13 +207,18 @@ function parseLocals(options, slotsLocals, {attrs}, html) {
199
207
* @param {String } tag
200
208
* @param html
201
209
* @param {Object } content
210
+ * @param {String } defaultSlotName
202
211
* @return {Object }
203
212
*/
204
- function parseSlotsLocals ( tag , html , content = [ ] ) {
213
+ function parseSlotsLocals ( tag , html , content , defaultSlotName ) {
205
214
const slots = { } ;
206
215
207
216
const getNodeName = node => {
208
- let name = node . attrs && node . attrs . name ;
217
+ if ( ! node . attrs ) {
218
+ node . attrs = { } ;
219
+ }
220
+
221
+ let { name} = node . attrs ;
209
222
210
223
if ( ! name ) {
211
224
name = Object . keys ( { ...node . attrs } ) . find ( name => ! Object . keys ( slotTypes ) . includes ( name ) && name !== 'type' && name !== defaultSlotType ) ;
@@ -260,11 +273,12 @@ function parseSlotsLocals(tag, html, content = []) {
260
273
* @param {Boolean } strict
261
274
* @param {String } slotTagName
262
275
* @param {Boolean|String } fallbackSlotTagName
276
+ * @param defaultSlotName
263
277
* @return {Object } tree
264
278
*/
265
- function mergeSlots ( tree , node , strict , { slotTagName, fallbackSlotTagName} ) {
266
- const slots = getSlots ( slotTagName , tree , fallbackSlotTagName ) ; // Slot in component.html
267
- const fillSlots = getSlots ( slotTagName , node . content ) ; // Slot in page.html
279
+ function mergeSlots ( tree , node , strict , { slotTagName, fallbackSlotTagName} , defaultSlotName ) {
280
+ const slots = getSlots ( slotTagName , tree , defaultSlotName , fallbackSlotTagName ) ; // Slot in component.html
281
+ const fillSlots = getSlots ( slotTagName , node . content , defaultSlotName ) ; // Slot in page.html
268
282
const clean = content => content . replace ( / ( \n | \t ) / g, '' ) . trim ( ) ;
269
283
270
284
// Retrieve main content, means everything that is not inside slots
@@ -273,6 +287,18 @@ function mergeSlots(tree, node, strict, {slotTagName, fallbackSlotTagName}) {
273
287
if ( contentOutsideSlots . filter ( c => typeof c !== 'string' || clean ( c ) !== '' ) . length > 0 ) {
274
288
fillSlots [ defaultSlotName ] = [ { tag : slotTagName , attrs : { name : defaultSlotName } , content : [ ...contentOutsideSlots ] } ] ;
275
289
}
290
+
291
+ // Replace <content> with <block>
292
+ // required only when using extend + module syntax
293
+ if ( fallbackSlotTagName && slots [ defaultSlotName ] ) {
294
+ slots [ defaultSlotName ] . map ( slot => {
295
+ if ( slot . tag === ( fallbackSlotTagName === true ? 'content' : fallbackSlotTagName ) ) {
296
+ slot . tag = slotTagName ;
297
+ }
298
+
299
+ return slot ;
300
+ } ) ;
301
+ }
276
302
}
277
303
278
304
for ( const slotName of Object . keys ( slots ) ) {
@@ -351,10 +377,11 @@ function mergeContent(slotContent, defaultContent, slotType) {
351
377
* Get all slots from content
352
378
* @param {String } tag
353
379
* @param {Object } content
380
+ * @param defaultSlotName
354
381
* @param {Boolean|String } fallbackSlotTagName
355
382
* @return {Object }
356
383
*/
357
- function getSlots ( tag , content = [ ] , fallbackSlotTagName = false ) {
384
+ function getSlots ( tag , content , defaultSlotName , fallbackSlotTagName = false ) {
358
385
const slots = { } ;
359
386
360
387
// For compatibility with module slot name <content>
@@ -486,6 +513,10 @@ module.exports = (options = {}) => {
486
513
options . locals = { ...options . expressions . locals } ;
487
514
options . aware = { } ;
488
515
516
+ // options.locals.$isUndefined = value => value === void 0
517
+
518
+ log ( debug , 'Debug enabled?' ) ;
519
+
489
520
return function ( tree ) {
490
521
tree = processNodes ( tree , options , tree . messages ) ;
491
522
0 commit comments