Skip to content

Commit 4452607

Browse files
committed
Introduce built-in opt-in Mermaid feature (diagrams/charts)
1 parent 69f7c83 commit 4452607

File tree

10 files changed

+1267
-18
lines changed

10 files changed

+1267
-18
lines changed

demo-mermaid/mermaid.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
mermaid:
3+
themeVariables:
4+
lineColor: '#fff'
5+
---
6+
7+
Foo
8+
9+
```mermaid
10+
graph TD
11+
A[Client] --> B[Load Balancer]
12+
B --> C[Server01]
13+
B --> D[Server02]
14+
```
15+
16+
```js
17+
const x = 1;
18+
```
19+
20+
---
21+
22+
Bar
23+
24+
```mermaid
25+
graph TD
26+
A[Client] --> B[LB]
27+
B --> C[Server01]
28+
B --> D[Server02]
29+
```
30+
31+
```js
32+
const x = 2;
33+
```

demo-mermaid/reveal-md.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"mermaid": {
3+
"theme": "base",
4+
"themeVariables": {
5+
"primaryColor": "#0f0"
6+
}
7+
}
8+
}

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
1414

1515
export const defaults = loadJSON(path.join(__dirname, 'defaults.json'));
1616
export const revealBasePath = path.join(require.resolve('reveal.js'), '..', '..');
17+
export const mermaidBasePath = path.join(require.resolve('mermaid'), '..', '..');
1718
export const highlightThemePath = path.resolve(require.resolve('highlight.js'), '..', '..', 'styles');
1819

1920
const localConfig = tryReadJson5Configs(
@@ -66,7 +67,7 @@ export const getFilesGlob = () => mergedConfig.glob;
6667
export const getOptions = () => mergedConfig;
6768

6869
export const getSlideOptions = options => {
69-
return _.defaults({}, cliConfig, options, localConfig, defaults);
70+
return _.defaultsDeep({}, cliConfig, options, localConfig, defaults);
7071
};
7172

7273
export const getRevealOptions = options => {

lib/defaults.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"theme": "black",
1515
"title": "reveal-md",
1616
"verticalSeparator": "\r?\n----\r?\n",
17-
"glob": "**/*.md"
17+
"glob": "**/*.md",
18+
"mermaid": null
1819
}

lib/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const render = async (fullMarkdown, extraOptions = {}) => {
6969
scriptPaths,
7070
cssPaths,
7171
revealOptionsStr: JSON.stringify(revealOptions),
72+
mermaidOptionsStr: JSON.stringify(options.mermaid),
7273
watch: getWatch()
7374
});
7475
const markup = Mustache.render(template, context);

lib/server.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getWatch,
1414
getFaviconPath,
1515
revealBasePath,
16+
mermaidBasePath,
1617
highlightThemePath
1718
} from './config.js';
1819

@@ -32,10 +33,11 @@ export default async () => {
3233
const faviconPath = await getFaviconPath();
3334
app.use(favicon(faviconPath));
3435

35-
['plugin', 'dist'].forEach(dir => {
36-
app.use('/' + dir, staticDir(path.join(revealBasePath, dir)));
37-
console.log(`Serving reveal.js ${dir} from ${revealBasePath}`);
38-
});
36+
app.use('/plugin', staticDir(path.join(revealBasePath, 'plugin')));
37+
app.use('/dist', staticDir(path.join(revealBasePath, 'dist')));
38+
console.log(`Serving reveal.js from ${revealBasePath}`);
39+
40+
app.use('/mermaid', staticDir(path.join(mermaidBasePath)));
3941

4042
app.use('/css/highlight', staticDir(highlightThemePath));
4143

lib/template/reveal.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
@@ -19,9 +19,7 @@
1919

2020
{{#cssPaths}}
2121
<link rel="stylesheet" href="{{{.}}}" />
22-
{{/cssPaths}}
23-
24-
{{#watch}}
22+
{{/cssPaths}} {{#watch}}
2523

2624
<script>
2725
document.write(
@@ -46,6 +44,10 @@
4644

4745
<script src="{{{base}}}/dist/reveal.js"></script>
4846

47+
{{#mermaidOptionsStr}}
48+
<script src="{{{base}}}/mermaid/dist/mermaid.min.js"></script>
49+
{{/mermaidOptionsStr}}
50+
4951
<script src="{{{base}}}/plugin/markdown/markdown.js"></script>
5052
<script src="{{{base}}}/plugin/highlight/highlight.js"></script>
5153
<script src="{{{base}}}/plugin/zoom/zoom.js"></script>
@@ -73,6 +75,9 @@
7375
center: true,
7476
transition: 'default', // none/fade/slide/convex/concave/zoom
7577
slideNumber: true,
78+
highlight: {
79+
highlightOnLoad: false
80+
},
7681
plugins: [
7782
RevealMarkdown,
7883
RevealHighlight,
@@ -94,6 +99,18 @@
9499

95100
<script>
96101
Reveal.initialize(options);
102+
// var mermaidOptions = extend({ startOnLoad: true }, {{{revealOptionsStr}}}, queryOptions);
103+
Reveal.addEventListener('ready', function (event) {
104+
const blocks = Reveal.getRevealElement().querySelectorAll('pre code:not(.mermaid)');
105+
const hlp = Reveal.getPlugin('highlight');
106+
blocks.forEach(hlp.highlightBlock);
107+
});
108+
</script>
109+
110+
{{#mermaidOptionsStr}}
111+
<script>
112+
mermaid.initialize({{{mermaidOptionsStr}}});
97113
</script>
114+
{{/mermaidOptionsStr}}
98115
</body>
99116
</html>

0 commit comments

Comments
 (0)