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