Files
freeCodeCamp/tools/challenge-parser/parser/plugins/add-solution.js
Shaun Hamilton 59f17f237b refactor: files{} -> challengeFiles[], and key -> fileKey (#43023)
* fix(client): fix client

* fix propType and add comment

* revert user.json prettification

* slight type refactor and payload correction

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* update ChallengeFile type imports

* add cypress test for code-storage

* update test and storage epic

* fix Shaun's tired brain's logic

* refactor with suggestions

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* update codeReset

* increate cypress timeout because firefox is slow

* remove unused import to make linter happy

* use focus on editor

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* use more specific seletor for cypress editor test

* account for silly null challengeFiles

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2021-08-12 20:48:28 +02:00

44 lines
1.2 KiB
JavaScript

const { root } = require('mdast-builder');
const visitChildren = require('unist-util-visit-children');
const { editableRegionMarker } = require('./add-seed');
const getAllBetween = require('./utils/between-headings');
const { getFileVisitor } = require('./utils/get-file-visitor');
const { splitOnThematicBreak } = require('./utils/split-on-thematic-break');
function validateMarkers({ value }) {
const lines = value.split('\n');
if (lines.some(line => line.match(RegExp(editableRegionMarker))))
throw Error(
'--fcc-editable-region-- should only appear in the --seed-contents--\n' +
'section, not in --solutions--'
);
}
function createPlugin() {
return function transformer(tree, file) {
const solutionArrays = splitOnThematicBreak(
getAllBetween(tree, `--solutions--`)
);
const solutions = [];
solutionArrays.forEach(nodes => {
const solution = {};
const solutionTree = root(nodes);
const visitForContents = visitChildren(
getFileVisitor(solution, 'contents', validateMarkers)
);
visitForContents(solutionTree);
solutions.push(Object.values(solution));
});
file.data = {
...file.data,
solutions: solutions
};
};
}
module.exports = createPlugin;