-
Notifications
You must be signed in to change notification settings - Fork 7
/
on-node-create.js
64 lines (55 loc) · 1.67 KB
/
on-node-create.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
const crypto = require(`crypto`)
const React = require(`react`)
const ReactDOMServer = require(`react-dom/server`)
const NotebookRender = require(`@nteract/notebook-render`).default
module.exports = async function onCreateNode(
{ node, loadNodeContent, boundActionCreators },
pluginOptions // eslint-disable-line no-unused-vars
) {
const { createNode, createParentChildLink } = boundActionCreators
// Filter out non-ipynb content by file extension and checkpoint notebooks
if (
node.extension !== `ipynb` ||
String(node.absolutePath).includes(`.ipynb_checkpoints`)
) {
return
}
// see: http://jupyter.readthedocs.io/en/latest/reference/mimetype.html
// if (node.internal.mediaType !== `application/x-ipynb+json`) {
// return
// }
const content = await loadNodeContent(node)
const jupyterNode = {
id: `${node.id} >>> JupyterNotebook`,
children: [],
parent: node.id,
internal: {
content,
type: `JupyterNotebook`,
},
}
jupyterNode.json = JSON.parse(content)
jupyterNode.metadata = jupyterNode.json.metadata
// render statically html with @nteract/notebook-render element
const reactComponent = React.createElement(
NotebookRender,
{
notebook: jupyterNode.json,
},
null
)
jupyterNode.html = ReactDOMServer.renderToStaticMarkup(reactComponent)
// Add path to the file path
if (node.internal.type === `File`) {
jupyterNode.fileAbsolutePath = node.absolutePath
}
jupyterNode.internal.contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(jupyterNode))
.digest(`hex`)
createNode(jupyterNode)
createParentChildLink({
parent: node,
child: jupyterNode,
})
}