feat: parse solution as siblings (#301)

this creates the ability for explanations to work if they are:

Not a child of a response type
Use an h2 tag for the explanation title
do not use the "Explanation" title.
This commit is contained in:
connorhaugh
2023-04-10 15:37:02 -04:00
committed by GitHub
parent 8a7dbdf4be
commit d58f349e1f

View File

@@ -384,18 +384,22 @@ export class OLXParser {
}
getSolutionExplanation(problemType) {
if (!_.has(this.problem, `${problemType}.solution`)) { return null; }
let solution = _.get(this.problem, `${problemType}.solution`);
if (!_.has(this.problem, `${problemType}.solution`) && !_.has(this.problem, 'solution')) { return null; }
let solution = _.get(this.problem, `${problemType}.solution`, null) || _.get(this.problem, 'solution', null);
const wrapper = Object.keys(solution)[0];
if (Object.keys(solution).length === 1 && wrapper === 'div') {
const parsedSolution = {};
Object.entries(solution.div).forEach(([key, value]) => {
if (key !== '@_class') {
if (key === 'p') {
value.shift();
if (key.indexOf('@_' === -1)) {
// The redundant "explanation" title should be removed.
if ((key === 'p' || key === 'h2') && (value['#text'] === 'Explanation' || value[0]['#text'] === 'Explanation')) {
if (_.isArray(value)) {
value.shift();
parsedSolution[key] = value;
}
} else {
parsedSolution[key] = value;
}
parsedSolution[key] = value;
}
});
solution = parsedSolution;