Files
openmaic/OpenMAIC/packages/@openmaic/importer/test/mathSerializer.test.ts
2026-08-16 14:58:47 +08:00

37 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, it, expect } from 'vitest';
import { ommlToLatex } from '../src/serializer/mathSerializer';
const M = 'xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"';
const omath = (inner: string) => `<m:oMath ${M}>${inner}</m:oMath>`;
/**
* 回归auto-fix.md 坑表OMML → LaTeX 转换的 JS 兜底路径ommlToLatex
*
* - 分数等基本结构要转成正确的 LaTeX 命令。
* - 梯度/反向传播页用的 ∂(U+2202)/∇(U+2207):早先因为不在 Greek 归一化范围 →
* 泄漏成 lone surrogateKaTeX 报错把源码渲染成红字。postProcessLatex 补了
* ∂→\partial、∇→\nabla 的兜底。
*/
describe('mathSerializer · ommlToLatex', () => {
it('分数 m:f → \\frac{a}{b}', () => {
const latex = ommlToLatex(
omath(
'<m:f><m:num><m:r><m:t>a</m:t></m:r></m:num><m:den><m:r><m:t>b</m:t></m:r></m:den></m:f>',
),
);
expect(latex).toBe('\\frac{a}{b}');
});
it('∂ (U+2202) → \\partial不泄漏原字符', () => {
const latex = ommlToLatex(omath('<m:r><m:t>\u2202</m:t></m:r>'));
expect(latex).toContain('\\partial');
expect(latex).not.toContain('\u2202');
});
it('∇ (U+2207) → \\nabla不泄漏原字符', () => {
const latex = ommlToLatex(omath('<m:r><m:t>\u2207</m:t></m:r>'));
expect(latex).toContain('\\nabla');
expect(latex).not.toContain('\u2207');
});
});