Desktop,Mobile: Fixes #9455: Fix KaTeX rendering (#9456)

pull/9478/head
Henry Heino 2023-12-06 11:23:08 -08:00 committed by GitHub
parent 604dcbc35b
commit fd5a4dcbbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -291,4 +291,29 @@ describe('MdToHtml', () => {
expect(html.html).toContain(opening + trimmedTex + closing);
}
});
it('should render inline KaTeX after a numbered equation', async () => {
const mdToHtml = newTestMdToHtml();
// This test is intended to verify that inline KaTeX renders correctly
// after creating a numbered equation with \begin{align}...\end{align}.
//
// See https://github.com/laurent22/joplin/issues/9455 for details.
const markdown = [
'$$',
'\\begin{align}\\text{Block}\\end{align}',
'$$',
'',
'$\\text{Inline}$',
].join('\n');
const { html } = await mdToHtml.render(markdown, null, { bodyOnly: true });
// Because we don't control the output of KaTeX, this test should be as general as
// possible while still verifying that rendering (without an error) occurs.
// Should have rendered the inline and block content without errors
expect(html).toContain('Inline</span>');
expect(html).toContain('Block</span>');
});
});

View File

@ -43,12 +43,22 @@ function stringifyKatexOptions(options: any) {
// \prob: {tokens: Array(12), numArgs: 1}
// \expval: {tokens: Array(14), numArgs: 2}
// \wf: {tokens: Array(6), numArgs: 0}
// \@eqnsw: "1"
//
// Additionally, some KaTeX macros don't follow this general format. For example
// \@eqnsw: "1"
// is created by \begin{align}...\end{align} environments, and doesn't have a "tokens" property.
if (options.macros) {
const toSerialize: any = {};
for (const k of Object.keys(options.macros)) {
const macroText: string[] = options.macros[k].tokens.map((t: any) => t.text);
toSerialize[k] = `${macroText.join('')}_${options.macros[k].numArgs}`;
const macro = options.macros[k];
if (typeof macro === 'string') {
toSerialize[k] = `${macro}_string`;
} else {
const macroText: string[] = macro.tokens.map((t: any) => t.text);
toSerialize[k] = `${macroText.join('')}_${macro.numArgs}`;
}
}
newOptions.macros = toSerialize;
}