fix: 🐛 Fixar links med mellanslag (#63)

This commit is contained in:
Johan Öbrink 2021-02-12 13:49:10 +01:00 committed by GitHub
parent 83ec3833c3
commit 3edbf8c2c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 8 deletions

View File

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

View File

@ -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