fix: links (#64)

* fix: 🐛 Fixar links med mellanslag

* fix: 🐛 Links med single quote
This commit is contained in:
Johan Öbrink 2021-02-12 14:06:29 +01:00 committed by GitHub
parent 3edbf8c2c6
commit 905b893ca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -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', () => {

View File

@ -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) {
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
const rxSpacesSing = /href='(.*)'/g
matches = trimmed.match(rxSpacesSing)
if (matches) {
// eslint-disable-next-line no-restricted-syntax
for (const match of matches) {
result = result.replace(match, match.replace(/ /g, '%20'))
}
}
return result
}
interface Node {