mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 05:07:06 +08:00
fix(editor): handle html content copied from google docs (#12383)
Closes: [BS-3508](https://linear.app/affine-design/issue/BS-3508/google-docs复制内容到affine时自动加粗问题) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved detection of bold, italic, underline, and strike-through formatting in imported HTML, supporting both tags and inline CSS styles. - Enhanced handling of inline elements containing block-level children to ensure correct formatting and structure during HTML import. - Introduced a plugin that converts inline elements with block-level children into block elements, preserving original tag information. - **Bug Fixes** - Resolved issues where block-level elements nested inside inline tags could cause incorrect formatting or structure. - **Tests** - Added comprehensive test coverage for HTML formatting conversions and plugin behavior to ensure accuracy and reliability. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
import rehypeParse from 'rehype-parse';
|
||||
import rehypeStringify from 'rehype-stringify';
|
||||
import { unified } from 'unified';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { rehypeInlineToBlock } from '../../../../adapters/html/rehype-plugins/inline-to-block';
|
||||
|
||||
describe('rehypeInlineToBlock', () => {
|
||||
const process = (html: string) => {
|
||||
return unified()
|
||||
.use(rehypeParse, { fragment: true })
|
||||
.use(rehypeInlineToBlock)
|
||||
.use(rehypeStringify)
|
||||
.processSync(html)
|
||||
.toString();
|
||||
};
|
||||
|
||||
it('should not transform inline elements without block children', () => {
|
||||
const input = '<b>Hello World</b>';
|
||||
const output = process(input);
|
||||
expect(output).toBe('<b>Hello World</b>');
|
||||
});
|
||||
|
||||
it('should transform inline elements containing block children', () => {
|
||||
const input = '<b><p>Hello World</p></b>';
|
||||
const output = process(input);
|
||||
expect(output).toBe('<div data-original-tag="b"><p>Hello World</p></div>');
|
||||
});
|
||||
|
||||
it('should preserve existing attributes when transforming', () => {
|
||||
const input = '<b class="test" id="demo"><p>Hello World</p></b>';
|
||||
const output = process(input);
|
||||
expect(output).toBe(
|
||||
'<div class="test" id="demo" data-original-tag="b"><p>Hello World</p></div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle multiple block elements within inline element', () => {
|
||||
const input = '<b><p>First</p><div>Second</div><h1>Third</h1></b>';
|
||||
const output = process(input);
|
||||
expect(output).toBe(
|
||||
'<div data-original-tag="b"><p>First</p><div>Second</div><h1>Third</h1></div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle mixed content (text and block elements)', () => {
|
||||
const input = '<b>Text before<p>Block element</p>Text after</b>';
|
||||
const output = process(input);
|
||||
expect(output).toBe(
|
||||
'<div data-original-tag="b">Text before<p>Block element</p>Text after</div>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle complex nested structures', () => {
|
||||
const input = '<b><div><p>Nested <b>inline</b> content</p></div></b>';
|
||||
const output = process(input);
|
||||
expect(output).toBe(
|
||||
'<div data-original-tag="b"><div><p>Nested <b>inline</b> content</p></div></div>'
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user