Skip to content

Commit

Permalink
Merge pull request #462 from thematters/develop
Browse files Browse the repository at this point in the history
Release: v0.2.3-alpha.3
  • Loading branch information
robertu7 authored Mar 12, 2024
2 parents 89e328c + 1ab6675 commit 58dda62
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/matters-editor",
"version": "0.2.3-alpha.2",
"version": "0.2.3-alpha.3",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
67 changes: 57 additions & 10 deletions src/transformers/normalize-sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import rehypeStringify from 'rehype-stringify'
import { unified } from 'unified'
import { describe, expect, test } from 'vitest'

import { normalizeArticleHTML } from './normalize'
import { normalizeArticleHTML, normalizeCommentHTML } from './normalize'
import { rehypeParseOptions, rehypeStringifyOptions } from './options'
import { sanitizeHTML } from './sanitize'
import { sanitizeHTML, type SanitizeHTMLOptions } from './sanitize'

const formatter = unified()
.use(rehypeParse, rehypeParseOptions)
Expand All @@ -21,15 +21,23 @@ const formatHTML = (html: string): string => {
return String(result)
}

const expectProcessArticleHTML = (input: string, output: string) => {
const result = normalizeArticleHTML(sanitizeHTML(input))
const expectProcessArticleHTML = (
input: string,
output: string,
options?: SanitizeHTMLOptions,
) => {
const result = normalizeArticleHTML(sanitizeHTML(input, options))
expect(formatHTML(result).trim()).toBe(output)
}

// const expectProcessCommentHTML = (input: string, output: string) => {
// const result = normalizeCommentHTML(sanitizeHTML(input))
// expect(formatHTML(result).trim()).toBe(output)
// }
const expectProcessCommentHTML = (
input: string,
output: string,
options?: SanitizeHTMLOptions,
) => {
const result = normalizeCommentHTML(sanitizeHTML(input, options))
expect(formatHTML(result).trim()).toBe(output)
}

describe('Sanitize and normalize article', () => {
test('squeeze empty paragraphys', () => {
Expand All @@ -48,7 +56,6 @@ describe('Sanitize and normalize article', () => {
<p></p>
<p><br></p>
<p><br/></p>
<p><br></br></p>
<p><br/><br/><br/></p>
<p></p>
`,
Expand All @@ -65,8 +72,48 @@ describe('Sanitize and normalize article', () => {
<p><br class="smart"></p>
<p><br class="smart"></p>
`,
{ maxEmptyParagraphs: 2 },
)
})
})

// describe('Sanitize and normalize comment', () => {})
describe('Sanitize and normalize comment', () => {
test('skip squeezing empty paragraphys', () => {
expectProcessCommentHTML(
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></p>
<p></p>
<p>abc</p>
<p></p>
<p>abc</p>
<p></p>
<p></p>
<p></p>
<p>abc</p>
<p></p>
<p><br class="smart"></p>
<p><br class="smart"></p>
<p><br class="smart"><br class="smart"><br class="smart"></p>
<p></p>
`,
)
})
})
19 changes: 12 additions & 7 deletions src/transformers/sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('Sanitization: custom', () => {
<p></p>
<p><br></p>
<p><br/></p>
<p><br></br></p>
<p><br/><br/><br/></p>
<p></p>
`,
Expand All @@ -56,10 +55,11 @@ describe('Sanitization: custom', () => {
<p><br></p>
<p><br></p>
`,
{ maxEmptyParagraphs: 2 },
)
})

test('allow max one empty paragraphys', () => {
test('skip squeezing empty paragraphs', () => {
expectSanitizeHTML(
stripIndent`
<p>abc</p>
Expand All @@ -75,20 +75,25 @@ describe('Sanitization: custom', () => {
<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></p>abc
<p><br></p>
<p></p>
<p></p>abc
<p></p>
<p>abc</p>
<p><br></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>
`,
{ maxEmptyParagraphs: 1 },
)
})
})
Expand Down
11 changes: 8 additions & 3 deletions src/transformers/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ export const sanitizeHTML = (
.use(rehypeParse, rehypeParseOptions)
.use(rehypeRaw)
.use(rehypeSanitize, rehypeSanitizeOptions)
.use(rehypeSqueezeParagraphs, { maxCount: maxEmptyParagraphs ?? 2 })
.use(rehypeFormat)
.use(rehypeStringify, rehypeStringifyOptions)

if (maxEmptyParagraphs) {
formatter.use(rehypeSqueezeParagraphs, {
maxCount: maxEmptyParagraphs,
})
}

formatter.use(rehypeFormat).use(rehypeStringify, rehypeStringifyOptions)

const result = formatter.processSync(html)
return String(result)
Expand Down

0 comments on commit 58dda62

Please sign in to comment.