fix(client): formatting challenge descriptions and instructions
This commit is contained in:
committed by
mrugesh mohapatra
parent
0e3d9a7465
commit
90a744e4f4
@ -5,12 +5,12 @@ Object {
|
|||||||
"description": "<section id=\\"description\\">
|
"description": "<section id=\\"description\\">
|
||||||
<p>Welcome to freeCodeCamp's HTML coding challenges. These will walk you through web development step-by-step.</p>
|
<p>Welcome to freeCodeCamp's HTML coding challenges. These will walk you through web development step-by-step.</p>
|
||||||
<p>Lorem Ipsum with <code>some code</code></p>
|
<p>Lorem Ipsum with <code>some code</code></p>
|
||||||
<blockquote>
|
<p><blockquote>
|
||||||
<p>Some text in a blockquote</p>
|
<p>Some text in a blockquote</p>
|
||||||
<p>Some text in a blockquote, with <code>code</code></p>
|
<p>Some text in a blockquote, with <code>code</code></p>
|
||||||
</blockquote>
|
</blockquote></p>
|
||||||
<pre><code class=\\"language-html\\"><p>We aim to preserve this</p>
|
<p><pre><code class=\\"language-html\\"><p>We aim to preserve this</p>
|
||||||
</code></pre>
|
</code></pre></p>
|
||||||
</section>",
|
</section>",
|
||||||
"instructions": "<section id=\\"instructions\\">
|
"instructions": "<section id=\\"instructions\\">
|
||||||
<p>To pass the test on this challenge, change your <code>h1</code> element's text to say \\"Hello World\\".</p>
|
<p>To pass the test on this challenge, change your <code>h1</code> element's text to say \\"Hello World\\".</p>
|
||||||
|
@ -3,6 +3,21 @@ const toHTML = require('hast-util-to-html');
|
|||||||
|
|
||||||
const { sectionFilter } = require('./utils');
|
const { sectionFilter } = require('./utils');
|
||||||
|
|
||||||
|
function createPNode() {
|
||||||
|
return {
|
||||||
|
type: 'element',
|
||||||
|
tagName: 'p',
|
||||||
|
children: [],
|
||||||
|
properties: {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
function createBlankLineNode() {
|
||||||
|
return {
|
||||||
|
type: 'text',
|
||||||
|
value: '\n'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function textToData(sectionIds) {
|
function textToData(sectionIds) {
|
||||||
if (!sectionIds || !Array.isArray(sectionIds) || sectionIds.length <= 0) {
|
if (!sectionIds || !Array.isArray(sectionIds) || sectionIds.length <= 0) {
|
||||||
throw new Error("textToData must have an array of section id's supplied");
|
throw new Error("textToData must have an array of section id's supplied");
|
||||||
@ -12,7 +27,42 @@ function textToData(sectionIds) {
|
|||||||
function visitor(node) {
|
function visitor(node) {
|
||||||
sectionIds.forEach(sectionId => {
|
sectionIds.forEach(sectionId => {
|
||||||
if (sectionFilter(node, 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 = {
|
||||||
...file.data,
|
...file.data,
|
||||||
[sectionId]: textArray
|
[sectionId]: textArray
|
||||||
|
Reference in New Issue
Block a user