Fix RegExpArrayMatch guard

This commit is contained in:
wvffle 2022-11-17 21:01:11 +00:00 committed by Kasper Seweryn
parent 6370f03a23
commit 6b537cb8bb
1 changed files with 2 additions and 1 deletions

View File

@ -9,6 +9,7 @@ showdown.extension('openExternalInNewTab', {
regex: /<a.+?href.+">/g,
replace (text: string) {
const matches = text.match(/href="(.+)">/) ?? []
const url = matches[1] ?? './'
if ((!url.startsWith('http://') && !url.startsWith('https://')) || url.startsWith('mailto:')) {
@ -16,7 +17,7 @@ showdown.extension('openExternalInNewTab', {
}
const { hostname } = new URL(url)
return hostname !== location.hostname
return hostname !== location.hostname && typeof matches[0] === 'string'
? text.replace(matches[0], `href="${url}" target="_blank" rel="noopener noreferrer">`)
: text
}