diff --git a/front/src/composables/useMarkdown.ts b/front/src/composables/useMarkdown.ts index 1f4269826..2fbf0c8a7 100644 --- a/front/src/composables/useMarkdown.ts +++ b/front/src/composables/useMarkdown.ts @@ -6,8 +6,8 @@ import showdown from 'showdown' showdown.extension('openExternalInNewTab', { type: 'output', - regex: //g, - replace (text: string) { + regex: //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 `${text}` } }) diff --git a/front/test/specs/composables/useMarkdown.test.ts b/front/test/specs/composables/useMarkdown.test.ts new file mode 100644 index 000000000..bb3b558ed --- /dev/null +++ b/front/test/specs/composables/useMarkdown.test.ts @@ -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('

https://open.audio

') + }) + + it('should not link raw path', () => { + const html = useMarkdownRaw('/library/tags') + expect(html).toBe('

/library/tags

') + }) + + it('should not add target="_blank" to internal links', () => { + const html = useMarkdownRaw('[/library/tags](/library/tags)') + expect(html).toBe('

/library/tags

') + }) + + it('should handle multiple links', () => { + const html = useMarkdownRaw('https://open.audio https://funkwhale.audio') + expect(html).toBe('

https://open.audio https://funkwhale.audio

') + }) + }) +})