Dustin Schau
6 years ago
3 changed files with 43 additions and 27 deletions
@ -1,28 +1,29 @@ |
|||
const {readdirSync, readFileSync} = require('fs'); |
|||
const {join, resolve} = require('path'); |
|||
const crypto = require(`crypto`) |
|||
|
|||
// docblock goes here
|
|||
const createContentDigest = obj => crypto |
|||
.createHash(`md5`) |
|||
.update(obj) |
|||
.digest(`hex`) |
|||
|
|||
// Store code snippets in GraphQL for the home page examples.
|
|||
// Snippets will be matched with markdown templates of the same name.
|
|||
exports.sourceNodes = ({graphql, actions}) => { |
|||
exports.onCreateNode = async ({node, loadNodeContent, actions}) => { |
|||
const {createNode} = actions; |
|||
const {absolutePath, ext, name, relativeDirectory, sourceInstanceName} = node |
|||
|
|||
const path = resolve(__dirname, '../../content/home/examples'); |
|||
const files = readdirSync(path); |
|||
|
|||
files.forEach(file => { |
|||
if (file.match(/\.js$/)) { |
|||
const code = readFileSync(join(path, file), 'utf8'); |
|||
const id = file.replace(/\.js$/, ''); |
|||
|
|||
createNode({ |
|||
id, |
|||
children: [], |
|||
parent: 'EXAMPLES', |
|||
internal: { |
|||
type: 'ExampleCode', |
|||
contentDigest: JSON.stringify(code), |
|||
}, |
|||
}); |
|||
} |
|||
}); |
|||
if (sourceInstanceName === 'content' && relativeDirectory === 'home/examples' && ext === '.js') { |
|||
const code = await loadNodeContent(node); |
|||
createNode({ |
|||
id: name, |
|||
children: [], |
|||
parent: 'EXAMPLES', |
|||
code, |
|||
mdAbsolutePath: absolutePath.replace(/\.js$/, '.md'), |
|||
internal: { |
|||
type: 'ExampleCode', |
|||
contentDigest: createContentDigest(JSON.stringify(code)), |
|||
}, |
|||
}); |
|||
} |
|||
}; |
|||
|
Loading…
Reference in new issue