diff --git a/src/transformers/lib/rehypeSqueezeParagraphs.ts b/src/transformers/lib/rehypeSqueezeParagraphs.ts index 0518896..23d7956 100644 --- a/src/transformers/lib/rehypeSqueezeParagraphs.ts +++ b/src/transformers/lib/rehypeSqueezeParagraphs.ts @@ -8,7 +8,8 @@ import { type Root, type RootContent } from 'hast' * => *



* - * @param {number} maxCount + * @param {number} maxCount: maximum number of empty paragraphs, -1 to retain all + * */ export const rehypeSqueezeParagraphs = ({ maxCount }: { maxCount: number }) => @@ -18,6 +19,7 @@ export const rehypeSqueezeParagraphs = } const children: RootContent[] = [] + const isRetainAll = maxCount < 0 let count = 0 let touched = false @@ -47,9 +49,9 @@ export const rehypeSqueezeParagraphs = return } - // cap empty paragraphs + // cap empty paragraphs or retain all by adding
count++ - if (count <= maxCount) { + if (count <= maxCount || isRetainAll) { children.push({ type: 'element', tagName: 'p', @@ -68,7 +70,7 @@ export const rehypeSqueezeParagraphs = } }) - if (touched) { + if (touched || isRetainAll) { tree.children = children } } diff --git a/src/transformers/normalize-sanitize.test.ts b/src/transformers/normalize-sanitize.test.ts index a95b5da..bf3dcae 100644 --- a/src/transformers/normalize-sanitize.test.ts +++ b/src/transformers/normalize-sanitize.test.ts @@ -40,7 +40,7 @@ const expectProcessCommentHTML = ( } describe('Sanitize and normalize article', () => { - test('squeeze empty paragraphys', () => { + test('squeeze empty paragraphs', () => { expectProcessArticleHTML( stripIndent`

abc

@@ -75,10 +75,50 @@ describe('Sanitize and normalize article', () => { { maxEmptyParagraphs: 2 }, ) }) + + test('squeeze and retain all empty paragraphs', () => { + expectProcessArticleHTML( + stripIndent` +

abc

+

+

+ abc +

+

abc

+

+

+

+

abc

+

+


+


+




+

+ `, + stripIndent` +

abc

+


+


+

abc

+


+

abc

+


+


+


+

abc

+


+


+


+


+


+ `, + { maxEmptyParagraphs: -1 }, + ) + }) }) describe('Sanitize and normalize comment', () => { - test('skip squeezing empty paragraphys', () => { + test('skip squeezing empty paragraphs', () => { expectProcessCommentHTML( stripIndent`

abc

diff --git a/src/transformers/sanitize.test.ts b/src/transformers/sanitize.test.ts index c8a0115..a116ccb 100644 --- a/src/transformers/sanitize.test.ts +++ b/src/transformers/sanitize.test.ts @@ -24,7 +24,7 @@ describe('Sanitization: custom', () => { ) }) - test('allow max two empty paragraphys', () => { + test('allow max two empty paragraphs', () => { expectSanitizeHTML( stripIndent`

abc

@@ -59,6 +59,45 @@ describe('Sanitization: custom', () => { ) }) + test('squeeze and retain all empty paragraphs', () => { + expectSanitizeHTML( + stripIndent` +

abc

+

+

+ abc +

+

abc

+

+

+

+

abc

+

+


+


+




+

+ `, + stripIndent` +

abc

+


+


abc +


+

abc

+


+


+


+

abc

+


+


+


+


+


+ `, + { maxEmptyParagraphs: -1 }, + ) + }) + test('skip squeezing empty paragraphs', () => { expectSanitizeHTML( stripIndent`