diff --git a/tools/challenge-md-parser/challengeSeed-to-data.js b/tools/challenge-md-parser/challengeSeed-to-data.js index e56927ace8..1d69816256 100644 --- a/tools/challenge-md-parser/challengeSeed-to-data.js +++ b/tools/challenge-md-parser/challengeSeed-to-data.js @@ -20,8 +20,6 @@ function defaultFile(lang) { }; } function createCodeGetter(key, regEx, seeds) { - console.log('seeds'); - console.log(seeds); return container => { const { properties: { id } @@ -122,4 +120,5 @@ function createPlugin() { }; } -module.exports = createPlugin; +exports.challengeSeedToData = createPlugin; +exports.createCodeGetter = createCodeGetter; diff --git a/tools/challenge-md-parser/challengeSeed-to-data.test.js b/tools/challenge-md-parser/challengeSeed-to-data.test.js index 489886e04b..5f2866d8b1 100644 --- a/tools/challenge-md-parser/challengeSeed-to-data.test.js +++ b/tools/challenge-md-parser/challengeSeed-to-data.test.js @@ -1,8 +1,9 @@ /* global describe it expect beforeEach */ -const mockAST = require('./fixtures/challenge-html-ast.json'); -const challengeSeedToData = require('./challengeSeed-to-data'); const isArray = require('lodash/isArray'); +const mockAST = require('./fixtures/challenge-html-ast.json'); +const { challengeSeedToData } = require('./challengeSeed-to-data'); + describe('challengeSeed-to-data plugin', () => { const plugin = challengeSeedToData(); let file = { data: {} }; diff --git a/tools/challenge-md-parser/index.js b/tools/challenge-md-parser/index.js index 5b601b1172..83b6d0bfb1 100644 --- a/tools/challenge-md-parser/index.js +++ b/tools/challenge-md-parser/index.js @@ -9,7 +9,7 @@ const raw = require('rehype-raw'); const frontmatterToData = require('./frontmatter-to-data'); const textToData = require('./text-to-data'); const testsToData = require('./tests-to-data'); -const challengeSeedToData = require('./challengeSeed-to-data'); +const { challengeSeedToData } = require('./challengeSeed-to-data'); const solutionsToData = require('./solution-to-data'); const processor = unified() diff --git a/tools/challenge-md-parser/solution-to-data.js b/tools/challenge-md-parser/solution-to-data.js index 66a742b17b..5740a4a4b1 100644 --- a/tools/challenge-md-parser/solution-to-data.js +++ b/tools/challenge-md-parser/solution-to-data.js @@ -2,11 +2,15 @@ const visit = require('unist-util-visit'); const { selectAll } = require('hast-util-select'); const { sectionFilter } = require('./utils'); +const { createCodeGetter } = require('./challengeSeed-to-data'); + +const solutionRE = /(.+)-solution$/; function createPlugin() { return function transformer(tree, file) { function visitor(node) { if (sectionFilter(node, 'solution')) { + // fallback for single-file challenges const solutions = selectAll('code', node).map( element => element.children[0].value ); @@ -14,6 +18,21 @@ function createPlugin() { ...file.data, solutions }; + const solutionFiles = {}; + const codeDivs = selectAll('div', node); + const solutionContainers = codeDivs.filter(({ properties: { id } }) => + solutionRE.test(id) + ); + solutionContainers.forEach( + createCodeGetter('contents', solutionRE, solutionFiles) + ); + + file.data = { + ...file.data, + solutionFiles: Object.keys(solutionFiles).map( + lang => solutionFiles[lang] + ) + }; } } visit(tree, 'element', visitor);