Commit a8b7a4b7 authored by Phil Hughes's avatar Phil Hughes

Pulled in latest changes

parent 2f36c549
...@@ -10,8 +10,61 @@ ...@@ -10,8 +10,61 @@
import marked from 'marked'; import marked from 'marked';
import Prompt from './prompt.vue'; import Prompt from './prompt.vue';
const renderer = new marked.Renderer();
/*
Regex to match KaTex blocks.
Supports the following:
\begin{equation}<math>\end{equation}
$$<math>$$
inline $<math>$
The matched text then goes through the KaTex renderer & then outputs the HTML
*/
const katexRegexString = `(
^\\\\begin{[a-zA-Z]+}\\s
|
^\\$\\$
|
\\s\\$(?!\\$)
)
(.+?)
(
\\s\\\\end{[a-zA-Z]+}$
|
\\$\\$$
|
\\$
)
`.replace(/\s/g, '').trim();
renderer.paragraph = (t) => {
let text = t;
let inline = false;
if (typeof katex !== 'undefined') {
const katexString = text.replace(/\\/g, '\\');
const matches = new RegExp(katexRegexString, 'gi').exec(katexString);
if (matches && matches.length > 0) {
if (matches[1].trim() === '$' && matches[3].trim() === '$') {
inline = true;
text = `${katexString.replace(matches[0], '')} ${katex.renderToString(matches[2])}`;
} else {
text = katex.renderToString(matches[2]);
}
}
}
return `<p class="${inline ? 'inline-katex' : ''}">${text}</p>`;
};
marked.setOptions({ marked.setOptions({
sanitize: true, sanitize: true,
renderer,
}); });
export default { export default {
...@@ -26,20 +79,7 @@ ...@@ -26,20 +79,7 @@
}, },
computed: { computed: {
markdown() { markdown() {
const regex = new RegExp('^\\$\\$(.*)\\$\\$$', 'g'); return marked(this.cell.source.join(''));
const source = this.cell.source.map((line) => {
const matches = regex.exec(line.trim());
// Only render use the Katex library if it is actually loaded
if (matches && matches.length > 0 && typeof katex !== 'undefined') {
return katex.renderToString(matches[1]);
}
return line;
});
return marked(source.join(''));
}, },
}, },
}; };
...@@ -50,4 +90,9 @@ ...@@ -50,4 +90,9 @@
display: block; display: block;
text-align: center; text-align: center;
} }
.markdown .inline-katex .katex {
display: inline;
text-align: initial;
}
</style> </style>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment