fix(client): formatting challenge descriptions and instructions
This commit is contained in:
committed by
mrugesh mohapatra
parent
0e3d9a7465
commit
90a744e4f4
@ -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
|
||||
|
Reference in New Issue
Block a user