37 lines
2.6 KiB
TypeScript
37 lines
2.6 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import React from 'react'
|
|
import { renderToStaticMarkup } from 'react-dom/server'
|
|
import { ReactMarkdown } from '../src/renderer/src/workbuddy/markdown/ReactMarkdown'
|
|
import { ReactMarkdownProvider, type TableActionContext } from '../src/renderer/src/workbuddy/markdown/context'
|
|
import { createMarkdownParser } from '../src/renderer/src/workbuddy/markdown/parser'
|
|
|
|
const source='| 路径 | 持久化状态 | 内容 |\n| --- | --- | --- |\n| 取消 | `cancelled` ✓ | 0 字符 |\n| 正常 | `done` ✓ | 「收到」 |'
|
|
function render(text: string, finished = true, action?: (context: TableActionContext) => React.ReactNode): string {
|
|
return renderToStaticMarkup(<ReactMarkdownProvider value={{ mdInstance: createMarkdownParser({ loading: true }), renderTableAction: action }}>
|
|
<ReactMarkdown isFinished={finished} sourceId="source-message">{text}</ReactMarkdown>
|
|
</ReactMarkdownProvider>)
|
|
}
|
|
const html=render(source)
|
|
assert.equal((html.match(/class="cr-table-block /g)??[]).length,1)
|
|
assert.match(html,/cr-table-block__scroller/)
|
|
assert.match(html,/cr-table-block__inner/)
|
|
assert.equal((html.match(/cr-inline-code markdown-content__inline-code/g)??[]).length,2)
|
|
assert.match(html,/<\/code>(?:<span>)? ✓/,'code and checkmark retain the original separating space')
|
|
assert.equal((html.match(/<table /g)??[]).length,1,'table root recursion does not wrap itself again')
|
|
const context: TableActionContext[]=[]
|
|
const action=(value: TableActionContext): React.ReactNode=>{context.push(value);return <button disabled={value.disabled}>quote</button>}
|
|
const quoted=render(source,false,action)
|
|
assert.match(quoted,/<button disabled="">quote<\/button>/)
|
|
assert.equal(context[0].markdown,source)
|
|
assert.equal(context[0].sourceId,'source-message')
|
|
assert.equal(context[0].disabled,true)
|
|
context.length=0;render(source,true,action);assert.equal(context[0].disabled,false)
|
|
const mixed=render('| link | path |\n| --- | ---: |\n| [safe](https://example.invalid) | `src/demo.ts` |')
|
|
assert.match(mixed,/target="_blank" rel="noreferrer"/)
|
|
assert.match(mixed,/text-align:right/)
|
|
assert.match(mixed,/cr-inline-code markdown-content__inline-code/)
|
|
assert.match(render('> '+source.replaceAll('\n','\n> ')),/cr-blockquote[\s\S]*cr-table-block__inner/)
|
|
assert.match(render('```ts\nconst x=1\n```'),/cr-code-like-box|cr-code-block/,'fenced blocks retain the dedicated code renderer')
|
|
assert.match(render(source+'\n\nFollowing paragraph',false),/Following/)
|
|
console.log('table-renderer-regression: source shell, recursive inline code/spacing, links, alignment, quoted tables, streaming quote contract and fence isolation passed')
|