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', () => {
|
it('handles missing html', () => {
|
||||||
expect(() => trim()).not.toThrow()
|
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', () => {
|
describe('toMarkdown', () => {
|
||||||
it('turns html into Markdown', () => {
|
it('turns html into Markdown', () => {
|
||||||
|
|
|
@ -1,14 +1,27 @@
|
||||||
import * as h2m from 'h2m'
|
import * as h2m from 'h2m'
|
||||||
import { htmlDecode } from 'js-htmlencode'
|
import { htmlDecode } from 'js-htmlencode'
|
||||||
|
|
||||||
export const trim = (html: string = ''): string => html
|
export const trim = (html: string = ''): string => {
|
||||||
.replace(/ /g, ' ')
|
const trimmed = html
|
||||||
.split('>')
|
.replace(/ /g, ' ')
|
||||||
.map((token) => token.trim())
|
.split('>')
|
||||||
.join('>')
|
.map((token) => token.trim())
|
||||||
.split('</')
|
.join('>')
|
||||||
.map((token) => token.trim())
|
.split('</')
|
||||||
.join('</')
|
.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 {
|
interface Node {
|
||||||
name: string
|
name: string
|
||||||
|
|
Loading…
Reference in New Issue