forked from markmap/gatsby-remark-markmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.js
32 lines (30 loc) · 839 Bytes
/
gatsby-ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const React = require('react');
const { Transformer } = require('markmap-lib');
const { escapeScript, buildCode } = require('markmap-common');
const transformer = new Transformer();
const assets = transformer.getAssets();
const { scripts, styles } = assets;
const assetsText = JSON.stringify({
scripts: scripts?.map(item => {
if (item.type === 'script') return item;
const { fn, getParams } = item.data;
const code = escapeScript(buildCode(fn, getParams?.({
getMarkmap: () => window.markmap,
}) || []));
return {
type: 'script',
data: {
textContent: code,
},
};
}),
styles,
});
exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
setHeadComponents([
React.createElement('meta', {
name: 'markmap:assets',
content: assetsText,
}),
]);
};