Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to retain all empty paragraphs #463

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/transformers/lib/rehypeSqueezeParagraphs.ts
Original file line number Diff line number Diff line change
@@ -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 }) =>
@@ -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 <br>
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
}
}
44 changes: 42 additions & 2 deletions src/transformers/normalize-sanitize.test.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ const expectProcessCommentHTML = (
}

describe('Sanitize and normalize article', () => {
test('squeeze empty paragraphys', () => {
test('squeeze empty paragraphs', () => {
expectProcessArticleHTML(
stripIndent`
<p>abc</p>
@@ -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>
41 changes: 40 additions & 1 deletion src/transformers/sanitize.test.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ describe('Sanitization: custom', () => {
)
})

test('allow max two empty paragraphys', () => {
test('allow max two empty paragraphs', () => {
expectSanitizeHTML(
stripIndent`
<p>abc</p>
@@ -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`