From ca0117ce64fbf1ce7eb6f476c37fc769e3df561e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20=C3=96brink?= Date: Sun, 14 Feb 2021 17:05:34 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Tog=20bort=20radbrytning?= =?UTF-8?q?=20i=20bold=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/parseHtml.test.ts | 8 +++++++- lib/parseHtml.ts | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/parseHtml.test.ts b/lib/parseHtml.test.ts index 80b276b16..a233a27b4 100644 --- a/lib/parseHtml.test.ts +++ b/lib/parseHtml.test.ts @@ -34,6 +34,12 @@ describe('parseHtml', () => { Hello ` + expect(clean(html)).toEqual(expected) + }) + it('handles breaks in ', () => { + const html = 'Uppdatering 2021-02-08
' + const expected = 'Uppdatering 2021-02-08' + expect(clean(html)).toEqual(expected) }) }) @@ -131,4 +137,4 @@ Stort tack för ert samarbete!` expect(toMarkdown(html)).toEqual(expected) }) }) -}) \ No newline at end of file +}) diff --git a/lib/parseHtml.ts b/lib/parseHtml.ts index 815505ac1..aa053d4f5 100644 --- a/lib/parseHtml.ts +++ b/lib/parseHtml.ts @@ -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)) }