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

Adding support for mermaid directives and themeVariables #192

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ graph TD;

- `markdown-mermaid.languages` — Configures language ids for Mermaid code blocks. The default is `["mermaid"]`.

- `markdown-mermaid.directives` — Configures directives and `themeVariables`.
(See [directives](https://mermaid.js.org/config/directives.html), [themeVariables](https://mermaid.js.org/config/theming.html)).
Use `"debug": true` to display directives being sent to `mermaid.initialize()`. When `themeVariables` is set, any theme is overridden with `"theme": "base"`.

Example (add to `setttings.json`):
```json
{
"markdown-mermaid.directives": {
Copy link
Owner

Choose a reason for hiding this comment

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

This new setting should also be documented in the package.json

"sequence": {
"mirrorActors": false,
},
"themeVariables": {
"primaryColor": "#BB2528",
"primaryTextColor": "#fff",
"primaryBorderColor": "#7C0000",
"lineColor": "#F8B229",
"secondaryColor": "#006100",
"tertiaryColor": "#fff"
},
"debug": true,
}
}
```

![A mermaid diagram with directives and themeVariables](https://github.com/frankieliu/vscode-markdown-mermaid/blob/adding-mermaid-config/docs/sequenceDiagramWithDirectives.png?raw=true)

### Using custom CSS in the Markdown Preview

You can use the built-in functionality to add custom CSS. More info can be found in the [markdown.styles documentation](https://code.visualstudio.com/Docs/languages/markdown#_using-your-own-css)
Expand Down
Binary file added docs/sequenceDiagramWithDirectives.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 3 additions & 13 deletions src/markdownPreview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@
*
* This runs in the markdown preview's webview.
*/
import mermaid, { MermaidConfig } from 'mermaid';
import { registerMermaidAddons, renderMermaidBlocksInElement } from '../shared-mermaid';
import mermaid from 'mermaid';
import { loadMermaidConfig, registerMermaidAddons, renderMermaidBlocksInElement } from '../shared-mermaid';

function init() {
const configSpan = document.getElementById('markdown-mermaid');
const darkModeTheme = configSpan?.dataset.darkModeTheme;
const lightModeTheme = configSpan?.dataset.lightModeTheme;

const config: MermaidConfig = {
startOnLoad: false,
theme: (document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? darkModeTheme ?? 'dark'
: lightModeTheme ?? 'default' ) as MermaidConfig['theme'],
};

const config= loadMermaidConfig();
mermaid.initialize(config);
registerMermaidAddons();

Expand Down
4 changes: 4 additions & 0 deletions src/shared-mermaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ export function loadMermaidConfig(): MermaidConfig {
const darkModeTheme = configSpan?.dataset.darkModeTheme;
const lightModeTheme = configSpan?.dataset.lightModeTheme;

const directives = JSON.parse(configSpan?.dataset.directives ?? '');

return {
startOnLoad: false,
theme: (document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? darkModeTheme ?? 'dark'
: lightModeTheme ?? 'default') as MermaidConfig['theme'],

...directives,
};
}
18 changes: 15 additions & 3 deletions src/vscode-extension/themeing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ function sanitizeMermaidTheme(theme: string | undefined) {
export function injectMermaidTheme(md: MarkdownIt) {
const render = md.renderer.render;
md.renderer.render = function (...args) {
const darkModeTheme = sanitizeMermaidTheme(vscode.workspace.getConfiguration(configSection).get('darkModeTheme'));
const lightModeTheme = sanitizeMermaidTheme(vscode.workspace.getConfiguration(configSection).get('lightModeTheme'));
const config = vscode.workspace.getConfiguration(configSection);

const darkModeTheme = sanitizeMermaidTheme(config.get('darkModeTheme'));
const lightModeTheme = sanitizeMermaidTheme(config.get('lightModeTheme'));

let directivesAttr = '';
try {
const rawDirectives = config.get<object>("directives", {});
directivesAttr = JSON.stringify(rawDirectives);
} catch (e) {
console.error('Failed to serialize mermaid directives', e);
}

return `<span id="${configSection}" aria-hidden="true"
data-dark-mode-theme="${darkModeTheme}"
data-light-mode-theme="${lightModeTheme}"></span>
data-light-mode-theme="${lightModeTheme}"
data-directives='${directivesAttr.replaceAll(`'`, '&apos;')}'></span>
${render.apply(md.renderer, args)}`;
};
return md;
Expand Down
2 changes: 1 addition & 1 deletion src/vscode-extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"module": "commonjs",
"lib": [
"es2020"
"es2022"
],
"sourceMap": true
},
Expand Down