From 2863c87507a4b88bbb2c7661d466fac6dbc4f46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Fr=C3=B6hlich?= Date: Thu, 25 Apr 2024 11:13:36 +0200 Subject: [PATCH] feat: mermaid support --- docs/.vitepress/config.mts | 12 ++- docs/.vitepress/mermaid-markdown-all.ts | 53 +++++++++ docs/.vitepress/theme/Mermaid.vue | 136 ++++++++++++++++++++++++ docs/.vitepress/theme/index.ts | 11 ++ docs/.vitepress/theme/mermaid.ts | 7 ++ 5 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 docs/.vitepress/mermaid-markdown-all.ts create mode 100644 docs/.vitepress/theme/Mermaid.vue create mode 100644 docs/.vitepress/theme/index.ts create mode 100644 docs/.vitepress/theme/mermaid.ts diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index c9e109c..ee02494 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -1,18 +1,16 @@ import { defineConfig } from 'vitepress' -import { withMermaid } from "vitepress-plugin-mermaid"; +import MermaidExample from './mermaid-markdown-all'; import { version } from "../../package.json"; // https://vitepress.dev/reference/site-config export default - // withMermaid( defineConfig({ title: "ABAP OData Framework", lang: 'en-US', base: '/odata-fw/', description: "A odata framework for a SAP System. ", - head: [ ['link', { rel: 'icon', href: '/favicon.ico' }], ['meta', { property: 'og:type', content: 'website' }], @@ -24,6 +22,11 @@ export default sitemap: { hostname: "https://miggi92.github.io/odata-fw/" }, + markdown: { + config: async (md) => { + await MermaidExample(md); + } + }, themeConfig: { // https://vitepress.dev/reference/default-theme-config nav: nav(), @@ -71,8 +74,7 @@ export default }, } } - // ) - ); +); function nav() { return [ diff --git a/docs/.vitepress/mermaid-markdown-all.ts b/docs/.vitepress/mermaid-markdown-all.ts new file mode 100644 index 0000000..64a069b --- /dev/null +++ b/docs/.vitepress/mermaid-markdown-all.ts @@ -0,0 +1,53 @@ +import type { MarkdownRenderer } from 'vitepress'; + +const MermaidExample = async (md: MarkdownRenderer) => { + const defaultRenderer = md.renderer.rules.fence; + + if (!defaultRenderer) { + throw new Error('defaultRenderer is undefined'); + } + + md.renderer.rules.fence = (tokens, index, options, env, slf) => { + const token = tokens[index]; + const language = token.info.trim(); + if (language.startsWith('mermaid')) { + const key = index; + return ` + + + + + +`; + } else if (language === 'warning') { + return `

WARNING

${token.content}}

`; + } else if (language === 'note') { + return `

NOTE

${token.content}}

`; + } else if (language === 'regexp') { + // shiki doesn't yet support regexp code blocks, but the javascript + // one still makes RegExes look good + token.info = 'javascript'; + // use trimEnd to move trailing `\n` outside if the JavaScript regex `/` block + token.content = `/${token.content.trimEnd()}/\n`; + return defaultRenderer(tokens, index, options, env, slf); + } else if (language === 'jison') { + return `
+ + jison +
+      ${token.content.replace(//g, '>')}
+      
+
`; + } + + return defaultRenderer(tokens, index, options, env, slf); + }; +}; + +export default MermaidExample; diff --git a/docs/.vitepress/theme/Mermaid.vue b/docs/.vitepress/theme/Mermaid.vue new file mode 100644 index 0000000..b98c493 --- /dev/null +++ b/docs/.vitepress/theme/Mermaid.vue @@ -0,0 +1,136 @@ + + + + + diff --git a/docs/.vitepress/theme/index.ts b/docs/.vitepress/theme/index.ts new file mode 100644 index 0000000..c0ce460 --- /dev/null +++ b/docs/.vitepress/theme/index.ts @@ -0,0 +1,11 @@ +import DefaultTheme from 'vitepress/theme'; +import Mermaid from './Mermaid.vue'; +import type { EnhanceAppContext } from 'vitepress'; + +export default { + ...DefaultTheme, + enhanceApp({ app }: EnhanceAppContext) { + // register global components + app.component('Mermaid', Mermaid); + }, +}; diff --git a/docs/.vitepress/theme/mermaid.ts b/docs/.vitepress/theme/mermaid.ts new file mode 100644 index 0000000..4752a1c --- /dev/null +++ b/docs/.vitepress/theme/mermaid.ts @@ -0,0 +1,7 @@ +import mermaid, { type MermaidConfig } from 'mermaid'; + +export const render = async (id: string, code: string, config: MermaidConfig): Promise => { + mermaid.initialize(config); + const { svg } = await mermaid.render(id, code); + return svg; +};