Skip to content

Commit

Permalink
Feat: proper math (#52)
Browse files Browse the repository at this point in the history
* Package: bump version

* Feat: pass math to DOM in $$ tags

* Fix: remove KaTeX from style

* Feat: add setup.py for JupyterLab develop

* Package: bump markup dependency version

* Refactor: drop math rendering from docutils

We need to unify this across all math plugins, so for now just require the amsmath impl

* Feat: add `sphinx-display-math` plugin

This reproduces the sphinx treatment of display math for convenient multi-line and multiple-equation environments

* Chore: uncap markup dependency
  • Loading branch information
agoose77 authored Jun 22, 2022
1 parent dbaf3f9 commit 161a97f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 69 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-myst",
"version": "0.1.7-a5",
"version": "0.1.7-alpha.6",
"description": "A Myst renderer for JupyterLab",
"keywords": [
"jupyter",
Expand Down Expand Up @@ -45,9 +45,8 @@
"watch:labextension": "jupyter labextension watch ."
},
"dependencies": {
"@agoose77/jupyterlab-markup": "^2.0.0",
"@agoose77/jupyterlab-markup": "^2.1.0-alpha.0",
"@jupyterlab/application": "^3.0.0",
"katex": "^0.14.0",
"markdown-it": "^12.2.3",
"markdown-it-amsmath": "^0.3.1",
"markdown-it-docutils": "^0.1.4",
Expand Down Expand Up @@ -94,4 +93,4 @@
},
"outputDir": "jupyterlab_myst/labextension"
}
}
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ classifiers = [
]
dependencies = [
"jupyter_server>=1.6,<2",
"jupyterlab-markup~=2.0",
"jupyterlab-markup>=2.1.0a0",
"importlib_metadata"
]
dynamic = [
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# setup.py shim for use with versions of JupyterLab that require
# it for extensions.
__import__("setuptools").setup()
52 changes: 2 additions & 50 deletions src/builtins/docutils/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
import { simpleMarkdownItPlugin } from '@agoose77/jupyterlab-markup';
import { JupyterFrontEndPlugin } from '@jupyterlab/application';
import type MarkdownIt from 'markdown-it';
import katex from 'katex';

import { PACKAGE_NS } from '../../tokens';
import { EvalRole } from './roles';
import { EvalDirectiveAny, EvalFigureDirective } from './directives';

import {
directivesDefault,
rolesDefault,
IOptions
IOptions,
rolesDefault
} 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 Down Expand Up @@ -74,29 +49,6 @@ export const docutils: JupyterFrontEndPlugin<void> = simpleMarkdownItPlugin(
roles: roles,
directives: directives
});

// Add renderers to MarkdownIt
md.renderer.rules['math_block'] = (tokens, idx) => {
const token = tokens[idx];
const content = wrapDisplayMath(token.content.trim());
const rendered = katex.renderToString(content, {
displayMode: true,
throwOnError: false,
output: 'htmlAndMathml'
});
return `<div class="${token.attrGet('class')}">${rendered}</div>`;
};

md.renderer.rules['math_inline'] = (tokens, idx) => {
const token = tokens[idx];
const content = token.content.trim();
const rendered = katex.renderToString(content, {
displayMode: false,
throwOnError: false,
output: 'htmlAndMathml'
});
return `<span class="${token.attrGet('class')}">${rendered}</span>`;
};
}

return [wrappedDocutilsPlugin];
Expand Down
9 changes: 8 additions & 1 deletion src/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { frontMatter } from './front-matter';
import { docutils } from './docutils';
import { amsmath } from './amsmath';
import { mystExtras } from './myst-extras';
import { sphinxMath } from './sphinx-math';

/**
* Builtin plugins provided by this labextension
*/
export const BUILTINS = [frontMatter, docutils, amsmath, mystExtras];
export const BUILTINS = [
frontMatter,
docutils,
amsmath,
mystExtras,
sphinxMath
];
69 changes: 69 additions & 0 deletions src/builtins/sphinx-math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { simpleMarkdownItPlugin } from '@agoose77/jupyterlab-markup';
import { JupyterFrontEndPlugin } from '@jupyterlab/application';
import MarkdownIt from 'markdown-it';

import { PACKAGE_NS } from '../tokens';

function splitPart(text: string): string {
if (text.includes('\\\\')) {
return `\\begin{split}${text}\\end{split}`;
} else {
return text;
}
}

export function wrapDisplayMath(text: string): string {
const parts = text.split('\n\n');
const split_parts = parts.map(splitPart);

// Allow multiline equations using split
if (parts.length === 1) {
return `\\begin{equation}\\begin{split}${parts[0]}\\end{split}\\end{equation}\n`;
}
// Allow aligned equations with aligned
else if (parts.length > 1) {
let result = ' \\begin{align}\\begin{aligned}';
result += split_parts.join('\\\\\n');
result += ' \\end{aligned}\\end{align}';
return result;
} else {
return split_parts.join('////');
}
}

/**
* Provides Sphinx math-environment support
*/
export const sphinxMath: JupyterFrontEndPlugin<void> = simpleMarkdownItPlugin(
PACKAGE_NS,
{
id: 'sphinx-display-math',
title: 'Sphinx Display Math',
description:
'Plugin for transforming display math using the Sphinx displaymath rules',
documentationUrls: {},
examples: {
'Multi-line Equation ': '$$\nx = y \\\\ + mx + c\n$$\n',
'Multiple Equations ': '$$x = y\n\ny + z = 2$$\n'
},
plugin: async () => {
return [
(md: MarkdownIt) => {
md.core.ruler.push('sphinx-displaymath', state => {
state.tokens.forEach(token => {
if (token.type === 'math_block') {
token.content = wrapDisplayMath(token.content);
} else if (token.type === 'inline') {
token.children.forEach(token => {
if (token.type === 'math_inline_double') {
token.content = wrapDisplayMath(token.content);
}
});
}
});
});
}
];
}
}
);
1 change: 0 additions & 1 deletion style/base.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@import 'markdown-it-docutils';
@import 'katex/dist/katex.css';

/* Use theme colours */
:root {
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1695,11 +1695,6 @@ commander@7, commander@^7.0.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==

commander@^8.0.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==

commander@~6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-6.0.0.tgz#2b270da94f8fb9014455312f829a1129dbf8887e"
Expand Down Expand Up @@ -4100,13 +4095,6 @@ jws@^3.2.2:
jwa "^1.4.1"
safe-buffer "^5.0.1"

katex@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/katex/-/katex-0.14.1.tgz#e7e2f568d19739593ef169f206d19907d51d5f5d"
integrity sha512-CweL2N9NLce3SfRonyqRYahz7skD2/VVhGGrh+d3IytKjKEGg1vwFarLrtCOakwVJoqZxldkEr0eUkUwGkAOTw==
dependencies:
commander "^8.0.0"

keygrip@~1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/keygrip/-/keygrip-1.1.0.tgz#871b1681d5e159c62a445b0c74b615e0917e7226"
Expand Down

0 comments on commit 161a97f

Please sign in to comment.