diff options
author | Phil Hughes <me@iamphill.com> | 2017-04-13 13:02:17 +0100 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2017-04-13 13:02:17 +0100 |
commit | a8b7a4b71760932341b3a58542b5702495d65ba9 (patch) | |
tree | 7d3a8daaa8060fcfd63e7faf91d5956836d308ae | |
parent | 2f36c5497cd9850035150641e72f90e42811df5c (diff) | |
download | gitlab-ce-a8b7a4b71760932341b3a58542b5702495d65ba9.tar.gz |
Pulled in latest changes
-rw-r--r-- | app/assets/javascripts/notebook/cells/markdown.vue | 73 |
1 files changed, 59 insertions, 14 deletions
diff --git a/app/assets/javascripts/notebook/cells/markdown.vue b/app/assets/javascripts/notebook/cells/markdown.vue index 4e0e60284f1..3e8240d10ec 100644 --- a/app/assets/javascripts/notebook/cells/markdown.vue +++ b/app/assets/javascripts/notebook/cells/markdown.vue @@ -10,8 +10,61 @@ 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\\$(?!\\$) + ) + (.+?) + ( + \\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({ sanitize: true, + renderer, }); export default { @@ -26,20 +79,7 @@ }, computed: { markdown() { - const regex = new RegExp('^\\$\\$(.*)\\$\\$$', 'g'); - - 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('')); + return marked(this.cell.source.join('')); }, }, }; @@ -50,4 +90,9 @@ display: block; text-align: center; } + +.markdown .inline-katex .katex { + display: inline; + text-align: initial; +} </style> |