Skip to content

Commit

Permalink
fix: 馃悰 Fixar links med mellanslag (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanObrink committed Feb 12, 2021
1 parent 83ec383 commit 3edbf8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/parseHtml.test.ts
Expand Up @@ -14,6 +14,13 @@ describe('parseHtml', () => {
it('handles missing html', () => {
expect(() => trim()).not.toThrow()
})
it('handles links with spaces', () => {
const html = `<div>
<a href="/foo bar">Hello </a>
</div>`

expect(trim(html)).toEqual('<div><a href="/foo%20bar">Hello</a></div>')
})
})
describe('toMarkdown', () => {
it('turns html into Markdown', () => {
Expand Down
29 changes: 21 additions & 8 deletions lib/parseHtml.ts
@@ -1,14 +1,27 @@
import * as h2m from 'h2m'
import { htmlDecode } from 'js-htmlencode'

export const trim = (html: string = ''): string => html
.replace(/&#160;/g, ' ')
.split('>')
.map((token) => token.trim())
.join('>')
.split('</')
.map((token) => token.trim())
.join('</')
export const trim = (html: string = ''): string => {
const trimmed = html
.replace(/&#160;/g, ' ')
.split('>')
.map((token) => token.trim())
.join('>')
.split('</')
.map((token) => token.trim())
.join('</')
const rxSpaces = /href="(.*)"/g
const matches = trimmed.match(rxSpaces)
if (matches) {
let result = trimmed
// eslint-disable-next-line no-restricted-syntax
for (const match of matches) {
result = result.replace(match, match.replace(/ /g, '%20'))
}
return result
}
return trimmed
}

interface Node {
name: string
Expand Down

0 comments on commit 3edbf8c

Please sign in to comment.