Skip to content

Commit

Permalink
cheerio integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
knyga committed May 21, 2019
1 parent 2d06e0d commit 0a99144
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/convert/notagHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
context.commands.push({name: 'print', data: node.data});
return context;
} else if(!isHasNestedTagsPresent) {
// context.commands.push({name: 'println', data: node.data});
context.commands.push({name: 'println', data: node.data});
return context;
}
return null;
Expand Down
40 changes: 38 additions & 2 deletions src/convert/tdTagHandler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
const checkIsStyleBold = require('../utils/checkIsStyleBold');

const collectInnerTags = (node) => {
const stack = [node];
const tags = {};
while(stack.length) {
const n = stack.pop();
if(n.type === 'tag') {
tags[n.name] = true;
if(n.children) {
for(let i=0; i<n.children.length; i+=1) {
stack.push(n.children[i]);
}
}
}
}

return Object.keys(tags);
};

const getData = (node) => {
const stack = [node];
const data = [];
while(stack.length) {
const n = stack.pop();
if(n.data) {
data.push(n.data);
}
if(n.children) {
for(let i=0; i<n.children.length; i+=1) {
stack.push(n.children[i]);
}
}
}

return data.join('');
};

const tdTagHandler = {
isIgnoreOtherHandlers: true,
checkIsAllowed: (context, {tag}) => tag === 'td',
checkIsBold: (node) => {
if(/<b>.+<\/b>/.test(node.toString()) || checkIsStyleBold(node)) {
if(collectInnerTags(node).includes('b') || checkIsStyleBold(node)) {
return true;
}
return false;
Expand All @@ -15,7 +51,7 @@ const tdTagHandler = {
attrs.bold = true;
delete attrs.style;
}
context.data.push({...attrs, text: node.children[0].data});
context.data.push({...attrs, text: getData(node)});
return context;
},
sanitizeHtml: {
Expand Down

0 comments on commit 0a99144

Please sign in to comment.