forked from daattali/beautiful-jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
72 lines (66 loc) · 1.76 KB
/
gatsby-browser.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import './style.css';
function loadScript(url) {
return new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = url;
s.onload = resolve;
s.onerror = reject;
document.body.appendChild(s);
});
}
plugins: [
{
resolve: 'gatsby-transformer-remark',
options: {
plugins: [
// Note that `gatsby-remark-markmap` must be put before other plugins
// that handle code blocks, e.g. `gatsby-remark-prismjs`
'gatsby-remark-markmap',
'gatsby-remark-prismjs',
],
},
},
const defaultOptions = {
loadDeps() {
return loadScript('https://cdn.jsdelivr.net/npm/d3@5');
},
plugins: [],
markmap: {},
};
let loading;
function initialize(options) {
if (!loading) {
loading = (async () => {
options = {
...defaultOptions,
...options,
};
const { loadDeps, plugins } = options;
if (typeof loadDeps === 'function') {
await loadDeps();
}
const { markmap, loadPlugins } = require('markmap-lib/dist/view');
if (plugins?.length) loadPlugins(plugins);
return { markmap, options };
})();
}
return loading;
}
export function onRouteUpdate(context, pluginOptions) {
const markmaps = Array.from(document.querySelectorAll('.gatsby-markmap'));
if (markmaps.length) {
initialize(pluginOptions)
.then(({ markmap, options }) => {
markmaps.forEach(wrapper => {
const svg = d3.select(wrapper).append('svg');
try {
const data = JSON.parse(wrapper.dataset.markmap);
markmap(svg, data, options.markmap);
} catch (err) {
console.error(`Error loading markmap ${svg && svg.id}!`);
console.error(err);
}
});
});
}
}