mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 08:50:08 +08:00
* fix: parse inline math in note tables and statistics notation BlockNote only renders `inlineMath` nodes, and two common shapes never became one: - `parseNoteMarkdown` converted inline `$...$` in a block's top-level content, but table cells live under `tableContent.rows`, which the walk never reached. - The normaliser treated `$Var(N)$` and `$Cov(X, Y)$` as prose because the Latin-word heuristic did not recognise statistics function names, so it escaped the delimiters as stray dollars. Recurse into `tableContent` cells and add the common statistics function names to the math-word allowlist, with regression tests for both. Co-Authored-By: Claude Code <noreply@anthropic.com> * fix(web): count statistics names as maths only when they are called "mean", "median", "rank", "span" and "var" are English words and a keyword too, so with them in the word list "$52k mean, $48k median" became a formula and a backticked `var x = 1` on a page with maths turned into KaTeX. Keep them out of MATH_WORDS and accept them only before "(", which is how Var(X) and Cov(X, Y) are written. --------- Co-authored-by: Claude Code <noreply@anthropic.com> Co-authored-by: zszz3 <91608029+zszz3@users.noreply.github.com>
104 lines
3.6 KiB
TypeScript
104 lines
3.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { parseNoteMarkdown } from '@/features/notes/model/markdown'
|
|
import { resultLearningBlock } from '@/features/notes/model/results'
|
|
import type { NoteBlock } from '@/lib/notes-api'
|
|
const parse = (text: string): NoteBlock[] => [
|
|
{
|
|
id: crypto.randomUUID(),
|
|
type: 'paragraph',
|
|
props: {},
|
|
content: [{ type: 'text', text, styles: {} }],
|
|
},
|
|
]
|
|
describe('notebook migration and generated content', () => {
|
|
it('preserves editor.md markers, math and old diagram fences', () => {
|
|
const blocks = parseNoteMarkdown(
|
|
'【重点】《保留》 $x^2$\n\n$$\ny = x^2\n$$\n\n```flow\nst=>start: Start\n```',
|
|
parse
|
|
)
|
|
expect(JSON.stringify(blocks)).toContain('【重点】《保留》')
|
|
expect(JSON.stringify(blocks)).toContain('inlineMath')
|
|
expect(blocks.find(b => b.type === 'math')?.props.formula).toBe('y = x^2')
|
|
expect(blocks.find(b => b.type === 'learning')?.props.markdown).toContain('st=>start: Start')
|
|
})
|
|
it('keeps unfinished math input as ordinary text', () => {
|
|
expect(parseNoteMarkdown('$$\nunclosed', parse)[0].type).toBe('paragraph')
|
|
})
|
|
it('converts inline math inside table cells', () => {
|
|
const tableParse = (): NoteBlock[] => [
|
|
{
|
|
id: 'table',
|
|
type: 'table',
|
|
props: {},
|
|
content: {
|
|
type: 'tableContent',
|
|
rows: [
|
|
{
|
|
cells: [
|
|
{
|
|
type: 'tableCell',
|
|
content: [
|
|
{ type: 'text', text: '$P(A\\mid B)=\\dfrac{P(AB)}{P(B)}$', styles: {} },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
]
|
|
const blocks = parseNoteMarkdown('table', tableParse)
|
|
const table = blocks[0].content as {
|
|
rows: Array<{ cells: Array<{ content: Array<{ type: string; props?: { formula?: string } }> }> }>
|
|
}
|
|
const cell = table.rows[0].cells[0].content
|
|
expect(cell).toHaveLength(1)
|
|
expect(cell[0].type).toBe('inlineMath')
|
|
expect(cell[0].props?.formula).toBe('P(A\\mid B)=\\dfrac{P(AB)}{P(B)}')
|
|
})
|
|
it('persists question payloads without the chat quiz provider', () => {
|
|
const result = resultLearningBlock({
|
|
summary: { results: [{ qa_pair: { question: 'Frequency?', correct_answer: 'Hz' } }] },
|
|
})
|
|
expect(result?.props.kind).toBe('quiz')
|
|
expect(JSON.parse(String(result?.props.data)).questions[0].question).toBe('Frequency?')
|
|
})
|
|
it('preserves visualization envelopes and media URLs', () => {
|
|
const payload = {
|
|
renderer: { id: 'svg' },
|
|
payload: { data: '<svg/>' },
|
|
response: 'An illustration',
|
|
}
|
|
expect(JSON.parse(String(resultLearningBlock(payload)?.props.data))).toEqual(payload)
|
|
expect(resultLearningBlock({ response: 'Just text' })).toBeUndefined()
|
|
expect(
|
|
resultLearningBlock({
|
|
workspace_items: [{ url: '/api/workspaces/a/files/b', mime_type: 'audio/mpeg' }],
|
|
})?.props.kind
|
|
).toBe('media')
|
|
})
|
|
})
|
|
|
|
it('previews text blocks with their formatting and table cells intact', async () => {
|
|
const { blockPreview } = await import('@/features/notes/model/blocks')
|
|
expect(
|
|
blockPreview({
|
|
id: 'h',
|
|
type: 'heading',
|
|
props: { level: 2 },
|
|
content: [{ type: 'text', text: 'Idea', styles: { bold: true } }],
|
|
})
|
|
).toBe('## **Idea**')
|
|
expect(
|
|
blockPreview({ id: 'todo', type: 'checkListItem', props: { checked: true }, content: 'Done' })
|
|
).toBe('- [x] Done')
|
|
expect(
|
|
blockPreview({
|
|
id: 'table',
|
|
type: 'table',
|
|
props: {},
|
|
content: { rows: [{ cells: [[{ type: 'text', text: 'A|B' }]] }] },
|
|
})
|
|
).toContain('| A\\|B |\n| --- |')
|
|
})
|