check:prettier was not part of my gate routine, so formatting drift accumulated across the session's touched files (and a few older ones). Run prettier --write to bring the repo back to 'All matched files use Prettier code style!'. Formatting only — no logic changes. tsc/tests/build all green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { Descendant } from 'slate';
|
|
import { toMatrixCustomHTML } from './output';
|
|
import { BlockType } from './types';
|
|
|
|
// Loose Slate node builders for the test.
|
|
const txt = (text: string, marks: Record<string, unknown> = {}) =>
|
|
({ text, ...marks }) as unknown as Descendant;
|
|
const el = (type: BlockType, children: unknown[]) => ({ type, children }) as unknown as Descendant;
|
|
|
|
const OPTS = {
|
|
allowTextFormatting: true,
|
|
allowInlineMarkdown: false,
|
|
allowBlockMarkdown: false,
|
|
allowMath: true,
|
|
};
|
|
|
|
test('inline $…$ → data-mx-maths span, surrounding prose preserved', () => {
|
|
assert.equal(
|
|
toMatrixCustomHTML(txt('a $x^2$ b'), OPTS),
|
|
'a <span data-mx-maths="x^2"><code>x^2</code></span> b',
|
|
);
|
|
});
|
|
|
|
test('single-line block $$…$$ → data-mx-maths div', () => {
|
|
assert.equal(
|
|
toMatrixCustomHTML(txt('$$E=mc^2$$'), OPTS),
|
|
'<div data-mx-maths="E=mc^2"><code>E=mc^2</code></div>',
|
|
);
|
|
});
|
|
|
|
test('LaTeX special chars are escaped in attribute + fallback', () => {
|
|
assert.equal(
|
|
toMatrixCustomHTML(txt('$a<b$'), OPTS),
|
|
'<span data-mx-maths="a<b"><code>a<b</code></span>',
|
|
);
|
|
});
|
|
|
|
test('math bypasses markdown — underscores are not italicised', () => {
|
|
// Without pre-markdown extraction, `_` would become <em>.
|
|
assert.equal(
|
|
toMatrixCustomHTML(txt('$a_b$'), { ...OPTS, allowInlineMarkdown: true }),
|
|
'<span data-mx-maths="a_b"><code>a_b</code></span>',
|
|
);
|
|
});
|
|
|
|
test('currency ($5 and $10) stays literal', () => {
|
|
assert.equal(toMatrixCustomHTML(txt('$5 and $10'), OPTS), '$5 and $10');
|
|
});
|
|
|
|
test('no math conversion inside an inline code mark', () => {
|
|
const out = toMatrixCustomHTML(txt('$x$', { code: true }), OPTS);
|
|
assert.ok(!out.includes('data-mx-maths'));
|
|
assert.ok(out.includes('<code>$x$</code>'));
|
|
});
|
|
|
|
test('no math conversion inside a code block', () => {
|
|
const block = el(BlockType.CodeBlock, [el(BlockType.CodeLine, [txt('$x$')])]);
|
|
const out = toMatrixCustomHTML(block, OPTS);
|
|
assert.ok(!out.includes('data-mx-maths'));
|
|
assert.ok(out.includes('$x$'));
|
|
});
|
|
|
|
test('a message with no math is unchanged', () => {
|
|
assert.equal(toMatrixCustomHTML(txt('just hello'), OPTS), 'just hello');
|
|
});
|