Files
freecodecamp--freecodecamp/tools/challenge-parser/parser/plugins/add-tests.js
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

35 lines
1017 B
JavaScript

const chunk = require('lodash/chunk');
const { getSection } = require('./utils/get-section');
const mdastToHtml = require('./utils/mdast-to-html');
function plugin() {
return transformer;
function transformer(tree, file) {
const hintNodes = getSection(tree, '--hints--');
if (hintNodes.length % 2 !== 0)
throw Error(
'Hints must be in pairs: each hint text followed by a test code block'
);
const tests = chunk(hintNodes, 2).map(getTest);
file.data.tests = tests;
}
}
function getTest(hintNodes) {
const [textNode, testStringNode] = hintNodes;
const text = mdastToHtml([textNode]);
const testString = testStringNode.value;
if (!text) throw Error('text is missing from hint');
// stub tests (i.e. text, but no testString) are allowed, but the md must
// have a code block, even if it is empty.
if (!testString && testString !== '')
throw Error('testString (code block) is missing from hint');
return { text, testString };
}
module.exports = plugin;