debian-mirror-gitlab/app/assets/javascripts/notebook/cells/markdown.vue

109 lines
2.2 KiB
Vue
Raw Normal View History

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