Skip to content

Commit

Permalink
Merge pull request #53 from dominikcz/main
Browse files Browse the repository at this point in the history
allow to set a class for parent div container
  • Loading branch information
emersonbottero committed Aug 4, 2023
2 parents 07e0c0d + e6bb5d8 commit 541839d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { version } from "../../package.json";

export default withMermaid(
defineConfig({
// mermaidPlugin: {
// class: "mermaid my-class", // set additional css classes for parent container
// },
lang: "en-US",
title: "VitePress Plugin Mermaid",
description: "Mermaid support for vitepress",
Expand Down
6 changes: 4 additions & 2 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export default withMermaid({
mermaid: {
// refer https://mermaid.js.org/config/setup/modules/mermaidAPI.html#mermaidapi-configuration-defaults for options
},
// optionally set additional config for plugin itself with MermaidPluginConfig
mermaidPlugin: {
class: "mermaid my-class", // set additional css classes for parent container
},
});
```

Use in any Markdown file

Code with ```mmd

```mmd
Expand Down
7 changes: 6 additions & 1 deletion src/Mermaid.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-html="svg" class="mermaid"></div>
<div v-html="svg" :class="props.class"></div>
</template>

<script setup>
Expand Down Expand Up @@ -27,6 +27,11 @@ const props = defineProps({
type: String,
required: true,
},
class:{
type: String,
required: false,
default: "mermaid",
}
});
const svg = ref(null);
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type UserConfig } from "vitepress";
import { MermaidMarkdown } from "./mermaid-markdown";
import { MermaidPlugin } from "./mermaid-plugin";
import { MermaidPlugin, MermaidPluginConfig } from "./mermaid-plugin";
import { MermaidConfig } from "mermaid/dist/config.type";

export { MermaidMarkdown } from "./mermaid-markdown";
Expand All @@ -11,14 +11,15 @@ export { UserConfig };
declare module "vitepress" {
interface UserConfig {
mermaid?: MermaidConfig;
mermaidPlugin?: MermaidPluginConfig;
}
}

export const withMermaid = (config: UserConfig) => {
if (!config.markdown) config.markdown = {};
const markdownConfigOriginal = config.markdown.config || (() => {});
config.markdown.config = (...args) => {
MermaidMarkdown(...args);
MermaidMarkdown(...args, config.mermaidPlugin);
markdownConfigOriginal(...args);
};

Expand Down
5 changes: 3 additions & 2 deletions src/mermaid-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const MermaidMarkdown = (md) => {
export const MermaidMarkdown = (md, pluginOptions) => {
const sum = (o) => {
function pad(hash, len) {
while (hash.length < len) {
Expand Down Expand Up @@ -61,10 +61,11 @@ export const MermaidMarkdown = (md) => {
if (token.info.trim() === "mermaid") {
try {
const key = index;
const cssClass = pluginOptions?.class || 'mermaid';
return `
<Suspense>
<template #default>
<Mermaid id="mermaid-${key}" graph="${encodeURIComponent(
<Mermaid id="mermaid-${key}" class="${cssClass}" graph="${encodeURIComponent(
token.content
)}"></Mermaid>
</template>
Expand Down
5 changes: 5 additions & 0 deletions src/mermaid-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface MermaidPluginOptions extends MermaidConfig {
externalDiagrams: ExternalDiagramDefinition[];
}

// Additional configuration for plugin itself. Separate model, not to risk name conflicts with future MermaidConfig options
export interface MermaidPluginConfig {
class?: string;
}

const DEFAULT_OPTIONS: MermaidConfig = {
//We set loose as default here because is needed to load images
securityLevel: "loose",
Expand Down

0 comments on commit 541839d

Please sign in to comment.