From 82fa2dcc844a019e71284ddae4d7cf67a2804e37 Mon Sep 17 00:00:00 2001 From: Kajetan Kazimierczak Date: Fri, 30 Apr 2021 10:37:44 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20F=C3=B6rb=C3=A4ttrad=20pa?= =?UTF-8?q?rsning=20av=20nyhetsbrev=20(#125)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 FörbĂ€ttrad parsning av nyhetsbrev * refactor: 💡 unfolded loop to get rid of linting error. :( * Clean up rearrangeWhitespace * undo Co-authored-by: Viktor Sarström --- lib/parse/__tests__/news.test.ts | 2 +- lib/parseHtml.test.ts | 11 ++++----- lib/parseHtml.ts | 39 +++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/parse/__tests__/news.test.ts b/lib/parse/__tests__/news.test.ts index a74cf56d..29648fc0 100644 --- a/lib/parse/__tests__/news.test.ts +++ b/lib/parse/__tests__/news.test.ts @@ -168,7 +168,7 @@ describe('newsItem', () => { const item = newsItemDetails(response) const expected = - '[1177 hemsida](https://www.1177.se/sjukdomar--besvar/hud-har-och-naglar/harbotten-och-harsackar/huvudloss/)​​​​​​​' + '[1177 hemsida](https://www.1177.se/sjukdomar--besvar/hud-har-och-naglar/harbotten-och-harsackar/huvudloss/)' expect(item.body).toContain(expected) expect(item.body).toContain(' **tillfĂ€llet** ') }) diff --git a/lib/parseHtml.test.ts b/lib/parseHtml.test.ts index 7a4681aa..dad4ce91 100644 --- a/lib/parseHtml.test.ts +++ b/lib/parseHtml.test.ts @@ -65,7 +65,7 @@ describe('parseHtml', () => { ` - const expected = `# Hello # + const expected = `# Hello **World** - Foo @@ -92,8 +92,7 @@ Alla knep Ă€r tillĂ„tna. Kolla in Reddit: [https://reddit.com/water-balloons/where-to-buy/](https://reddit.com/water-balloons/where-to-buy/) ...och hĂ€r: [https://reddit.com/splash-wars/](https://reddit.com/splash-wars/) - -## Om att vara hemma vid symtom ## +## Om att vara hemma vid symtom Även HackerNews Ă€r bra. @@ -101,10 +100,10 @@ Kolla in Reddit: [https://reddit.com/water-balloons/where-to-buy/](https://reddi Vi fortsĂ€tter ocksĂ„ att: -- hĂ„lla avstĂ„nd. +- hĂ„lla avstĂ„nd. - ha flera digitala möten. -- tvĂ€tta hĂ€nderna. -- undvika kollektivtrafik om det Ă€r möjligt. +- tvĂ€tta hĂ€nderna. +- undvika kollektivtrafik om det Ă€r möjligt. - stanna hemma Ă€ven nĂ€r man bara kĂ€nner sig lite sjuk. - vĂ€dra ofta diff --git a/lib/parseHtml.ts b/lib/parseHtml.ts index a2a76d30..a7847ed3 100644 --- a/lib/parseHtml.ts +++ b/lib/parseHtml.ts @@ -41,13 +41,40 @@ const deepClean = (node: HTMLElement): HTMLElement => { } const rearrangeWhitespace = (html: string = ''): string => { - let content = html.split(' ').join('&nbsp;') + let content = html + .replace(/]*>/gm, '') + .split('').join('') + .replace(/]*>/gm, '') + .split('').join('') + .split(' ').join('&nbsp;') + + // FIXME: Make a loop that doesn't break linting trimNodes.forEach((trimNode) => { content = content.split(`<${trimNode}> `).join(` <${trimNode}>`) content = content.split(` `).join(` `) - content = content.split(`<${trimNode}>&nbsp;`).join(`&nbsp;<${trimNode}>`) - content = content.split(`&nbsp;`).join(`&nbsp;`) + content = content.split(`<${trimNode}>&nbsp;`).join(` <${trimNode}>`) + content = content.split(`&nbsp;`).join(` `) }) + + trimNodes.forEach((trimNode) => { + content = content.split(`<${trimNode}> `).join(` <${trimNode}>`) + content = content.split(` `).join(` `) + content = content.split(`<${trimNode}>&nbsp;`).join(` <${trimNode}>`) + content = content.split(`&nbsp;`).join(` `) + }) + trimNodes.forEach((trimNode) => { + content = content.split(`<${trimNode}> `).join(` <${trimNode}>`) + content = content.split(` `).join(` `) + content = content.split(`<${trimNode}>&nbsp;`).join(` <${trimNode}>`) + content = content.split(`&nbsp;`).join(` `) + }) + trimNodes.forEach((trimNode) => { + content = content.split(`<${trimNode}> `).join(` <${trimNode}>`) + content = content.split(` `).join(` `) + content = content.split(`<${trimNode}>&nbsp;`).join(` <${trimNode}>`) + content = content.split(`&nbsp;`).join(` `) + }) + return content } @@ -66,6 +93,12 @@ const overides = { img: (node: Node) => `![${node.attrs.title || ''}](${node.attrs.src})`, i: (node: Node) => `*${node.md}*`, b: (node: Node) => `**${node.md}**`, + 'h1': (node: Node) => `# ${node.md}\n`, + 'h2': (node: Node) => `## ${node.md}\n`, + 'h3': (node: Node) => `### ${node.md}\n`, + 'h4': (node: Node) => `#### ${node.md}\n`, + 'h5': (node: Node) => `##### ${node.md}\n`, + 'h6': (node: Node) => `###### ${node.md}\n`, } export const toMarkdown = (html: string): string => {