This is a paragraph.
'); + expect(result.markdown).toContain('This is a paragraph.'); + }); + + it('converts strong and b tags', () => { + const result = htmlToMarkdown('bold and also bold'); + expect(result.markdown).toContain('**bold**'); + expect(result.markdown).toContain('**also bold**'); + }); + + it('converts em and i tags', () => { + const result = htmlToMarkdown('italic and also italic'); + expect(result.markdown).toContain('*italic*'); + expect(result.markdown).toContain('*also italic*'); + }); + + it('converts del and s tags', () => { + const result = htmlToMarkdown('const x = 1;');
+ expect(result.markdown).toContain('`const x = 1;`');
+ });
+
+ it('converts pre code block', () => {
+ const result = htmlToMarkdown('line1\nline2');
+ expect(result.markdown).toContain('```');
+ expect(result.markdown).toContain('line1');
+ expect(result.markdown).toContain('line2');
+ });
+
+ it('converts pre code block with language', () => {
+ const result = htmlToMarkdown(
+ 'const x = 1;',
+ );
+ expect(result.markdown).toContain('```javascript');
+ expect(result.markdown).toContain('const x = 1;');
+ });
+
+ it('converts anchor links', () => {
+ const result = htmlToMarkdown('Link text');
+ expect(result.markdown).toContain('[Link text](https://example.com)');
+ });
+
+ it('converts anchor links with title', () => {
+ const result = htmlToMarkdown('Link');
+ expect(result.markdown).toContain('[Link](https://example.com "Title")');
+ });
+
+ it('converts images', () => {
+ const result = htmlToMarkdown('
');
+ expect(result.markdown).toContain('');
+ });
+
+ it('converts images with title', () => {
+ const result = htmlToMarkdown('
');
+ expect(result.markdown).toContain('');
+ });
+
+ it('converts unordered list', () => {
+ const result = htmlToMarkdown(''); + expect(result.markdown).toContain('> Quote text'); + }); + + it('converts horizontal rule', () => { + const result = htmlToMarkdown('Quote text
| Name | Type |
|---|---|
| John | User |
text
'); + expect(result.markdown).not.toContain('alert'); + expect(result.markdown).not.toContain('.x{}'); + expect(result.markdown).toContain('text'); + }); + + it('handles nested elements', () => { + const result = htmlToMarkdown('bold and italic
'); + expect(result.markdown).toContain('**bold**'); + expect(result.markdown).toContain('*italic*'); + }); + + it('returns hasError false for valid HTML', () => { + const result = htmlToMarkdown('Valid
'); + expect(result.hasError).toBe(false); + expect(result.error).toBeUndefined(); + }); + + it('returns correct length stats', () => { + const html = 'Hello
'; + const result = htmlToMarkdown(html); + expect(result.originalLength).toBe(html.length); + expect(result.markdownLength).toBe(result.markdown.length); + }); + + it('handles full HTML document', () => { + const result = htmlToMarkdown(SAMPLE_HTML); + expect(result.hasError).toBe(false); + expect(result.markdown).toContain('# 欢迎使用 HTML 转 Markdown'); + expect(result.markdown).toContain('**HTML**'); + expect(result.markdown).toContain('*Markdown*'); + expect(result.markdown).toContain('```javascript'); + expect(result.markdown).toContain('| 名称 | 类型 |'); + expect(result.markdown).toContain('> 这是一段引用文本'); + }); + + it('handles plain text without tags', () => { + const result = htmlToMarkdown('Just plain text'); + expect(result.markdown).toContain('Just plain text'); + expect(result.hasError).toBe(false); + }); + + it('handles task list items', () => { + const result = htmlToMarkdown( + 'Content
');
+ expect(result.html).toContain(' {
+ const result = markdownToHtml('use `npm install` command');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain('npm install');
+ });
+
+ it('应该转换引用块', () => {
+ const result = markdownToHtml('> This is a quote');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain('');
+ expect(result.html).toContain('This is a quote');
+ });
+
+ it('应该转换表格', () => {
+ const md = '| A | B |\n|---|---|\n| 1 | 2 |';
+ const result = markdownToHtml(md);
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain('');
+ expect(result.html).toContain('A ');
+ expect(result.html).toContain('1 ');
+ });
+
+ it('应该转换任务列表', () => {
+ const result = markdownToHtml('- [x] done\n- [ ] todo');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain(' {
+ const result = markdownToHtml('~~deleted~~');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain('deleted');
+ });
+
+ it('应该处理空字符串', () => {
+ const result = markdownToHtml('');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toBe('');
+ expect(result.originalLength).toBe(0);
+ expect(result.htmlLength).toBe(0);
+ });
+
+ it('应该处理空白字符串', () => {
+ const result = markdownToHtml(' \n ');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toBe('');
+ });
+
+ it('应该处理中文内容', () => {
+ const result = markdownToHtml('# 你好世界\n\n这是**中文**内容。');
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain('你好世界');
+ expect(result.html).toContain('中文');
+ });
+
+ it('应该转换示例 Markdown', () => {
+ const result = markdownToHtml(SAMPLE_MARKDOWN);
+ expect(result.hasError).toBe(false);
+ expect(result.html).toContain('');
+ expect(result.html).toContain('');
+ expect(result.originalLength).toBe(SAMPLE_MARKDOWN.length);
+ expect(result.htmlLength).toBeGreaterThan(result.originalLength);
+ });
+});
+
+describe('wrapHtmlDocument', () => {
+ it('应该生成完整的 HTML 文档', () => {
+ const doc = wrapHtmlDocument('Hello
', 'Test Title');
+ expect(doc).toContain('');
+ expect(doc).toContain('');
+ expect(doc).toContain('Test Title ');
+ expect(doc).toContain('');
+ expect(doc).toContain('Hello
');
+ expect(doc).toContain('');
+ });
+
+ it('应该转义标题中的特殊字符', () => {
+ const doc = wrapHtmlDocument('test
', 'Title