Last active
May 24, 2017 10:46
-
-
Save craigmulligan/0bf942bfe14bf2a3aacce909cb05d594 to your computer and use it in GitHub Desktop.
gatsby plugin to extract useful readme info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require(`crypto`) | |
const readmeParser = require('readme-parser') | |
module.exports = async function onNodeCreate({ | |
node, | |
getNode, | |
loadNodeContent, | |
boundActionCreators, | |
}) { | |
const { createNode, updateNode } = boundActionCreators | |
// We are only concerned with readmes | |
if (node.name !== `README`) { | |
return | |
} | |
// We only care about markdown content | |
if (node.internal.mediaType !== `text/x-markdown`) { | |
return | |
} | |
const content = await loadNodeContent(node) | |
const data = readmeParser(content) | |
const objStr = JSON.stringify(data) | |
const contentDigest = crypto | |
.createHash(`md5`) | |
.update(objStr) | |
.digest(`hex`) | |
const readmeNode = { | |
...data, | |
id: `${node.id} >>> readme`, | |
children: [], | |
parent: node.id, | |
internal: { | |
contentDigest, | |
type: `File`, | |
mediaType: `application/json`, | |
content: objStr, | |
}, | |
} | |
// Add path to the markdown file path | |
if (node.internal.type === `File`) { | |
readmeNode.fileAbsolutePath = node.absolutePath | |
} | |
node.children = node.children.concat([readmeNode.id]) | |
updateNode(node) | |
createNode(readmeNode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment