fix(useMarkdown): fix parsing multiple links

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2552>
This commit is contained in:
Kasper Seweryn 2023-08-14 15:54:27 +02:00 committed by Marge
parent fc979983ca
commit b64ca34fd7
2 changed files with 28 additions and 3 deletions

View File

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

View File

@ -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>')
})
})
})