-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add <br> inside empty paragraph in
rehypeSqueezeParagraphs
- Loading branch information
Showing
7 changed files
with
206 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './rehypeSqueezeParagraphs' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { type Root, type RootContent } from 'hast' | ||
|
||
/** | ||
* Squeeze empty paragraphs to a maximum of N | ||
* | ||
* e.g. | ||
* <p></p><p></p><p></p><p></p><p></p><p></p> | ||
* => | ||
* <p></p><p></p> | ||
* | ||
* @param {number} maxCount | ||
*/ | ||
export const rehypeSqueezeParagraphs = | ||
({ maxCount }: { maxCount: number }) => | ||
(tree: Root) => { | ||
if (tree.type !== 'root') { | ||
return | ||
} | ||
|
||
const children: RootContent[] = [] | ||
let count = 0 | ||
let touched = false | ||
|
||
tree.children.forEach((node) => { | ||
// skip empty text nodes | ||
if (node.type === 'text' && node.value.replace(/\s/g, '') === '') { | ||
children.push(node) | ||
return | ||
} | ||
|
||
// skip non-paragraph nodes | ||
if (node.type !== 'element' || node.tagName !== 'p') { | ||
count = 0 | ||
children.push(node) | ||
return | ||
} | ||
|
||
// skip non-empty paragraphs: | ||
// - <p></p> | ||
// - <p><br/></p> | ||
const isEmptyParagraph = | ||
node.children.length === 0 || | ||
node.children.every((n) => n.type === 'element' && n.tagName === 'br') | ||
if (!isEmptyParagraph) { | ||
count = 0 | ||
children.push(node) | ||
return | ||
} | ||
|
||
// cap empty paragraphs | ||
count++ | ||
if (count <= maxCount) { | ||
children.push({ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [ | ||
{ | ||
type: 'element', | ||
tagName: 'br', | ||
properties: {}, | ||
children: [], | ||
}, | ||
], | ||
}) | ||
} else { | ||
touched = true | ||
} | ||
}) | ||
|
||
if (touched) { | ||
tree.children = children | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { stripIndent } from 'common-tags' | ||
import rehypeFormat from 'rehype-format' | ||
import rehypeParse from 'rehype-parse' | ||
import rehypeRaw from 'rehype-raw' | ||
import rehypeStringify from 'rehype-stringify' | ||
import { unified } from 'unified' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
import { normalizeArticleHTML } from './normalize' | ||
import { rehypeParseOptions, rehypeStringifyOptions } from './options' | ||
import { sanitizeHTML } from './sanitize' | ||
|
||
const formatter = unified() | ||
.use(rehypeParse, rehypeParseOptions) | ||
.use(rehypeRaw) | ||
.use(rehypeFormat) | ||
.use(rehypeStringify, rehypeStringifyOptions) | ||
|
||
const formatHTML = (html: string): string => { | ||
const result = formatter.processSync(html) | ||
return String(result) | ||
} | ||
|
||
const expectProcessArticleHTML = (input: string, output: string) => { | ||
const result = normalizeArticleHTML(sanitizeHTML(input)) | ||
expect(formatHTML(result).trim()).toBe(output) | ||
} | ||
|
||
// const expectProcessCommentHTML = (input: string, output: string) => { | ||
// const result = normalizeCommentHTML(sanitizeHTML(input)) | ||
// expect(formatHTML(result).trim()).toBe(output) | ||
// } | ||
|
||
describe('Sanitize and normalize article', () => { | ||
test('squeeze empty paragraphys', () => { | ||
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></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>abc</p> | ||
<p><br class="smart"></p> | ||
<p><br class="smart"></p> | ||
`, | ||
) | ||
}) | ||
}) | ||
|
||
// describe('Sanitize and normalize comment', () => {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,33 @@ | ||
import { type Root, type RootContent } from 'hast' | ||
import rehypeFormat from 'rehype-format' | ||
import rehypeParse from 'rehype-parse' | ||
import rehypeRaw from 'rehype-raw' | ||
import rehypeSanitize from 'rehype-sanitize' | ||
import rehypeStringify from 'rehype-stringify' | ||
import { unified } from 'unified' | ||
|
||
import { rehypeSqueezeParagraphs } from './lib' | ||
import { | ||
rehypeParseOptions, | ||
rehypeSanitizeOptions, | ||
rehypeStringifyOptions, | ||
} from './options' | ||
|
||
/** | ||
* Squeeze empty paragraphs to a maximum of N | ||
* | ||
* e.g. | ||
* <p></p><p></p><p></p><p></p><p></p><p></p> | ||
* => | ||
* <p></p><p></p> | ||
* | ||
* @param {number} maxCount | ||
*/ | ||
const rehypeSqueezeParagraphs = | ||
({ maxCount }: { maxCount: number }) => | ||
(tree: Root) => { | ||
if (tree.type !== 'root') { | ||
return | ||
} | ||
|
||
const children: RootContent[] = [] | ||
let count = 0 | ||
let touched = false | ||
|
||
tree.children.forEach((node) => { | ||
// skip empty text nodes | ||
if (node.type === 'text' && node.value.replace(/\s/g, '') === '') { | ||
children.push(node) | ||
return | ||
} | ||
|
||
// skip non-paragraph nodes | ||
if (node.type !== 'element' || node.tagName !== 'p') { | ||
count = 0 | ||
children.push(node) | ||
return | ||
} | ||
|
||
// skip non-empty paragraphs: | ||
// - <p></p> | ||
// - <p><br/></p> | ||
const isEmptyParagraph = | ||
node.children.length === 0 || | ||
node.children.every((n) => n.type === 'element' && n.tagName === 'br') | ||
if (!isEmptyParagraph) { | ||
count = 0 | ||
children.push(node) | ||
return | ||
} | ||
|
||
// cap empty paragraphs | ||
count++ | ||
if (count <= maxCount) { | ||
children.push({ | ||
type: 'element', | ||
tagName: 'p', | ||
properties: {}, | ||
children: [], | ||
}) | ||
} else { | ||
touched = true | ||
} | ||
}) | ||
|
||
if (touched) { | ||
tree.children = children | ||
} | ||
} | ||
export interface SanitizeHTMLOptions { | ||
maxEmptyParagraphs?: number | ||
} | ||
|
||
const formatter = unified() | ||
.use(rehypeParse, rehypeParseOptions) | ||
.use(rehypeRaw) | ||
.use(rehypeSanitize, rehypeSanitizeOptions) | ||
.use(rehypeSqueezeParagraphs, { maxCount: 2 }) | ||
.use(rehypeFormat) | ||
.use(rehypeStringify, rehypeStringifyOptions) | ||
export const sanitizeHTML = ( | ||
html: string, | ||
{ maxEmptyParagraphs }: SanitizeHTMLOptions = {}, | ||
): string => { | ||
const formatter = unified() | ||
.use(rehypeParse, rehypeParseOptions) | ||
.use(rehypeRaw) | ||
.use(rehypeSanitize, rehypeSanitizeOptions) | ||
.use(rehypeSqueezeParagraphs, { maxCount: maxEmptyParagraphs ?? 2 }) | ||
.use(rehypeFormat) | ||
.use(rehypeStringify, rehypeStringifyOptions) | ||
|
||
export const sanitizeHTML = (html: string): string => { | ||
const result = formatter.processSync(html) | ||
return String(result) | ||
} |