Skip to content

Commit

Permalink
fix: links (kolplattformen#64)
Browse files Browse the repository at this point in the history
* fix: 🐛 Fixar links med mellanslag

* fix: 🐛 Links med single quote
  • Loading branch information
JohanObrink authored Feb 12, 2021
1 parent 3edbf8c commit 905b893
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/parseHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ describe('parseHtml', () => {
it('handles links with spaces', () => {
const html = `<div>
<a href="/foo bar">Hello </a>
<a href='/foo bar'>Hello </a>
</div>`

expect(trim(html)).toEqual('<div><a href="/foo%20bar">Hello</a></div>')
expect(trim(html)).toEqual('<div><a href="/foo%20bar">Hello</a><a href=\'/foo%20bar\'>Hello</a></div>')
})
})
describe('toMarkdown', () => {
Expand Down
17 changes: 13 additions & 4 deletions lib/parseHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ export const trim = (html: string = ''): string => {
.split('</')
.map((token) => token.trim())
.join('</')

let result = trimmed
const rxSpaces = /href="(.*)"/g
const matches = trimmed.match(rxSpaces)
let matches = trimmed.match(rxSpaces)
if (matches) {
// eslint-disable-next-line no-restricted-syntax
for (const match of matches) {
result = result.replace(match, match.replace(/ /g, '%20'))
}
}

const rxSpacesSing = /href='(.*)'/g
matches = trimmed.match(rxSpacesSing)
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
return result
}

interface Node {
Expand Down

0 comments on commit 905b893

Please sign in to comment.