19 lines
1.6 KiB
TypeScript
19 lines
1.6 KiB
TypeScript
import React from 'react';
|
|
import {test} from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {renderToStaticMarkup} from 'react-dom/server';
|
|
import {NoteMarkdown} from '../src/components/NoteMarkdown';
|
|
const render=(text:string)=>renderToStaticMarkup(<NoteMarkdown text={text}/>);
|
|
test('Markdown renders nested bullets, numbered and task lists as semantic lists',()=>{
|
|
const html=render('- 第一项\n - 子项目\n- 第二项\n\n3. 第三步\n4. 第四步\n\n- [x] 完成\n- [ ] 待办');
|
|
assert.match(html,/<ul>/);assert.match(html,/<li>第一项\n<ul>/);assert.match(html,/<ol start="3">/);assert.match(html,/type="checkbox"/);assert.match(html,/checked/);assert.ok(!html.includes('- 第一项'));
|
|
});
|
|
test('Markdown keeps fenced code, tables, strong text and subtle links',()=>{
|
|
const html=render('## 标题\n\n**重点** 和 [相关笔记](/explore/abc?agent_mode=True)\n\n| 名称 | 值 |\n| --- | --- |\n| 条目 | 1 |\n\n```ts\nconst items = [1, 2];\n```');
|
|
assert.match(html,/<h2>标题/);assert.match(html,/<strong>重点/);assert.match(html,/<table>/);assert.match(html,/<pre><code class="language-ts">/);assert.match(html,/note-inline-link/);assert.match(html,/href="\/explore\/abc"/);
|
|
});
|
|
test('Untrusted Markdown cannot execute HTML or javascript links and images stay links',()=>{
|
|
const html=render('<script>alert(1)</script>\n\n[unsafe](javascript:alert)\n\n');
|
|
assert.ok(!html.includes('<script>'));assert.ok(!html.includes('href="javascript:'));assert.ok(!html.includes('<img'));assert.match(html,/href="\/api\/media\/example"/);
|
|
});
|