/** * parser.js 单元测试 * 覆盖 Markdown 解析器的所有语法分支与安全特性 */ import { parseMarkdown, safeUrl, slugify } from '../src/parser.js'; describe('parseMarkdown - 基础', () => { test('空输入返回空字符串', () => { expect(parseMarkdown('')).toBe(''); expect(parseMarkdown(null)).toBe(''); expect(parseMarkdown(undefined)).toBe(''); }); test('非字符串输入被转换为字符串', () => { expect(parseMarkdown(123)).toBe('
123
'); }); test('Windows 换行符被规范化', () => { const html = parseMarkdown('line1\r\nline2'); expect(html).toContain('line1'); expect(html).toContain('line2'); expect(html).not.toContain('\r'); }); }); describe('parseMarkdown - 标题', () => { test('h1 到 h6', () => { expect(parseMarkdown('# T1')).toBe('hello
'); }); test('多行段落合并', () => { const html = parseMarkdown('line1\nline2'); expect(html).toBe('line1
line2
p1
\np2
'); }); }); describe('parseMarkdown - 行内格式', () => { test('粗体 **text**', () => { expect(parseMarkdown('**bold**')).toBe('bold
'); }); test('粗体 __text__', () => { expect(parseMarkdown('__bold__')).toBe('bold
'); }); test('斜体 *text*', () => { expect(parseMarkdown('a *italic* b')).toBe('a italic b
'); }); test('斜体 _text_', () => { expect(parseMarkdown('a _italic_ b')).toBe('a italic b
'); }); test('删除线 ~~text~~', () => { expect(parseMarkdown('~~del~~')).toBe('del
code');
});
test('行内代码内容不被解析为格式', () => {
const html = parseMarkdown('`a **b** c`');
expect(html).toContain('a **b** c');
expect(html).not.toContain('');
});
test('行内代码内容被转义', () => {
const html = parseMarkdown('`');
expect(html).toContain('<script>');
expect(html).not.toContain('