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

118 lines
2.4 KiB
Vue
Raw Normal View History

2017-08-17 22:00:37 +05:30
<script>
2018-12-13 13:39:08 +05:30
/* global katex */
import marked from 'marked';
import sanitize from 'sanitize-html';
import Prompt from './prompt.vue';
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
const renderer = new marked.Renderer();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
/*
2017-08-17 22:00:37 +05:30
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
*/
2018-12-13 13:39:08 +05:30
const katexRegexString = `(
2017-08-17 22:00:37 +05:30
^\\\\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]+}$
|
\\$\\$$
|
\\$
)
2018-12-13 13:39:08 +05:30
`
.replace(/\s/g, '')
.trim();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
renderer.paragraph = t => {
let text = t;
let inline = false;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
if (typeof katex !== 'undefined') {
const katexString = text
.replace(/&amp;/g, '&')
2019-10-12 21:52:04 +05:30
.replace(/&=&/g, '\\space=\\space') // eslint-disable-line @gitlab/i18n/no-non-i18n-strings
2018-12-13 13:39:08 +05:30
.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
2018-12-13 13:39:08 +05:30
if (numberOfMatches && numberOfMatches.length !== 0) {
if (matchLocation > 0) {
let matches = regex.exec(katexString);
inline = true;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +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
}
2018-12-13 13:39:08 +05:30
} else {
const matches = regex.exec(katexString);
text = katex.renderToString(matches[2]);
2017-08-17 22:00:37 +05:30
}
}
2018-12-13 13:39:08 +05:30
}
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
return `<p class="${inline ? 'inline-katex' : ''}">${text}</p>`;
};
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
marked.setOptions({
sanitize: true,
renderer,
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
export default {
components: {
prompt: Prompt,
},
props: {
cell: {
type: Object,
required: true,
2017-08-17 22:00:37 +05:30
},
2018-12-13 13:39:08 +05:30
},
computed: {
markdown() {
return sanitize(marked(this.cell.source.join('').replace(/\\/g, '\\\\')), {
allowedTags: false,
allowedAttributes: {
'*': ['class'],
},
});
2017-08-17 22:00:37 +05:30
},
2018-12-13 13:39:08 +05:30
},
};
2017-08-17 22:00:37 +05:30
</script>
2018-03-17 18:26:18 +05:30
<template>
<div class="cell text-cell">
<prompt />
2019-02-15 15:39:39 +05:30
<div class="markdown" v-html="markdown"></div>
2018-03-17 18:26:18 +05:30
</div>
</template>
2017-08-17 22:00:37 +05:30
<style>
2018-12-13 13:39:08 +05:30
.markdown .katex {
display: block;
text-align: center;
}
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
.markdown .inline-katex .katex {
display: inline;
text-align: initial;
}
2017-08-17 22:00:37 +05:30
</style>