Skip to content

Commit

Permalink
feat: interactive examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Feb 26, 2024
1 parent 70c8a3d commit 2fa5683
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/mermaid/src/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export default defineConfig({
},
],
},
vue: {
template: {
compilerOptions: {
isCustomElement: (tag) => tag.includes('-'),
},
},
},
});

// Top (across the page) menu
Expand Down
3 changes: 2 additions & 1 deletion packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const MermaidExample = async (md: MarkdownRenderer) => {
// (it also adds `v-pre` to ignore Vue template syntax)
md.options.highlight(token.content, 'mermaid', langAttrs)
}
</div>`;
</div>
<MermaidRun/>`;
} else if (token.info.trim() === 'mermaid') {
const key = index;
return `
Expand Down
9 changes: 9 additions & 0 deletions packages/mermaid/src/docs/.vitepress/theme/MermaidRun.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<codapi-snippet
engine="mermaid"
sandbox="mermaid"
editor="basic"
output-mode="svg"
output="@next"
></codapi-snippet>
</template>
69 changes: 69 additions & 0 deletions packages/mermaid/src/docs/.vitepress/theme/codapi.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* editor styles */
.vp-doc .language-mermaid pre {
padding: 0;
}

.vp-doc .language-mermaid code {
border-radius: 8px;
padding: 20px 24px;
}

.vp-doc .language-mermaid code:focus::after {
position: absolute;
bottom: 2px;
right: 8px;
content: 'Ctrl+Enter to run';
color: var(--vp-code-lang-color);
font-family: var(--vp-font-family-base);
font-size: 12px;
font-weight: 500;
}

/* widget styles */
codapi-snippet {
display: block;
}

codapi-toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 1em;
margin-bottom: 1em;
}
codapi-toolbar button {
font-size: 1em;
font-weight: 500;
color: var(--vp-c-brand-1);
text-decoration: none;
text-underline-offset: 2px;
transition: color 0.25s, opacity 0.25s;
}
codapi-toolbar button::after {
content: ' ▶';
}
.vp-doc codapi-toolbar a {
text-decoration: none;
}

codapi-status {
display: block;
white-space: nowrap;
}

.vp-doc codapi-ref a {
color: var(--vp-c-text-1);
font-weight: 400;
text-decoration: underline;
}

codapi-output {
position: relative;
display: block;
}
codapi-output[hidden] {
display: none;
}
codapi-output a[href='#close'] {
display: none;
}
50 changes: 50 additions & 0 deletions packages/mermaid/src/docs/.vitepress/theme/codapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Codapi execution engine for Mermaid.

import mermaid from 'mermaid';

// exec executes a mermaid code snippet.
const exec = async (req) => {
try {
const start = new Date().valueOf();
const res = await render(req.files['']);
const elapsed = new Date().valueOf() - start;
return {
ok: true,
duration: elapsed,
stdout: res,
stderr: '',
};
} catch (exc) {
return {
ok: false,
duration: 0,
stdout: '',
stderr: exc.toString(),
};
}
};

// render renders a mermaid diagram according to the spec.
const render = async (spec) => {
const id = 'mermaid-' + hashCode(spec);
const { svg } = await mermaid.render(id, spec);
return svg;
};

// hashCode calculates a hashcode for a string.
const hashCode = (s) => {
return s.split('').reduce(function (a, b) {
a = (a << 5) - a + b.charCodeAt(0);
return a & a;
}, 0);
};

if (!import.meta.env.SSR) {
// register mermaid execution engine in codapi (client-only).
import('@antonz/codapi/dist/snippet.mjs');
window.codapi = window.codapi ?? {};
window.codapi.engines = {
...window.codapi.engines,
...{ mermaid: { init: () => {}, exec } },
};
}
4 changes: 4 additions & 0 deletions packages/mermaid/src/docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import DefaultTheme from 'vitepress/theme';
import './custom.css';
// @ts-ignore
import Mermaid from './Mermaid.vue';
import MermaidRun from './MermaidRun.vue';
// @ts-ignore
import Contributors from '../components/Contributors.vue';
// @ts-ignore
Expand All @@ -14,6 +15,8 @@ import Theme from 'vitepress/theme';
import '../style/main.css';
import 'uno.css';
import type { EnhanceAppContext } from 'vitepress';
import './codapi.css';
import './codapi.js';

export default {
...DefaultTheme,
Expand All @@ -27,6 +30,7 @@ export default {
enhanceApp({ app, router }: EnhanceAppContext) {
// register global components
app.component('Mermaid', Mermaid);
app.component('MermaidRun', MermaidRun);
app.component('Contributors', Contributors);
router.onBeforeRouteChange = (to) => {
try {
Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/src/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"fetch-contributors": "tsx .vitepress/scripts/fetch-contributors.ts"
},
"dependencies": {
"@antonz/codapi": "^0.16.0",
"@vueuse/core": "^10.1.0",
"jiti": "^1.18.2",
"vue": "^3.3",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2fa5683

Please sign in to comment.