fix: 🐛 Fixar links med mellanslag (#63)
This commit is contained in:
parent
83ec3833c3
commit
3edbf8c2c6
|
@ -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', () => {
|
||||
|
|
|
@ -1,14 +1,27 @@
|
|||
import * as h2m from 'h2m'
|
||||
import { htmlDecode } from 'js-htmlencode'
|
||||
|
||||
export const trim = (html: string = ''): string => html
|
||||
.replace(/ /g, ' ')
|
||||
.split('>')
|
||||
.map((token) => token.trim())
|
||||
.join('>')
|
||||
.split('</')
|
||||
.map((token) => token.trim())
|
||||
.join('</')
|
||||
export const trim = (html: string = ''): string => {
|
||||
const trimmed = html
|
||||
.replace(/ /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
|
||||
|
|
Loading…
Reference in New Issue