Skip to content

Commit

Permalink
feat: allow to retain all empty paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Apr 16, 2024
1 parent 1ab6675 commit 119a7ef
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/transformers/lib/rehypeSqueezeParagraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { type Root, type RootContent } from 'hast'
* =>
* <p><br></p><p><br></p>
*
* @param {number} maxCount
* @param {number} maxCount: maximum number of empty paragraphs, -1 to retain all
*
*/
export const rehypeSqueezeParagraphs =
({ maxCount }: { maxCount: number }) =>
Expand All @@ -18,6 +19,7 @@ export const rehypeSqueezeParagraphs =
}

const children: RootContent[] = []
const isRetainAll = maxCount < 0
let count = 0
let touched = false

Expand Down Expand Up @@ -47,9 +49,9 @@ export const rehypeSqueezeParagraphs =
return
}

// cap empty paragraphs
// cap empty paragraphs or retain all by adding <br>
count++
if (count <= maxCount) {
if (count <= maxCount || isRetainAll) {
children.push({
type: 'element',
tagName: 'p',
Expand All @@ -68,7 +70,7 @@ export const rehypeSqueezeParagraphs =
}
})

if (touched) {
if (touched || isRetainAll) {
tree.children = children
}
}
44 changes: 42 additions & 2 deletions src/transformers/normalize-sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const expectProcessCommentHTML = (
}

describe('Sanitize and normalize article', () => {
test('squeeze empty paragraphys', () => {
test('squeeze empty paragraphs', () => {
expectProcessArticleHTML(
stripIndent`
<p>abc</p>
Expand Down Expand Up @@ -75,10 +75,50 @@ describe('Sanitize and normalize article', () => {
{ maxEmptyParagraphs: 2 },
)
})

test('squeeze and retain all empty paragraphs', () => {
expectProcessArticleHTML(
stripIndent`
<p>abc</p>
<p></p>
<p></p>
abc
<p></p>
<p>abc</p>
<p></p>
<p></p>
<p></p>
<p>abc</p>
<p></p>
<p><br></p>
<p><br/></p>
<p><br/><br/><br/></p>
<p></p>
`,
stripIndent`
<p>abc</p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p>abc</p>
<p><br class="smart"></p>
<p>abc</p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p>abc</p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p><br class="smart"></p>
`,
{ maxEmptyParagraphs: -1 },
)
})
})

describe('Sanitize and normalize comment', () => {
test('skip squeezing empty paragraphys', () => {
test('skip squeezing empty paragraphs', () => {
expectProcessCommentHTML(
stripIndent`
<p>abc</p>
Expand Down
41 changes: 40 additions & 1 deletion src/transformers/sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Sanitization: custom', () => {
)
})

test('allow max two empty paragraphys', () => {
test('allow max two empty paragraphs', () => {
expectSanitizeHTML(
stripIndent`
<p>abc</p>
Expand Down Expand Up @@ -59,6 +59,45 @@ describe('Sanitization: custom', () => {
)
})

test('squeeze and retain all empty paragraphs', () => {
expectSanitizeHTML(
stripIndent`
<p>abc</p>
<p></p>
<p></p>
abc
<p></p>
<p>abc</p>
<p></p>
<p></p>
<p></p>
<p>abc</p>
<p></p>
<p><br></p>
<p><br/></p>
<p><br/><br/><br/></p>
<p></p>
`,
stripIndent`
<p>abc</p>
<p><br></p>
<p><br></p>abc
<p><br></p>
<p>abc</p>
<p><br></p>
<p><br></p>
<p><br></p>
<p>abc</p>
<p><br></p>
<p><br></p>
<p><br></p>
<p><br></p>
<p><br></p>
`,
{ maxEmptyParagraphs: -1 },
)
})

test('skip squeezing empty paragraphs', () => {
expectSanitizeHTML(
stripIndent`
Expand Down

0 comments on commit 119a7ef

Please sign in to comment.