fix: 🐛 Tog bort radbrytning i bold (#66)

This commit is contained in:
Johan Öbrink 2021-02-14 17:05:34 +01:00 committed by GitHub
parent a5dfb704f4
commit ca0117ce64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -34,6 +34,12 @@ describe('parseHtml', () => {
<A href="/foo%20bar">Hello</A>
</DIV>`
expect(clean(html)).toEqual(expected)
})
it('handles breaks in <strong>', () => {
const html = '<strong>Uppdatering 2021-02-08 <br /></strong>'
const expected = '<STRONG>Uppdatering 2021-02-08</STRONG>'
expect(clean(html)).toEqual(expected)
})
})
@ -131,4 +137,4 @@ Stort tack för ert samarbete!`
expect(toMarkdown(html)).toEqual(expected)
})
})
})
})

View File

@ -5,13 +5,16 @@ import {
parse, HTMLElement, TextNode,
} from 'node-html-parser'
const trimNodes = [
const noChildren = [
'strong',
'b',
'em',
'i',
'u',
's',
]
const trimNodes = [
...noChildren,
'h1',
'h2',
'h3',
@ -40,7 +43,11 @@ const deepClean = (node: HTMLElement): HTMLElement => {
const cleaned = new HTMLElement(node.tagName, {}, attributes, node.parentNode)
node.childNodes.forEach((childNode) => {
if (childNode instanceof HTMLElement) {
cleaned.childNodes.push(deepClean(childNode))
if (node.tagName && noChildren.includes(node.tagName.toLowerCase())) {
cleaned.childNodes.push(cleanText(new TextNode(childNode.innerText), node.tagName))
} else {
cleaned.childNodes.push(deepClean(childNode))
}
} else if (childNode instanceof TextNode) {
cleaned.childNodes.push(cleanText(childNode, node.tagName))
}