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

Mermaid Support #165

Merged
merged 8 commits into from
Aug 27, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ issue](https://github.com/jannis-baum/vivify/issues/new/choose) or
[extended](https://www.markdownguide.org/extended-syntax/) syntax support
- [KaTeX math](https://katex.org)
- [graphviz/dot graphs](https://graphviz.org/doc/info/lang.html)
- [Mermaid diagrams & charts](https://mermaid.js.org)
- [GitHub alert
blocks](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)
- links to other files: [relative links like in
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"markdown-it-table-of-contents": "^0.6.0",
"markdown-it-task-lists": "^2.1.1",
"markdown-it-texmath": "^1.0.0",
"mermaid": "^11.0.2",
"node-stream-zip": "^1.15.0",
"open": "^10.1.0",
"tmp": "^0.2.3",
Expand Down
2 changes: 2 additions & 0 deletions src/parser/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import MarkdownIt from 'markdown-it';
import anchor from 'markdown-it-anchor';
import highlight from './highlight.js';
import graphviz from './dot.js';
import mermaid from './mermaid.js';
import githubAlerts from 'markdown-it-github-alerts';
import config from '../config.js';
import { Renderer } from './parser.js';
Expand Down Expand Up @@ -66,6 +67,7 @@ mdit.use(anchor, {
});
mdit.use(graphviz);
mdit.use(githubAlerts);
mdit.use(mermaid);

const renderMarkdown: Renderer = (content: string) => {
return mdit.render(content);
Expand Down
12 changes: 12 additions & 0 deletions src/parser/mermaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import MarkdownIt from 'markdown-it';

export default function mermaid(md: MarkdownIt) {
const defaultRender = md.renderer.rules.fence!;
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
const token = tokens[idx];
if (token.info === 'mermaid') {
return `<pre class="mermaid">${token.content}</pre>`;
}
return defaultRender(tokens, idx, options, env, self);
};
}
1 change: 1 addition & 0 deletions src/routes/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class StaticProvider {
static mime(path: string): string {
if (path.endsWith('.css')) return 'text/css';
if (path.endsWith('.js')) return 'text/javascript';
if (path.endsWith('.mjs')) return 'text/javascript';
if (path.endsWith('.ttf')) return 'font/ttf';
if (path.endsWith('.woff2')) return 'font/woff2';
throw new Error('Unknown MIME type');
Expand Down
16 changes: 16 additions & 0 deletions src/routes/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,23 @@ router.get(/.*/, async (req: Request, res: Response) => {
window.VIV_PORT = "${config.port}";
window.VIV_PATH = "${urlToPath(req.path)}";
</script>

${config.scripts ? `<script type="text/javascript">${config.scripts}</script>` : ''}

<script type="module">
import mermaid from '/static/mermaid/mermaid.esm.min.mjs';
const darkModePreference = window.matchMedia("(prefers-color-scheme: dark)");
mermaid.initialize({ startOnLoad: true, theme: darkModePreference.matches ? 'dark' : 'default' })

function updateTheme() {
if (document.getElementsByClassName('mermaid').length > 0) {
window.location.reload()
}
}
darkModePreference.addEventListener("change", () => updateTheme());
// deprecated method for backward compatibility
darkModePreference.addEventListener(() => updateTheme());
</script>
<script type="text/javascript" src="/static/client.js"></script>
</html>
`);
Expand Down
1 change: 1 addition & 0 deletions static/katex
Binary file removed static/katex/fonts/KaTeX_AMS-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_AMS-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_AMS-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Caligraphic-Bold.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Caligraphic-Bold.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Caligraphic-Bold.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Caligraphic-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Caligraphic-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Caligraphic-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Fraktur-Bold.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Fraktur-Bold.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Fraktur-Bold.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Fraktur-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Fraktur-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Fraktur-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Bold.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Bold.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Bold.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-BoldItalic.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-BoldItalic.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-BoldItalic.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Italic.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Italic.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Italic.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Main-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Math-BoldItalic.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Math-BoldItalic.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Math-BoldItalic.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Math-Italic.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Math-Italic.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Math-Italic.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Bold.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Bold.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Bold.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Italic.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Italic.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Italic.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_SansSerif-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Script-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Script-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Script-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size1-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size1-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size1-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size2-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size2-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size2-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size3-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size3-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size3-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size4-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size4-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Size4-Regular.woff2
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Typewriter-Regular.ttf
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Typewriter-Regular.woff
Binary file not shown.
Binary file removed static/katex/fonts/KaTeX_Typewriter-Regular.woff2
Binary file not shown.
2 changes: 0 additions & 2 deletions static/katex/katex.css

This file was deleted.

1 change: 1 addition & 0 deletions static/mermaid
11 changes: 11 additions & 0 deletions tests/rendering/markdown-additional.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ digraph {
}
```

## Mermaid

```mermaid
flowchart LR

A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Two| E[Result 2]
```

## \<kbd> tag

While not a markdown syntax, this has a default style:
Expand Down
Loading