Files
freeCodeCamp/tools/client-plugins/gatsby-source-challenges/create-challenge-nodes.js
2024-10-28 15:52:40 -05:00

64 lines
1.4 KiB
JavaScript

const crypto = require('crypto');
const createdIds = new Set();
function createChallengeNode(
challenge,
reporter,
{ isReloading } = { isReloading: false }
) {
const contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(challenge))
.digest('hex');
const internal = {
contentDigest,
type: challenge.challengeType === 7 ? 'CertificateNode' : 'ChallengeNode'
};
if (internal.type === 'ChallengeNode') {
const { tests = [], block, dashedName, superBlock } = challenge;
const slug = `/learn/${superBlock}/${block}/${dashedName}`;
const blockHashSlug = `/learn/${superBlock}/#${block}`;
challenge.fields = {
slug,
blockName: block,
tests,
blockHashSlug
};
}
// Challenge id should be unique for CertificateNodes, but not for
// ChallengeNodes
const id =
internal.type === 'ChallengeNode' ? challenge.fields.slug : challenge.id;
if (createdIds.has(id) && !isReloading) {
throw Error(`
Challenge slugs must be unique, but ${id} already exists.
`);
}
createdIds.add(id);
return JSON.parse(
JSON.stringify(
Object.assign(
{},
{
children: [],
parent: null,
internal,
sourceInstanceName: 'challenge'
},
{ challenge },
{
id
}
)
)
);
}
exports.createChallengeNode = createChallengeNode;