Commit 1d16a3fc authored by NGPixel's avatar NGPixel

feat: mermaid support for markdown

parent 951ebb6b
...@@ -222,6 +222,9 @@ import twemoji from 'twemoji' ...@@ -222,6 +222,9 @@ import twemoji from 'twemoji'
// Prism (Syntax Highlighting) // Prism (Syntax Highlighting)
import Prism from 'prismjs' import Prism from 'prismjs'
// Mermaid
import mermaid from 'mermaid'
// Helpers // Helpers
import katexHelper from './common/katex' import katexHelper from './common/katex'
...@@ -331,6 +334,8 @@ md.renderer.rules.emoji = (token, idx) => { ...@@ -331,6 +334,8 @@ md.renderer.rules.emoji = (token, idx) => {
// Vue Component // Vue Component
// ======================================== // ========================================
let mermaidId = 0
export default { export default {
components: { components: {
markdownHelp markdownHelp
...@@ -367,6 +372,7 @@ export default { ...@@ -367,6 +372,7 @@ export default {
previewShown (newValue, oldValue) { previewShown (newValue, oldValue) {
if (newValue && !oldValue) { if (newValue && !oldValue) {
this.$nextTick(() => { this.$nextTick(() => {
this.renderMermaidDiagrams()
Prism.highlightAllUnder(this.$refs.editorPreview) Prism.highlightAllUnder(this.$refs.editorPreview)
Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs')) Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs'))
}) })
...@@ -387,6 +393,7 @@ export default { ...@@ -387,6 +393,7 @@ export default {
this.$store.set('editor/content', newContent) this.$store.set('editor/content', newContent)
this.previewHTML = md.render(newContent) this.previewHTML = md.render(newContent)
this.$nextTick(() => { this.$nextTick(() => {
this.renderMermaidDiagrams()
Prism.highlightAllUnder(this.$refs.editorPreview) Prism.highlightAllUnder(this.$refs.editorPreview)
Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs')) Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs'))
this.scrollSync(this.cm) this.scrollSync(this.cm)
...@@ -528,6 +535,15 @@ export default { ...@@ -528,6 +535,15 @@ export default {
this.$nextTick(() => { this.$nextTick(() => {
this.cm.refresh() this.cm.refresh()
}) })
},
renderMermaidDiagrams () {
document.querySelectorAll('.editor-markdown-preview pre.line-numbers > code.language-mermaid').forEach(elm => {
mermaidId++
const mermaidDef = elm.innerText
const mmElm = document.createElement('div')
mmElm.innerHTML = `<div id="mermaid-id-${mermaidId}">${mermaid.render(`mermaid-id-${mermaidId}`, mermaidDef)}</div>`
elm.parentElement.replaceWith(mmElm)
})
} }
}, },
mounted() { mounted() {
...@@ -537,6 +553,12 @@ export default { ...@@ -537,6 +553,12 @@ export default {
this.$store.set('editor/content', '# Header\nYour content here') this.$store.set('editor/content', '# Header\nYour content here')
} }
// Initialize Mermaid API
mermaid.initialize({
startOnLoad: false,
theme: this.$vuetify.theme.dark ? `dark` : `default`
})
// Initialize CodeMirror // Initialize CodeMirror
this.cm = CodeMirror.fromTextArea(this.$refs.cm, { this.cm = CodeMirror.fromTextArea(this.$refs.cm, {
......
...@@ -249,6 +249,7 @@ ...@@ -249,6 +249,7 @@
<script> <script>
import { StatusIndicator } from 'vue-status-indicator' import { StatusIndicator } from 'vue-status-indicator'
import Prism from 'prismjs' import Prism from 'prismjs'
import mermaid from 'mermaid'
import { get } from 'vuex-pathify' import { get } from 'vuex-pathify'
import _ from 'lodash' import _ from 'lodash'
import ClipboardJS from 'clipboard' import ClipboardJS from 'clipboard'
...@@ -432,6 +433,12 @@ export default { ...@@ -432,6 +433,12 @@ export default {
// -> Highlight Code Blocks // -> Highlight Code Blocks
Prism.highlightAllUnder(this.$refs.container) Prism.highlightAllUnder(this.$refs.container)
// -> Render Mermaid diagrams
mermaid.mermaidAPI.initialize({
startOnLoad: true,
theme: this.$vuetify.theme.dark ? `dark` : `default`
})
// -> Handle anchor scrolling // -> Handle anchor scrolling
this.$nextTick(() => { this.$nextTick(() => {
if (window.location.hash && window.location.hash.length > 1) { if (window.location.hash && window.location.hash.length > 1) {
......
...@@ -248,6 +248,7 @@ ...@@ -248,6 +248,7 @@
"ignore-loader": "0.1.2", "ignore-loader": "0.1.2",
"jest": "25.1.0", "jest": "25.1.0",
"js-cookie": "2.2.1", "js-cookie": "2.2.1",
"mermaid": "8.4.8",
"mini-css-extract-plugin": "0.9.0", "mini-css-extract-plugin": "0.9.0",
"moment-duration-format": "2.3.2", "moment-duration-format": "2.3.2",
"offline-plugin": "5.0.7", "offline-plugin": "5.0.7",
......
module.exports = { module.exports = {
init($, config) { init($, config) {
$('pre.prismjs > code.language-mermaid').each((i, elm) => {
const mermaidContent = $(elm).html()
$(elm).parent().replaceWith(`<div class="mermaid">${mermaidContent}</div>`)
})
} }
} }
This diff was suppressed by a .gitattributes entry.
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