11import { Plugin , Transformer } from 'unified' ;
22import { Node } from 'unist' ;
33
4- export const createRemoveEmptyParagraphsPlugin = (
5- enabled = false
6- ) : Plugin => {
4+ export const createRemoveEmptyParagraphsPlugin = ( enabled = false ) : Plugin => {
75 return ( ) : Transformer => {
86 if ( ! enabled ) {
97 return ( tree : Node ) => tree ;
@@ -17,7 +15,7 @@ export const createRemoveEmptyParagraphsPlugin = (
1715 } ;
1816} ;
1917
20- const NBSP_REGEX = / \u00a0 / g;
18+ const NBSP_REGEX = / \u00A0 / g;
2119const MEANINGFUL_VOID_ELEMENTS = new Set ( [
2220 'audio' ,
2321 'canvas' ,
@@ -37,14 +35,18 @@ const pruneEmptyParagraphs = (node: any, parent: any) => {
3735 return ;
3836 }
3937
40- if ( node . type === 'element' && node . tagName === 'p' && parent ) {
41- if ( isParagraphEffectivelyEmpty ( node ) && Array . isArray ( parent . children ) ) {
42- const index = parent . children . indexOf ( node ) ;
43-
44- if ( index !== - 1 ) {
45- parent . children . splice ( index , 1 ) ;
46- return ;
47- }
38+ if (
39+ node . type === 'element' &&
40+ node . tagName === 'p' &&
41+ parent &&
42+ isParagraphEffectivelyEmpty ( node ) &&
43+ Array . isArray ( parent . children )
44+ ) {
45+ const index = parent . children . indexOf ( node ) ;
46+
47+ if ( index !== - 1 ) {
48+ parent . children . splice ( index , 1 ) ;
49+ return ;
4850 }
4951 }
5052
@@ -62,7 +64,9 @@ const isParagraphEffectivelyEmpty = (element: any): boolean => {
6264 return true ;
6365 }
6466
65- return element . children . every ( ( child : any ) => isNodeEffectivelyEmpty ( child ) ) ;
67+ return element . children . every ( ( child : any ) =>
68+ isNodeEffectivelyEmpty ( child )
69+ ) ;
6670} ;
6771
6872const isNodeEffectivelyEmpty = ( node : any ) : boolean => {
@@ -98,7 +102,9 @@ const isNodeEffectivelyEmpty = (node: any): boolean => {
98102 return true ;
99103 }
100104
101- return element . children . every ( ( child : any ) => isNodeEffectivelyEmpty ( child ) ) ;
105+ return element . children . every ( ( child : any ) =>
106+ isNodeEffectivelyEmpty ( child )
107+ ) ;
102108 }
103109
104110 return true ;
@@ -109,5 +115,5 @@ const isWhitespace = (value: string): boolean => {
109115 return true ;
110116 }
111117
112- return value . replace ( NBSP_REGEX , ' ' ) . trim ( ) === '' ;
118+ return value . replaceAll ( NBSP_REGEX , ' ' ) . trim ( ) === '' ;
113119} ;
0 commit comments