@@ -324,14 +324,113 @@ function groupUtilityToSass(
324324 return ''
325325}
326326
327+ let peerModifierList : IGroupModifierPair [ ] = [ ]
328+
329+ /**
330+ * Convert peer-* modifiers to sub-selectors
331+ *
332+ * @param nodeTree IHtmlNode[]
333+ * @param deepth number
334+ * @param isChildNodes boolean
335+ *
336+ * @returns string
337+ */
338+ function peerUtilityToSass (
339+ nodeTree : IHtmlNode [ ] ,
340+ peerClass : string ,
341+ isChildNodes = false
342+ ) : string {
343+ if ( ! isChildNodes ) {
344+ peerModifierList = [ ]
345+ }
346+
347+ let groupSass = ''
348+
349+ const groupPattern = / p e e r - ( [ a - z 0 - 9 ] + ) : ( [ a - z 0 - 9 -:\/ ] + ) / gm
350+
351+ nodeTree . forEach ( ( node : IHtmlNode ) => {
352+ if ( node . filterAttributes ) {
353+ console . log ( node . filterAttributes . class ?. match ( groupPattern ) )
354+ if (
355+ node . filterAttributes . class &&
356+ node . filterAttributes . class ?. match ( groupPattern )
357+ ) {
358+ node . filterAttributes . class ?. match ( groupPattern ) ?. forEach ( ( item ) => {
359+ const matches = new RegExp ( groupPattern ) . exec ( item )
360+
361+ const groupModifierPair = < IGroupModifierPair > {
362+ modifier : matches ?. [ 1 ] ,
363+ utility : matches ?. [ 2 ] ,
364+ className : getClassName ( node , node . order ) ,
365+ }
366+
367+ console . log ( groupModifierPair )
368+
369+ peerModifierList . push ( groupModifierPair )
370+ } )
371+
372+ if ( node . filterAttributes . class . match ( / ( p e e r ) (? ! - ) / gm) ) {
373+ return groupSass
374+ } else if ( node . children . length ) {
375+ peerUtilityToSass ( node . children , peerClass , true )
376+ }
377+ } else {
378+ peerUtilityToSass ( node . children , peerClass , true )
379+ }
380+ }
381+ } )
382+
383+ if ( ! isChildNodes ) {
384+ if ( peerModifierList . length > 0 ) {
385+ const modifierGroups = peerModifierList . reduce ( ( prev , next ) => {
386+ prev [ next . modifier ] = prev [ next . modifier ] || [ ]
387+ prev [ next . modifier ] . push ( next )
388+
389+ return prev
390+ } , Object . create ( null ) )
391+
392+ Object . entries ( modifierGroups ) ?. forEach ( ( [ modifier , utilityList ] ) => {
393+ const _utilityList = < IGroupModifierPair [ ] > utilityList
394+
395+ const classGroups = _utilityList . reduce ( ( prev , next ) => {
396+ prev [ next . className ] = prev [ next . className ] || [ ]
397+ prev [ next . className ] . push ( next )
398+
399+ return prev
400+ } , Object . create ( null ) )
401+
402+ groupSass += `\n\n/* #region Peer modifier: ${ modifier } */\n\n`
403+
404+ Object . entries ( classGroups ) ?. forEach ( ( [ className , utilityList ] ) => {
405+ const _utilityList = < IGroupModifierPair [ ] > utilityList
406+
407+ const classList = _utilityList
408+ . map ( ( x : IGroupModifierPair ) => x . utility )
409+ . join ( ' ' )
410+
411+ groupSass += `${ peerClass } :${ modifier } ~ ${ className } {\n`
412+ groupSass += `\t\t@apply ${ classList } ;\n`
413+ groupSass += `\t}\n`
414+ } )
415+
416+ groupSass += `\n/* #endregion */\n\n`
417+ } )
418+ }
419+
420+ return groupSass
421+ }
422+
423+ return ''
424+ }
425+
327426/**
328427 * Extract SASS tree from HTML JSON tree
329428 *
330429 * @param {IHtmlNode } nodeTree
331430 *
332431 * @returns string
333432 */
334- function getSassTree ( nodeTree : IHtmlNode [ ] ) {
433+ function getSassTree ( nodeTree : IHtmlNode [ ] ) : string {
335434 return nodeTree
336435 . map ( ( node : IHtmlNode ) => {
337436 let treeString = '' ,
@@ -374,7 +473,7 @@ function getSassTree(nodeTree: IHtmlNode[]) {
374473 let groupUtilityTree = ''
375474
376475 // convert group utilities
377- if ( node . filterAttributes ?. class ?. match ( / ( g r o u p ) (? ! - ) / gm) ) {
476+ if ( node . filterAttributes ?. class ?. match ( / ( g r o u p ) (? ! - ) / gm) ) {
378477 groupUtilityTree = groupUtilityToSass ( node . children )
379478
380479 if ( groupUtilityTree !== '' ) {
@@ -391,7 +490,19 @@ function getSassTree(nodeTree: IHtmlNode[]) {
391490 }
392491 }
393492
394- return `${ classComment }
493+ let peerUtilityTree = ''
494+
495+ // convert peer utilities
496+ if ( node . filterAttributes ?. class ?. match ( / ( p e e r ) (? ! - ) / gm) ) {
497+ peerUtilityTree = peerUtilityToSass ( nodeTree , className )
498+
499+ if ( peerUtilityTree !== '' ) {
500+ // clear parent group class name
501+ treeString = treeString . replace ( / ( p e e r ) (? ! - ) / gm, ' ' )
502+ }
503+ }
504+
505+ return `${ classComment } ${ peerUtilityTree }
395506 ${ className } {
396507 ${ treeString } ${ subTreeString }
397508 }`
@@ -527,6 +638,11 @@ export function convertToSass(
527638 htmlTreeResult = ''
528639
529640 if ( sassTreeResult ) {
641+ sassTreeResult = sassTreeResult . replace (
642+ / p e e r - ( [ a - z 0 - 9 ] + ) : ( [ a - z 0 - 9 -:\/ ] + ) / gm,
643+ ''
644+ )
645+
530646 htmlTreeResult = getHtmlTree ( filteredHtmlData )
531647
532648 const cssTreeResult = getCssTree ( styles )
0 commit comments