Files
react.dev/plugins/gatsby-transformer-home-example-code/gatsby-node.js
Vladimir Razuvaev 81bfee964d Fix issues on Windows (#3213)
* Upgrade gatsby-remark-code-repls

* fix path issue on Windows

Gatsby uses `slash` to normalize paths. Because of this `path.join` causes an issue on Windows
2020-08-19 14:12:35 -04:00

39 lines
993 B
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*/
const crypto = require('crypto');
const path = require('path');
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.onCreateNode = async ({actions, node, loadNodeContent}) => {
const {createNode} = actions;
const {absolutePath, ext, name, relativeDirectory, sourceInstanceName} = node;
if (
sourceInstanceName === 'content' &&
relativeDirectory === 'home/examples' &&
ext === '.js'
) {
const code = await loadNodeContent(node);
createNode({
id: name,
children: [],
parent: node.id,
code,
mdAbsolutePath: absolutePath.replace(/\.js$/, '.md'),
internal: {
type: 'ExampleCode',
contentDigest: createContentDigest(JSON.stringify(code)),
},
});
}
};