Skip to content

Commit

Permalink
Desktop,Mobile: Fixes #9455: Fix KaTeX rendering (#9456)
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator committed Dec 6, 2023
1 parent 604dcbc commit fd5a4dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/app-cli/tests/MdToHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>');
});
});
14 changes: 12 additions & 2 deletions packages/renderer/MdToHtml/rules/katex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit fd5a4dc

Please sign in to comment.