1-
21import { h } from 'hastscript' ;
32import { toString } from 'hast-util-to-string' ;
43/**
5- * Custom Rehype plugin to wrap sections and replace H2 with a <Header> component.
6- * @returns {import('unified').Transformer }
7- */
4+ * Custom Rehype plugin to wrap sections and replace H2 with a <Header> component.
5+ * @returns {import('unified').Transformer }
6+ */
87
98
109export default function rehypeSectionWrapper ( ) {
1110 /**
12- * @param {import('hast').Root } tree
13- */
11+ * @param {import('hast').Root } tree
12+ */
1413 return ( tree ) => {
1514 const newChildren = [ ] ;
1615 let currentWrapper = null ;
@@ -20,44 +19,53 @@ export default function rehypeSectionWrapper() {
2019 newChildren . push ( wrapper ) ;
2120 return wrapper ;
2221 } ;
23-
22+
2423 for ( const node of tree . children ) {
2524 // Check if the node is an H1 element
2625 if ( node . type === 'element' && node . tagName === 'h1' ) {
27- // 1. Extract the text content of the H2 .
26+ // 1. Extract the text content of the H1 .
2827 const headerText = toString ( node ) ;
29-
28+
3029 // 2. Create a new <Header> component element with a 'title' prop.
31- const headerComponent = h ( 'div.outer-wrapper' , [ h ( 'div.middle-wrapper' ,
30+ const headerComponent = h ( 'div.outer-wrapper' , [ h ( 'div.middle-wrapper' ,
3231 [ h ( 'h1.h1-wrapper' , headerText ) , h ( 'div.inter-wrapper' , [ ] ) ] ) ] ) ;
3332
3433 // 3. Add the new <Header> component to the wrapper.
34+ if ( currentWrapper ) {
35+ if ( currentWrapper . children . length === 1 ) {
36+ // If the current wrapper is empty, remove it
37+ newChildren . pop ( ) ;
38+ }
39+ }
3540 newChildren . push ( h ( 'br' , [ ] ) ) ; // Add a horizontal rule before the header
3641 newChildren . push ( headerComponent ) ;
3742 newChildren . push ( h ( 'br' , [ ] ) ) ;
38- currentWrapper = createNewWrapper ( ) ;
43+ currentWrapper = null ;
3944 continue ;
4045 }
4146
4247 if ( node . type === 'element' && node . tagName === 'h2' ) {
4348 // 1. Extract the text content of the H2.
4449 const headerText = toString ( node ) ;
45-
50+
4651 // 2. Create a new <Header> component element with a 'title' prop.
47- const headerComponent = h ( 'div.outer-wrapper' , [ h ( 'div.middle-wrapper' ,
52+ const headerComponent = h ( 'div.outer-wrapper' , [ h ( 'div.middle-wrapper' ,
4853 [ h ( 'div.inter-wrapper' , [ h ( 'h2.h2-wrapper' , headerText ) ] ) ] ) ] )
4954
55+ if ( currentWrapper ) {
56+ if ( currentWrapper . children . length === 1 ) {
57+ // If the current wrapper is empty, remove it
58+ newChildren . pop ( ) ;
59+ }
60+ }
5061 // 3. Add the new <Header> component to the wrapper.
5162 newChildren . push ( h ( 'br' , [ ] ) ) ; // Add a horizontal rule before the header
5263 newChildren . push ( headerComponent ) ;
5364 newChildren . push ( h ( 'br' , [ ] ) ) ;
54- currentWrapper = createNewWrapper ( ) ;
65+ currentWrapper = null ;
5566 continue ;
5667 }
5768 if ( node . type === 'element' && node . tagName === 'hr' ) {
58-
59-
60- // 4. Add the new <Header> component to the wrapper.
6169 newChildren . push ( h ( 'br' , [ ] ) ) ; // Add a horizontal rule before the header
6270 currentWrapper = null ;
6371 continue ;
@@ -66,11 +74,13 @@ export default function rehypeSectionWrapper() {
6674 if ( ! currentWrapper ) {
6775 currentWrapper = createNewWrapper ( ) ;
6876 }
69-
70- // Add any other node to the current section.
77+
7178 currentWrapper . children . push ( node ) ;
79+
80+
7281 }
7382
83+
7484 tree . children = newChildren ;
7585 } ;
7686}
0 commit comments