fix(useMarkdown): fix parsing multiple links
Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2552>
This commit is contained in:
parent
fc979983ca
commit
b64ca34fd7
|
@ -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>`
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -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>')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue