Skip to content

Commit

Permalink
fix: 🐛 FörbĂ€ttrad parsning av nyhetsbrev (kolplattformen#125)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kajetan-kazimierczak and viktorlarsson authored Apr 30, 2021
1 parent 79e2a75 commit 82fa2dc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/parse/__tests__/news.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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** ')
})
Expand Down
11 changes: 5 additions & 6 deletions lib/parseHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('parseHtml', () => {
</tbody>
</table>
</div>`
const expected = `# Hello #
const expected = `# Hello
**World**
- Foo
Expand All @@ -92,19 +92,18 @@ 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.
[https://wnews.ycombinator.com/](https://wnews.ycombinator.com/)
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
Expand Down
39 changes: 36 additions & 3 deletions lib/parseHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 => {
Expand Down

0 comments on commit 82fa2dc

Please sign in to comment.