From 90a744e4f4afe4bf1d634e9c5b99c2a42b1ca00b Mon Sep 17 00:00:00 2001 From: Valeriy S Date: Thu, 27 Dec 2018 13:47:46 +0300 Subject: [PATCH] fix(client): formatting challenge descriptions and instructions --- .../__snapshots__/text-to-data.test.js.snap | 8 +-- tools/challenge-md-parser/text-to-data.js | 52 ++++++++++++++++++- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/tools/challenge-md-parser/__snapshots__/text-to-data.test.js.snap b/tools/challenge-md-parser/__snapshots__/text-to-data.test.js.snap index 6eae0c9290..98b906904f 100644 --- a/tools/challenge-md-parser/__snapshots__/text-to-data.test.js.snap +++ b/tools/challenge-md-parser/__snapshots__/text-to-data.test.js.snap @@ -5,12 +5,12 @@ Object { "description": "

Welcome to freeCodeCamp's HTML coding challenges. These will walk you through web development step-by-step.

Lorem Ipsum with some code

-
+

Some text in a blockquote

Some text in a blockquote, with code

-
-
<p>We aim to preserve this</p>
-
+

+

<p>We aim to preserve this</p>
+

", "instructions": "

To pass the test on this challenge, change your h1 element's text to say \\"Hello World\\".

diff --git a/tools/challenge-md-parser/text-to-data.js b/tools/challenge-md-parser/text-to-data.js index 9e186b20d0..638b5835a7 100644 --- a/tools/challenge-md-parser/text-to-data.js +++ b/tools/challenge-md-parser/text-to-data.js @@ -3,6 +3,21 @@ const toHTML = require('hast-util-to-html'); const { sectionFilter } = require('./utils'); +function createPNode() { + return { + type: 'element', + tagName: 'p', + children: [], + properties: {} + }; +} +function createBlankLineNode() { + return { + type: 'text', + value: '\n' + }; +} + function textToData(sectionIds) { if (!sectionIds || !Array.isArray(sectionIds) || sectionIds.length <= 0) { throw new Error("textToData must have an array of section id's supplied"); @@ -12,7 +27,42 @@ function textToData(sectionIds) { function visitor(node) { sectionIds.forEach(sectionId => { if (sectionFilter(node, sectionId)) { - const textArray = toHTML(node); + const newChildren = []; + let currentParagraph = null; + node.children.forEach(child => { + if (child.type !== 'text') { + if (child.type === 'element' && child.tagName === 'p') { + newChildren.push(child); + currentParagraph = null; + } else { + if (!currentParagraph) { + currentParagraph = createPNode(); + newChildren.push(currentParagraph); + } + currentParagraph.children.push(child); + } + } else { + const lines = child.value.split('\n'); + if (lines.filter(Boolean).length > 0) { + lines.forEach((line, index) => { + if (/^\s*$/.test(line)) { + currentParagraph = null; + } else { + if (!currentParagraph || index > 0) { + newChildren.push(createBlankLineNode()); + currentParagraph = createPNode(); + newChildren.push(currentParagraph); + } + currentParagraph.children.push({ ...child, value: line }); + } + }); + } else { + currentParagraph = null; + newChildren.push(createBlankLineNode()); + } + } + }); + const textArray = toHTML({ ...node, children: newChildren }); file.data = { ...file.data, [sectionId]: textArray