Skip to content

Commit

Permalink
feat(mermaid): Integrate mermaid in markdown renderer (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreascorti authored Jan 26, 2024
1 parent 00431f2 commit 259f1df
Show file tree
Hide file tree
Showing 5 changed files with 6,961 additions and 4,685 deletions.
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@types/dom-view-transitions": "^1.0.4",
"bootstrap-icons": "^1.11.3",
"eslint-plugin-svelte": "^2.35.1",
"mermaid": "^10.7.0",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.2.9",
"svelte-check": "^3.6.3",
Expand Down
29 changes: 29 additions & 0 deletions demo/src/lib/layout/Mermaid.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script lang="ts">
import mermaid, {type MermaidConfig} from 'mermaid';
export let code: string;
const mermaidDirective = (element: HTMLElement, config: MermaidConfig = {}) => {
const renderMermaid = async () => {
const {svg} = await mermaid.render('mermaid', code);
element.innerHTML = svg;
};
mermaid.initialize(config);
if (code) {
renderMermaid();
}
return {
update: (newConfig: MermaidConfig) => {
config = newConfig;
mermaid.initialize(config);
if (code) {
renderMermaid();
}
},
};
};
</script>

<div use:mermaidDirective class={`bg-light-subtle doc p-1 d-flex flex-nowrap align-items-start py-2 px-2 px-sm-4`}></div>

<style lang="scss">
</style>
3 changes: 3 additions & 0 deletions demo/src/lib/markdown/renderers/MdCode.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import Code from '$lib/layout/Code.svelte';
import Mermaid from '$lib/layout/Mermaid.svelte';
import Sample from '$lib/layout/Sample.svelte';
import type {SampleInfo} from '$lib/layout/sample';
import {selectedFramework$} from '$lib/stores';
Expand Down Expand Up @@ -49,6 +50,8 @@
{:else}
Sample not found, make sure to fill the samples.ts file.
{/if}
{:else if lang === 'mermaid'}
<Mermaid {code} />
{:else}
<Code language={lang} {code} />
{/if}
1 change: 1 addition & 0 deletions demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default defineConfig({
'@floating-ui/dom',
'svelte-markdown',
'marked',
'mermaid',
],
},
});
Loading

0 comments on commit 259f1df

Please sign in to comment.