2017-04-11 17:50:21 +00:00
|
|
|
<script>
|
|
|
|
/* global katex */
|
|
|
|
import marked from 'marked';
|
2018-01-09 08:39:22 +00:00
|
|
|
import sanitize from 'sanitize-html';
|
2017-04-11 17:50:21 +00:00
|
|
|
import Prompt from './prompt.vue';
|
|
|
|
|
2017-04-13 12:02:17 +00:00
|
|
|
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\\$(?!\\$)
|
|
|
|
)
|
2017-05-30 10:30:58 +00:00
|
|
|
((.|\\n)+?)
|
2017-04-13 12:02:17 +00:00
|
|
|
(
|
|
|
|
\\s\\\\end{[a-zA-Z]+}$
|
|
|
|
|
|
|
|
|
\\$\\$$
|
|
|
|
|
|
|
|
|
\\$
|
|
|
|
)
|
|
|
|
`.replace(/\s/g, '').trim();
|
|
|
|
|
|
|
|
renderer.paragraph = (t) => {
|
|
|
|
let text = t;
|
|
|
|
let inline = false;
|
|
|
|
|
|
|
|
if (typeof katex !== 'undefined') {
|
2017-05-30 12:08:38 +00:00
|
|
|
const katexString = text.replace(/&/g, '&')
|
|
|
|
.replace(/&=&/g, '\\space=\\space')
|
2017-05-30 11:59:14 +00:00
|
|
|
.replace(/<(\/?)em>/g, '_');
|
2017-05-30 10:30:58 +00:00
|
|
|
const regex = new RegExp(katexRegexString, 'gi');
|
2017-05-30 13:53:12 +00:00
|
|
|
const matchLocation = katexString.search(regex);
|
2017-05-30 10:30:58 +00:00
|
|
|
const numberOfMatches = katexString.match(regex);
|
2017-04-13 12:02:17 +00:00
|
|
|
|
2017-05-30 10:30:58 +00:00
|
|
|
if (numberOfMatches && numberOfMatches.length !== 0) {
|
2017-05-30 13:53:12 +00:00
|
|
|
if (matchLocation > 0) {
|
2017-05-30 10:30:58 +00:00
|
|
|
let matches = regex.exec(katexString);
|
2017-04-13 12:02:17 +00:00
|
|
|
inline = true;
|
|
|
|
|
2017-05-30 10:30:58 +00:00
|
|
|
while (matches !== null) {
|
2017-05-30 12:41:14 +00:00
|
|
|
const renderedKatex = katex.renderToString(matches[0].replace(/\$/g, ''));
|
|
|
|
text = `${text.replace(matches[0], ` ${renderedKatex}`)}`;
|
2017-05-30 10:30:58 +00:00
|
|
|
matches = regex.exec(katexString);
|
|
|
|
}
|
2017-04-13 12:02:17 +00:00
|
|
|
} else {
|
2017-05-30 10:30:58 +00:00
|
|
|
const matches = regex.exec(katexString);
|
2017-04-13 12:02:17 +00:00
|
|
|
text = katex.renderToString(matches[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<p class="${inline ? 'inline-katex' : ''}">${text}</p>`;
|
|
|
|
};
|
|
|
|
|
2017-04-13 07:03:58 +00:00
|
|
|
marked.setOptions({
|
|
|
|
sanitize: true,
|
2017-04-13 12:02:17 +00:00
|
|
|
renderer,
|
2017-04-13 07:03:58 +00:00
|
|
|
});
|
|
|
|
|
2017-04-11 17:50:21 +00:00
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
prompt: Prompt,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
cell: {
|
|
|
|
type: Object,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
markdown() {
|
2018-01-09 08:39:22 +00:00
|
|
|
return sanitize(marked(this.cell.source.join('').replace(/\\/g, '\\\\')), {
|
|
|
|
allowedTags: false,
|
|
|
|
allowedAttributes: {
|
|
|
|
'*': ['class'],
|
|
|
|
},
|
|
|
|
});
|
2017-04-11 17:50:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2017-10-04 00:07:41 +00:00
|
|
|
<template>
|
|
|
|
<div class="cell text-cell">
|
|
|
|
<prompt />
|
2018-01-05 00:18:35 +00:00
|
|
|
<div
|
|
|
|
class="markdown"
|
|
|
|
v-html="markdown">
|
|
|
|
</div>
|
2017-10-04 00:07:41 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2017-04-11 17:50:21 +00:00
|
|
|
<style>
|
2018-01-05 00:18:35 +00:00
|
|
|
.markdown .katex {
|
|
|
|
display: block;
|
|
|
|
text-align: center;
|
|
|
|
}
|
2017-04-13 12:02:17 +00:00
|
|
|
|
2018-01-05 00:18:35 +00:00
|
|
|
.markdown .inline-katex .katex {
|
|
|
|
display: inline;
|
|
|
|
text-align: initial;
|
|
|
|
}
|
2017-04-11 17:50:21 +00:00
|
|
|
</style>
|