Skip to content

Commit 6dc10a8

Browse files
committed
update with URL class to have a more robust parser
1 parent cfd2241 commit 6dc10a8

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

app/components/MarkdownText.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ function parseMarkdown(text: string): string {
3434
// Strikethrough: ~~text~~
3535
html = html.replace(/~~(.+?)~~/g, '<del>$1</del>')
3636
37-
// Links: [text](url) - only allow http, https, mailto
37+
// Links: [text](url) - only allow https, mailto
3838
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_match, text, url) => {
3939
const decodedUrl = url.replace(/&amp;/g, '&')
40-
if (/^(https?:|mailto:)/i.test(decodedUrl)) {
41-
return `<a href="${decodedUrl}" rel="nofollow noreferrer noopener" target="_blank">${text}</a>`
42-
}
40+
try {
41+
const parsed = new URL(decodedUrl)
42+
if (['https:', 'mailto:'].includes(parsed.protocol)) {
43+
const safeUrl = decodedUrl.replace(/"/g, '&quot;')
44+
return `<a href="${safeUrl}" rel="nofollow noreferrer noopener" target="_blank">${text}</a>`
45+
}
46+
} catch {}
4347
return `${text} (${url})`
4448
})
4549

0 commit comments

Comments
 (0)