Skip to content

Commit

Permalink
Fix: wrap display math like Sphinx (#49)
Browse files Browse the repository at this point in the history
* Fix: wrap display math like Sphinx

* Package: bump version
  • Loading branch information
agoose77 authored Jun 22, 2022
1 parent 3bc13ad commit dbaf3f9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -94,4 +94,4 @@
},
"outputDir": "jupyterlab_myst/labextension"
}
}
}
27 changes: 26 additions & 1 deletion src/builtins/docutils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -33,6 +57,7 @@ export const docutils: JupyterFrontEndPlugin<void> = simpleMarkdownItPlugin(
const docutilsPlugin = await import(
/* webpackChunkName: "markdown-it-docutils" */ 'markdown-it-docutils'
);

function wrappedDocutilsPlugin(md: MarkdownIt, options: IOptions) {
const roles = {
...(options?.roles ?? rolesDefault),
Expand All @@ -53,7 +78,7 @@ export const docutils: JupyterFrontEndPlugin<void> = 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,
Expand Down

0 comments on commit dbaf3f9

Please sign in to comment.