diff --git a/package.json b/package.json index 0a48f81..6107f32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jupyterlab-myst", - "version": "0.1.7-a4", + "version": "0.1.7-a5", "description": "A Myst renderer for JupyterLab", "keywords": [ "jupyter", @@ -94,4 +94,4 @@ }, "outputDir": "jupyterlab_myst/labextension" } -} +} \ No newline at end of file diff --git a/src/builtins/docutils/index.ts b/src/builtins/docutils/index.ts index 9dd1358..2691874 100644 --- a/src/builtins/docutils/index.ts +++ b/src/builtins/docutils/index.ts @@ -13,6 +13,30 @@ import { IOptions } from 'markdown-it-docutils'; +function splitPart(text: string): string { + if (text.includes('\\\\')) { + return `\\begin{split}${text}\\end{split}`; + } else { + return text; + } +} + +function wrapDisplayMath(text: string): string { + const parts = text.split('\n\n'); + const split_parts = parts.map(splitPart); + + if (parts.length === 1) { + return `\\begin{equation}\\begin{split}${parts[0]}\\end{split}\\end{equation}\n`; + } else if (parts.length > 1) { + let result = ' \\begin{align}\\begin{aligned}'; + result += split_parts.join('\\\\'); + result += ' \\begin{aligned}\\end{align}'; + return result; + } else { + return split_parts.join('////'); + } +} + /** * Provides docutils roles and directives */ @@ -33,6 +57,7 @@ export const docutils: JupyterFrontEndPlugin = simpleMarkdownItPlugin( const docutilsPlugin = await import( /* webpackChunkName: "markdown-it-docutils" */ 'markdown-it-docutils' ); + function wrappedDocutilsPlugin(md: MarkdownIt, options: IOptions) { const roles = { ...(options?.roles ?? rolesDefault), @@ -53,7 +78,7 @@ export const docutils: JupyterFrontEndPlugin = simpleMarkdownItPlugin( // Add renderers to MarkdownIt md.renderer.rules['math_block'] = (tokens, idx) => { const token = tokens[idx]; - const content = token.content.trim(); + const content = wrapDisplayMath(token.content.trim()); const rendered = katex.renderToString(content, { displayMode: true, throwOnError: false,