Skip to content

Commit 0264dc5

Browse files
committed
Render latex with katex
1 parent fb9211f commit 0264dc5

5 files changed

Lines changed: 41 additions & 4 deletions

File tree

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"imports-loader": "^0.8.0",
5353
"jquery": "^3.4.1",
5454
"js-sequence-diagrams": "git+https://github.com/Moeditor/js-sequence-diagrams.git",
55+
"katex": "^0.10.2",
5556
"lodash": "^4.17.15",
5657
"mermaid": "^8.2.3",
5758
"mini-css-extract-plugin": "^0.8.0",

src/extension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ export function activate(context: vscode.ExtensionContext) {
188188
md.use(require('markdown-it-mathjax')({
189189
beforeMath: '<span class="mathjax raw">',
190190
afterMath: '</span>',
191-
beforeInlineMath: '<span class="mathjax raw">\\(',
192-
afterInlineMath: '\\)</span>',
193-
beforeDisplayMath: '<span class="mathjax raw">\\[',
194-
afterDisplayMath: '\\]</span>'
191+
beforeInlineMath: '<span class="mathjax raw">',
192+
afterInlineMath: '</span>',
193+
beforeDisplayMath: '<span class="mathjax raw display">',
194+
afterDisplayMath: '</span>'
195195
}));
196196

197197
md.use(markdownitContainer, 'success', { render });

src/page.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import 'bootstrap3/dist/css/bootstrap.min.css';
44
import './mermaid.css';
55
import './github-gist.css';
66
import 'prismjs/themes/prism.css';
7+
import 'katex/dist/katex.css';
78

89
import './markdown.css';
910
import './style.css';
1011

1112
import * as mermaid from 'mermaid';
1213
import * as flowchart from 'flowchart.js';
14+
import * as katex from 'katex';
1315

1416
import 'js-sequence-diagrams';
1517
import Viz from 'viz.js';
@@ -116,3 +118,25 @@ graphvizs.each(function (key, value) {
116118
// console.warn(err)
117119
}
118120
})
121+
122+
$('span.mathjax.raw').removeClass('raw')
123+
.each(function (key, value) {
124+
var $value = $(value)
125+
var $ele = $(value).parent().parent()
126+
$value.unwrap()
127+
128+
let result
129+
if ($(value).hasClass('display')) {
130+
result = katex.renderToString($value.text(), {
131+
throwOnError: false,
132+
displayMode: true
133+
})
134+
} else {
135+
result = katex.renderToString($value.text(), {
136+
throwOnError: false
137+
})
138+
}
139+
140+
$value.html(result)
141+
$value.children().unwrap()
142+
})

src/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Window {
2+
MathJax: any
3+
}

0 commit comments

Comments
 (0)