1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-26 18:58:21 +02:00

Trying to add math support

This commit is contained in:
Laurent Cozic 2018-01-11 19:51:01 +00:00
parent 424443a2d8
commit 53da63e371
2 changed files with 19 additions and 5 deletions

View File

@ -64,6 +64,7 @@
"levenshtein": "^1.0.5", "levenshtein": "^1.0.5",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"markdown-it": "^8.4.0", "markdown-it": "^8.4.0",
"markdown-it-katex": "^2.0.3",
"md5": "^2.2.1", "md5": "^2.2.1",
"mime": "^2.0.3", "mime": "^2.0.3",
"moment": "^2.19.1", "moment": "^2.19.1",

View File

@ -156,7 +156,7 @@ class MdToHtml {
} }
} }
renderTokens_(tokens, options) { renderTokens_(markdownIt, tokens, options) {
let output = []; let output = [];
let previousToken = null; let previousToken = null;
let anchorAttrs = []; let anchorAttrs = [];
@ -212,9 +212,15 @@ class MdToHtml {
output.push('<br/>'); output.push('<br/>');
} else if (t.type === 'hr') { } else if (t.type === 'hr') {
output.push('<hr/>'); output.push('<hr/>');
} else if (t.type === 'math_inline') {
const mathText = markdownIt.render('$' + t.content + '$');
output.push(mathText);
} else if (t.type === 'math_block') {
const mathText = markdownIt.render('$$' + t.content + '$$');
output.push(mathText);
} else { } else {
if (t.children) { if (t.children) {
const parsedChildren = this.renderTokens_(t.children, options); const parsedChildren = this.renderTokens_(markdownIt, t.children, options);
output = output.concat(parsedChildren); output = output.concat(parsedChildren);
} else { } else {
if (t.content) { if (t.content) {
@ -260,7 +266,8 @@ class MdToHtml {
breaks: true, breaks: true,
linkify: true, linkify: true,
}); });
const env = {};
md.use(require('markdown-it-katex'));
// Hack to make checkboxes clickable. Ideally, checkboxes should be parsed properly in // Hack to make checkboxes clickable. Ideally, checkboxes should be parsed properly in
// renderTokens_(), but for now this hack works. Marking it with HORRIBLE_HACK so // renderTokens_(), but for now this hack works. Marking it with HORRIBLE_HACK so
@ -278,12 +285,13 @@ class MdToHtml {
} }
} }
const env = {};
const tokens = md.parse(body, env); const tokens = md.parse(body, env);
// console.info(body); // console.info(body);
// console.info(tokens); // console.info(tokens);
let renderedBody = this.renderTokens_(tokens, options); let renderedBody = this.renderTokens_(md, tokens, options);
if (HORRIBLE_HACK) { if (HORRIBLE_HACK) {
let loopCount = 0; let loopCount = 0;
@ -374,9 +382,14 @@ class MdToHtml {
width: auto; width: auto;
max-width: 100%; max-width: 100%;
} }
.katex .mfrac .frac-line:before {
/* top: 50%; */
/* padding-bottom: .7em; */
}
`; `;
const styleHtml = '<style>' + normalizeCss + "\n" + css + '</style>'; const styleHtml = '<style>' + normalizeCss + "\n" + css + '</style>' + '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css">';
const output = styleHtml + renderedBody; const output = styleHtml + renderedBody;