Skip to content

Commit

Permalink
fix: 🐛 Tog bort radbrytning i bold (kolplattformen#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanObrink authored Feb 14, 2021
1 parent a5dfb70 commit ca0117c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/parseHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down Expand Up @@ -131,4 +137,4 @@ Stort tack för ert samarbete!`
expect(toMarkdown(html)).toEqual(expected)
})
})
})
})
11 changes: 9 additions & 2 deletions lib/parseHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit ca0117c

Please sign in to comment.