Skip to content

Commit 251fd74

Browse files
committed
fixup! feat(text-editor): add removeEmptyParagraphs prop
1 parent 7712683 commit 251fd74

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/components/markdown/remove-empty-paragraphs-plugin.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ describe('remove empty paragraphs plugin', () => {
1414
createParagraph(),
1515
createParagraph([createText(' ')]),
1616
createParagraph([createText('\n')]),
17-
createParagraph([createText('\u00a0')]),
17+
createParagraph([createText('\u00A0')]),
1818
createParagraph([createElement('span')]),
19-
createParagraph([
20-
createElement('span', [createText('\u00a0')]),
21-
]),
19+
createParagraph([createElement('span', [createText('\u00A0')])]),
2220
]);
2321

2422
runPlugin(tree, true);

src/components/markdown/remove-empty-paragraphs-plugin.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Plugin, Transformer } from 'unified';
22
import { 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;
2119
const 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

6872
const 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

Comments
 (0)