-
Notifications
You must be signed in to change notification settings - Fork 880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keeping elements with empty content #212
Comments
My full hack which seems to work for now: const turndownService = new TurndownService({
blankReplacement (content, node) {
const types = ['SCRIPT', 'IFRAME']
if (types.indexOf(node.nodeName) !== -1) {
return `\n\n${node.outerHTML}\n\n`
} else {
const output = []
node.childNodes.forEach((child) => {
if (types.indexOf(child.nodeName) !== -1) {
output.push(child.outerHTML)
}
})
if (output.length) {
return '\n\n' + output.join('\n\n') + '\n\n'
} else {
return node.isBlock ? '\n\n' : ''
}
}
}
})
turndownService.keep(['iframe', 'script']) |
Thanks for raising this and for including so much detail. I think might be fixable by modifying the
I suppose a simple fix might be to add those elements to the |
@domchristie - I agree that iframe and script nodes should not be considered as BLANK and isBlank function should be updated to bypass them. |
@domchristie What about put those specials elements in the voidElements list. They act like it. I've tryed to add IFRAME in the In the code below in first case(isBlank) replace the iframe by an empty string and in the second one(voidElements) replace it with 'hey there'
|
True, although because they are not strictly void elements (by definition), then I feel there should be a separate condition. I seem to be able to keep iframes if the function isBlank (node) {
return (
['A', 'TH', 'TD', 'IFRAME'].indexOf(node.nodeName) === -1 &&
/^\s*$/i.test(node.textContent) &&
!isVoid(node) &&
!hasVoid(node)
)
} and iframe are "kept": turndownService.keep('iframe') |
Fixed by #243 |
Hello there, Sorry to be a pain but I think this could be opened up again. |
Using: turndown@4.0.1 via nodejs
Say you have this in your input:
You would expect that
turndownService.keep(['iframe', 'script'])
would keep these tags in the output, but it doesn't, because the nodes have no content in them. We could add some content, but I really don't want to.Ok, so we can use
blankReplacement
to surely fix this:Great, it works! Well, not quite...
Here we have a
<div>
around the iframe, but nothing else in that div, so that iframe is left out of the output. I'm guessing when parsing the outer div, it sees it only has an iframe in it, which is empty, so it counts the div as empty, never giving the iframe a chance to appear.I could continue by changing my
blankReplacement
function, but I feel this is a bug, or at least something which others will want to have as well, so could be added as an option.The text was updated successfully, but these errors were encountered: