diff --git a/package.json b/package.json index 1aef457..fa08524 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/transformers/normalize-sanitize.test.ts b/src/transformers/normalize-sanitize.test.ts index 6774330..a95b5da 100644 --- a/src/transformers/normalize-sanitize.test.ts +++ b/src/transformers/normalize-sanitize.test.ts @@ -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) @@ -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', () => { @@ -48,7 +56,6 @@ describe('Sanitize and normalize article', () => {



-






`, @@ -65,8 +72,48 @@ describe('Sanitize and normalize article', () => {



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

abc

+

+

+ abc +

+

abc

+

+

+

+

abc

+

+


+


+




+

+ `, + stripIndent` +

abc

+

+

+

abc

+

+

abc

+

+

+

+

abc

+

+


+


+




+

+ `, + ) + }) +}) diff --git a/src/transformers/sanitize.test.ts b/src/transformers/sanitize.test.ts index 05c2bd3..c8a0115 100644 --- a/src/transformers/sanitize.test.ts +++ b/src/transformers/sanitize.test.ts @@ -40,7 +40,6 @@ describe('Sanitization: custom', () => {



-






`, @@ -56,10 +55,11 @@ describe('Sanitization: custom', () => {



`, + { maxEmptyParagraphs: 2 }, ) }) - test('allow max one empty paragraphys', () => { + test('skip squeezing empty paragraphs', () => { expectSanitizeHTML( stripIndent`

abc

@@ -75,20 +75,25 @@ describe('Sanitization: custom', () => {



-






`, stripIndent`

abc

-


abc -


+

+

abc +

abc

-


+

+

+

abc

+


+


+




+

`, - { maxEmptyParagraphs: 1 }, ) }) }) diff --git a/src/transformers/sanitize.ts b/src/transformers/sanitize.ts index 0c7a1d4..debb5d3 100644 --- a/src/transformers/sanitize.ts +++ b/src/transformers/sanitize.ts @@ -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)