fix: 🐛 Förbättrad parsning av nyhetsbrev (#125)

* 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 <viktorsarstrom@gmail.com>
This commit is contained in:
Kajetan Kazimierczak 2021-04-30 10:37:44 +02:00 committed by GitHub
parent 79e2a7577b
commit 82fa2dcc84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 10 deletions

View File

@ -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** ')
})

View File

@ -65,7 +65,7 @@ describe('parseHtml', () => {
</tbody>
</table>
</div>`
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

View File

@ -41,13 +41,40 @@ const deepClean = (node: HTMLElement): HTMLElement => {
}
const rearrangeWhitespace = (html: string = ''): string => {
let content = html.split('&#160;').join('&amp;nbsp;')
let content = html
.replace(/<span[^>]*>/gm, '')
.split('</span>').join('')
.replace(/<div[^>]*>/gm, '')
.split('</div>').join('')
.split('&#160;').join('&amp;nbsp;')
// FIXME: Make a loop that doesn't break linting
trimNodes.forEach((trimNode) => {
content = content.split(`<${trimNode}> `).join(` <${trimNode}>`)
content = content.split(` </${trimNode}>`).join(`</${trimNode}> `)
content = content.split(`<${trimNode}>&amp;nbsp;`).join(`&amp;nbsp;<${trimNode}>`)
content = content.split(`&amp;nbsp;</${trimNode}>`).join(`</${trimNode}>&amp;nbsp;`)
content = content.split(`<${trimNode}>&amp;nbsp;`).join(` <${trimNode}>`)
content = content.split(`&amp;nbsp;</${trimNode}>`).join(`</${trimNode}> `)
})
trimNodes.forEach((trimNode) => {
content = content.split(`<${trimNode}> `).join(` <${trimNode}>`)
content = content.split(` </${trimNode}>`).join(`</${trimNode}> `)
content = content.split(`<${trimNode}>&amp;nbsp;`).join(` <${trimNode}>`)
content = content.split(`&amp;nbsp;</${trimNode}>`).join(`</${trimNode}> `)
})
trimNodes.forEach((trimNode) => {
content = content.split(`<${trimNode}> `).join(` <${trimNode}>`)
content = content.split(` </${trimNode}>`).join(`</${trimNode}> `)
content = content.split(`<${trimNode}>&amp;nbsp;`).join(` <${trimNode}>`)
content = content.split(`&amp;nbsp;</${trimNode}>`).join(`</${trimNode}> `)
})
trimNodes.forEach((trimNode) => {
content = content.split(`<${trimNode}> `).join(` <${trimNode}>`)
content = content.split(` </${trimNode}>`).join(`</${trimNode}> `)
content = content.split(`<${trimNode}>&amp;nbsp;`).join(` <${trimNode}>`)
content = content.split(`&amp;nbsp;</${trimNode}>`).join(`</${trimNode}> `)
})
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 => {