fix(useMarkdown): fix parsing multiple links

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2552>
environments/review-docs-renov-dx7eod/deployments/18277
Kasper Seweryn 2023-08-14 15:54:27 +02:00 zatwierdzone przez Marge
rodzic fc979983ca
commit b64ca34fd7
2 zmienionych plików z 28 dodań i 3 usunięć

Wyświetl plik

@ -6,8 +6,8 @@ import showdown from 'showdown'
showdown.extension('openExternalInNewTab', {
type: 'output',
regex: /<a.+?href.+">/g,
replace (text: string) {
regex: /<a.+?href.+?">/g,
replace(text: string) {
const matches = text.match(/href="(.+)">/) ?? []
const url = matches[1] ?? './'
@ -26,7 +26,7 @@ showdown.extension('openExternalInNewTab', {
showdown.extension('linkifyTags', {
type: 'language',
regex: /#[^\W]+/g,
replace (text: string) {
replace(text: string) {
return `<a href="/library/tags/${text.slice(1)}">${text}</a>`
}
})

Wyświetl plik

@ -0,0 +1,25 @@
import { useMarkdownRaw } from '~/composables/useMarkdown'
describe('useMarkdownRaw', () => {
describe('anchors', () => {
it('should add target="_blank" to external links', () => {
const html = useMarkdownRaw('https://open.audio')
expect(html).toBe('<p><a href="https://open.audio" target="_blank" rel="noopener noreferrer">https://open.audio</a></p>')
})
it('should not link raw path', () => {
const html = useMarkdownRaw('/library/tags')
expect(html).toBe('<p>/library/tags</p>')
})
it('should not add target="_blank" to internal links', () => {
const html = useMarkdownRaw('[/library/tags](/library/tags)')
expect(html).toBe('<p><a href="/library/tags">/library/tags</a></p>')
})
it('should handle multiple links', () => {
const html = useMarkdownRaw('https://open.audio https://funkwhale.audio')
expect(html).toBe('<p><a href="https://open.audio" target="_blank" rel="noopener noreferrer">https://open.audio</a> <a href="https://funkwhale.audio" target="_blank" rel="noopener noreferrer">https://funkwhale.audio</a></p>')
})
})
})