diff --git a/tools/challenge-parser/parser/plugins/add-video-question.js b/tools/challenge-parser/parser/plugins/add-video-question.js index edf971bb544..eab6406c5a6 100644 --- a/tools/challenge-parser/parser/plugins/add-video-question.js +++ b/tools/challenge-parser/parser/plugins/add-video-question.js @@ -3,6 +3,7 @@ const find = require('unist-util-find'); const { getSection } = require('./utils/get-section'); const getAllBefore = require('./utils/before-heading'); const mdastToHtml = require('./utils/mdast-to-html'); +const { getParagraphContent } = require('./utils/get-paragraph-content'); const { splitOnThematicBreak } = require('./utils/split-on-thematic-break'); @@ -80,13 +81,7 @@ function getAnswers(answersNodes) { function getSolution(solutionNodes) { let solution; try { - if (solutionNodes.length > 1) throw Error('Too many nodes'); - if (solutionNodes[0].children.length > 1) - throw Error('Too many child nodes'); - const solutionString = solutionNodes[0].children[0].value; - if (solutionString === '') throw Error('Non-empty string required'); - - solution = Number(solutionString); + solution = Number(getParagraphContent(solutionNodes[0])); if (Number.isNaN(solution)) throw Error('Not a number'); if (solution < 1) throw Error('Not positive number'); } catch (e) { diff --git a/tools/challenge-parser/parser/plugins/utils/get-paragraph-content.js b/tools/challenge-parser/parser/plugins/utils/get-paragraph-content.js new file mode 100644 index 00000000000..1c7b8ab3d15 --- /dev/null +++ b/tools/challenge-parser/parser/plugins/utils/get-paragraph-content.js @@ -0,0 +1,7 @@ +const mdastToHtml = require('./mdast-to-html'); + +function getParagraphContent(node) { + return node.type === 'paragraph' ? mdastToHtml(node.children) : null; +} + +module.exports.getParagraphContent = getParagraphContent; diff --git a/tools/challenge-parser/parser/plugins/utils/get-paragraph-content.test.js b/tools/challenge-parser/parser/plugins/utils/get-paragraph-content.test.js new file mode 100644 index 00000000000..059c43fe64e --- /dev/null +++ b/tools/challenge-parser/parser/plugins/utils/get-paragraph-content.test.js @@ -0,0 +1,22 @@ +const parseFixture = require('../../__fixtures__/parse-fixture'); +const { getParagraphContent } = require('./get-paragraph-content'); + +describe('getParagraphContent', () => { + let simpleAST; + + beforeAll(async () => { + simpleAST = await parseFixture('simple.md'); + }); + + it('should return the content of a paragraph node', () => { + const paragraphNode = simpleAST.children[1]; + expect(paragraphNode.type).toBe('paragraph'); + expect(getParagraphContent(paragraphNode)).toEqual('Paragraph 1'); + }); + + it('should return null if the node is not a paragraph', () => { + const headingNode = simpleAST.children[0]; + expect(headingNode.type).toBe('heading'); + expect(getParagraphContent(headingNode)).toBeNull(); + }); +});