Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use current mermaid version in docs. #3846

Merged
merged 14 commits into from
Nov 29, 2022
1 change: 1 addition & 0 deletions cypress/integration/rendering/theme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('themeCSS balancing, it', () => {
});
});

// TODO: Delete/Rename this describe, keeping the inner contents.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah 😄 Could not agree more

describe('Pie Chart', () => {
// beforeEach(()=>{
// cy.clock((new Date('2014-06-09')).getTime());
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"express": "^4.18.2",
"globby": "^13.1.2",
"husky": "^8.0.2",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.3.1",
"jison": "^0.4.18",
"js-yaml": "^4.1.0",
Expand All @@ -107,9 +106,6 @@
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
"vite": "^3.2.3",
"vitepress": "^1.0.0-alpha.28",
"vitepress-plugin-mermaid": "^2.0.8",
"vitepress-plugin-search": "^1.0.4-alpha.15",
"vitest": "^0.25.3"
},
"volta": {
Expand Down
6 changes: 3 additions & 3 deletions packages/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"d3": "^7.0.0",
"dagre-d3-es": "7.0.4",
"dompurify": "2.4.1",
"fast-clone": "^1.5.13",
"khroma": "^2.0.0",
"lodash-es": "^4.17.21",
"moment-mini": "^2.24.0",
Expand All @@ -81,7 +80,6 @@
"coveralls": "^3.1.1",
"cspell": "^6.14.3",
"globby": "^13.1.2",
"identity-obj-proxy": "^3.0.0",
"jison": "^0.4.18",
"js-base64": "^3.7.2",
"jsdom": "^20.0.2",
Expand All @@ -95,7 +93,9 @@
"typedoc": "^0.23.18",
"typedoc-plugin-markdown": "^3.13.6",
"typescript": "^4.8.4",
"unist-util-flatmap": "^1.0.0"
"unist-util-flatmap": "^1.0.0",
"vitepress": "^1.0.0-alpha.28",
"vitepress-plugin-search": "^1.0.4-alpha.15"
},
"files": [
"dist",
Expand Down
3 changes: 1 addition & 2 deletions packages/mermaid/src/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { version } from '../../../package.json';
import MermaidExample from './mermaid-markdown-all';
import { MermaidMarkdown } from 'vitepress-plugin-mermaid';
import { defineConfig, MarkdownOptions } from 'vitepress';

const allMarkdownTransformers: MarkdownOptions = {
// the shiki theme to highlight code blocks
theme: 'github-dark',
config: async (md) => {
await MermaidExample(md);
MermaidMarkdown(md);
},
};

Expand Down Expand Up @@ -108,6 +106,7 @@ function sidebarSyntax() {
{ text: 'Requirement Diagram', link: '/syntax/requirementDiagram' },
{ text: 'Gitgraph (Git) Diagram 🔥', link: '/syntax/gitgraph' },
{ text: 'C4C Diagram (Context) Diagram 🦺⚠️', link: '/syntax/c4c' },
{ text: 'Mindmaps 🔥', link: '/syntax/mindmap' },
{ text: 'Other Examples', link: '/syntax/examples' },
],
},
Expand Down
35 changes: 24 additions & 11 deletions packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,30 @@ const MermaidExample = async (md: MarkdownRenderer) => {

// doing ```mermaid-example {line-numbers=5 highlight=14-17} is not supported
const langAttrs = '';
return `<h5>Code:</h5>
<div class="language-mermaid">
<button class="copy"></button>
<span class="lang">mermaid</span>
${
// html is pre-escaped by the highlight function
// (it also adds `v-pre` to ignore Vue template syntax)
md.options.highlight(token.content, 'mermaid', langAttrs)
}
</div>
<h5>Diagram:</h5>`;
return `
<h5>Code:</h5>
<div class="language-mermaid">
<button class="copy"></button>
<span class="lang">mermaid</span>
${
// html is pre-escaped by the highlight function
// (it also adds `v-pre` to ignore Vue template syntax)
md.options.highlight(token.content, 'mermaid', langAttrs)
}
</div>`;
} else if (token.info.trim() === 'mermaid') {
const key = index;
return `
<Suspense>
<template #default>
<Mermaid id="mermaid-${key}" graph="${encodeURIComponent(token.content)}"></Mermaid>
</template>
<!-- loading state via #fallback slot -->
<template #fallback>
Loading...
</template>
</Suspense>
`;
}
if (token.info.trim() === 'warning') {
return `<div class="warning custom-block"><p class="custom-block-title">WARNING</p><p>${token.content}}</p></div>`;
Expand Down
66 changes: 66 additions & 0 deletions packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<template>
<div v-html="svg"></div>
</template>

<script setup>
import { onMounted, onUnmounted, ref } from 'vue';
import { render } from './mermaid';

const props = defineProps({
graph: {
type: String,
required: true,
},
id: {
type: String,
required: true,
},
});

const svg = ref(null);
let mut = null;

const mermaidConfig = {
securityLevel: 'loose',
startOnLoad: false,
};

onMounted(async () => {
mut = new MutationObserver(() => renderChart());
mut.observe(document.documentElement, { attributes: true });
await renderChart();

//refresh images on first render
const hasImages = /<img([\w\W]+?)>/.exec(decodeURIComponent(props.graph))?.length > 0;
if (hasImages)
setTimeout(() => {
let imgElements = document.getElementsByTagName('img');
let imgs = Array.from(imgElements);
if (imgs.length) {
Promise.all(
imgs
.filter((img) => !img.complete)
.map(
(img) =>
new Promise((resolve) => {
img.onload = img.onerror = resolve;
})
)
).then(() => {
renderChart();
});
}
}, 100);
});

onUnmounted(() => mut.disconnect());

const renderChart = async () => {
console.log('rendering chart' + props.id + props.graph);
const hasDarkClass = document.documentElement.classList.contains('dark');
mermaidConfig.theme = hasDarkClass ? 'dark' : 'default';

console.log({ mermaidConfig });
svg.value = await render(props.id, decodeURIComponent(props.graph), mermaidConfig);
};
</script>
4 changes: 2 additions & 2 deletions packages/mermaid/src/docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DefaultTheme from 'vitepress/theme';
// @ts-ignore
import Mermaid from 'vitepress-plugin-mermaid/Mermaid.vue';
import './custom.css';
// @ts-ignore
import Mermaid from './Mermaid.vue';
import { getRedirect } from './redirect';

export default {
Expand Down
14 changes: 14 additions & 0 deletions packages/mermaid/src/docs/.vitepress/theme/mermaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import mermaid, { type MermaidConfig } from 'mermaid';
import mindmap from '@mermaid-js/mermaid-mindmap';

try {
await mermaid.registerExternalDiagrams([mindmap]);
} catch (e) {
console.error(e);
}

export const render = async (id: string, code: string, config: MermaidConfig): Promise<string> => {
mermaid.initialize(config);
const svg = await mermaid.renderAsync(id, code);
return svg;
};
5 changes: 5 additions & 0 deletions packages/mermaid/src/docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export default defineConfig({
resolve: {
alias: {
mermaid: path.join(__dirname, '../../dist/mermaid.esm.min.mjs'), // Use this one to build

'@mermaid-js/mermaid-mindmap': path.join(
__dirname,
'../../../mermaid-mindmap/dist/mermaid-mindmap.esm.min.mjs'
), // Use this one to build
},
},
server: {
Expand Down
Loading