-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to render diagram via marked and mermaid
The theme isn't optimal but better than nothing I guess ?
- Loading branch information
Showing
3 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<script setup lang="ts"> | ||
import { ref, nextTick, onMounted, watch } from "vue"; | ||
import { marked } from "marked"; | ||
import mermaid from "mermaid"; | ||
// Props | ||
const props = defineProps({ | ||
markdownContent: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
// Refs | ||
const renderedHtml = ref<string>(""); | ||
// Initialize Mermaid | ||
mermaid.initialize({ | ||
startOnLoad: false, | ||
theme: "neutral", | ||
}); | ||
// Watch Markdown Content for Changes | ||
watch( | ||
() => props.markdownContent, | ||
(newContent) => { | ||
renderMarkdown(newContent); | ||
}, | ||
{ immediate: true }, | ||
); | ||
// Render Markdown Function | ||
async function renderMarkdown(content: string) { | ||
console.log("rendering now"); | ||
renderedHtml.value = await marked(content); | ||
await nextTick(); | ||
renderMermaidDiagrams(); | ||
} | ||
// Render Mermaid Diagrams | ||
function renderMermaidDiagrams() { | ||
const mermaidElements = document.querySelectorAll(".language-mermaid"); | ||
mermaidElements.forEach((element) => { | ||
const parent = element.parentElement; | ||
const code = element.textContent || ""; | ||
if (parent) { | ||
try { | ||
const id = `mermaid-${Math.random().toString(36).substr(2, 9)}`; | ||
mermaid.render(id, code).then((value) => (parent.innerHTML = value.svg)); | ||
} catch (e) { | ||
console.error("Mermaid rendering error:", e); | ||
} | ||
} | ||
}); | ||
} | ||
</script> | ||
|
||
<template> | ||
<div v-html="renderedHtml"></div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters