Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/native-mermaid-defaults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@streamdown/mermaid": patch
---

Add an option to use Mermaid's native theme and font defaults.
12 changes: 11 additions & 1 deletion apps/website/content/docs/plugins/mermaid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,13 @@ gitGraph
The Mermaid plugin uses these defaults:

- `startOnLoad: false` - Diagrams render on demand
- `theme: "default"` - Mermaid's default theme
- `theme: "default"` - Default plugin theme
- `securityLevel: "strict"` - Secure rendering
- `fontFamily: "monospace"` - Consistent code-like typography
- `suppressErrorRendering: true` - Prevents partial renders on errors

By default, the plugin provides its own theme and font settings for backward compatibility. To use Mermaid's native theme and font defaults instead, enable the `useMermaidDefaults` option when creating the plugin.

### Theme Customization

Customize the Mermaid theme using the `mermaid.config` prop:
Expand Down Expand Up @@ -373,14 +375,22 @@ For advanced configuration, use `createMermaidPlugin`:
```tsx
import { createMermaidPlugin } from '@streamdown/mermaid';

// Override the theme and font explicitly
const mermaid = createMermaidPlugin({
config: {
theme: 'dark',
fontFamily: 'monospace',
},
});

// Use Mermaid's native theme and font defaults
const nativeMermaid = createMermaidPlugin({
useMermaidDefaults: true,
});
```

When `useMermaidDefaults` is enabled, the plugin does not specify `theme` or `fontFamily`. You can still provide either property through `config` or the configuration passed to `getMermaid()`.

## Error Handling

### Custom Error Component
Expand Down
100 changes: 100 additions & 0 deletions packages/streamdown-mermaid/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mermaidLib from "mermaid";
import { describe, expect, it, vi } from "vitest";
import { createMermaidPlugin, mermaid } from "../index";

Expand Down Expand Up @@ -141,3 +142,102 @@ describe("MermaidInstance", () => {
consoleSpy.mockRestore();
});
});

describe("Mermaid native defaults", () => {
it("should preserve existing defaults by default", () => {
const initializeSpy = vi.spyOn(mermaidLib, "initialize");

try {
const plugin = createMermaidPlugin();
plugin.getMermaid().initialize({});

expect(initializeSpy).toHaveBeenCalledWith(
expect.objectContaining({
theme: "default",
fontFamily: "monospace",
startOnLoad: false,
securityLevel: "strict",
suppressErrorRendering: true,
})
);
} finally {
initializeSpy.mockRestore();
}
});

it("should omit theme and font when native defaults are enabled", () => {
const initializeSpy = vi.spyOn(mermaidLib, "initialize");

try {
const plugin = createMermaidPlugin({
useMermaidDefaults: true,
});

plugin.getMermaid().initialize({});

const config = initializeSpy.mock.calls.at(-1)?.[0];

expect(config).not.toHaveProperty("theme");
expect(config).not.toHaveProperty("fontFamily");
expect(config).toMatchObject({
startOnLoad: false,
securityLevel: "strict",
suppressErrorRendering: true,
});
} finally {
initializeSpy.mockRestore();
}
});

it("should allow explicit theme and font overrides in native mode", () => {
const initializeSpy = vi.spyOn(mermaidLib, "initialize");

try {
const plugin = createMermaidPlugin({
useMermaidDefaults: true,
config: {
theme: "dark",
fontFamily: "Inter",
},
});

plugin.getMermaid().initialize({});

expect(initializeSpy).toHaveBeenCalledWith(
expect.objectContaining({
theme: "dark",
fontFamily: "Inter",
})
);
} finally {
initializeSpy.mockRestore();
}
});

it("should allow getMermaid config to override native defaults", () => {
const initializeSpy = vi.spyOn(mermaidLib, "initialize");

try {
const plugin = createMermaidPlugin({
useMermaidDefaults: true,
});

plugin.getMermaid({
theme: "dark",
fontFamily: "Inter",
});

expect(initializeSpy).toHaveBeenCalledWith(
expect.objectContaining({
theme: "dark",
fontFamily: "Inter",
startOnLoad: false,
securityLevel: "strict",
suppressErrorRendering: true,
})
);
} finally {
initializeSpy.mockRestore();
}
});
});
24 changes: 16 additions & 8 deletions packages/streamdown-mermaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,30 @@ export interface MermaidPluginOptions {
* Default Mermaid configuration
*/
config?: MermaidConfig | undefined;
}

const defaultConfig: MermaidConfig = {
startOnLoad: false,
theme: "default",
securityLevel: "strict",
fontFamily: "monospace",
suppressErrorRendering: true,
};
/**
* Use Mermaid's native theme and font defaults
*/
useMermaidDefaults?: boolean | undefined;
}

/**
* Create a Mermaid plugin with optional configuration
*/
export function createMermaidPlugin(
options: MermaidPluginOptions = {}
): DiagramPlugin {
const defaultConfig: MermaidConfig = {
startOnLoad: false,
securityLevel: "strict",
suppressErrorRendering: true,
...(options.useMermaidDefaults
? {}
: {
theme: "default",
fontFamily: "monospace",
}),
};
let initialized = false;
let currentConfig: MermaidConfig = { ...defaultConfig, ...options.config };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import { createMermaidPlugin } from "../dist/index";

interface MermaidOptions {
config?: MermaidConfig;
useMermaidDefaults?: boolean;
}

// Forwarding possibly-`undefined` optional props must type-check.
export const createPlugin = (options: MermaidOptions) =>
createMermaidPlugin({ config: options.config });
createMermaidPlugin({
config: options.config,
useMermaidDefaults: options.useMermaidDefaults,
});

export const plugin = createMermaidPlugin({ config: undefined });
export const plugin = createMermaidPlugin({
config: undefined,
useMermaidDefaults: undefined,
});
Loading