fix: open links in new tab

The addition of external link warning has changed the behavior of
opening links from opening in a new tab to opening in the same tab.
This restores the old behavior and opens all links in a new tab.

Signed-off-by: Tony Chau <90050474+fo5for@users.noreply.github.com>
This commit is contained in:
Tony Chau
2026-08-15 22:27:47 +08:00
committed by Erik Michelson
parent d05cfe4e9f
commit ffcf79ef07
6 changed files with 22 additions and 11 deletions

View File

@@ -593,6 +593,7 @@ window.postProcess = postProcess
// rewrite external links to go through the /_link warning page
export function rewriteExternalLinks (view) {
if (window.externalLinkWarning === false) {
view.find('a:not([href^="#"]):not([target])').attr('target', '_blank').attr('rel', 'noopener')
return
}
const whitelist = window.externalLinkWhitelist || []
@@ -621,11 +622,18 @@ export function rewriteExternalLinks (view) {
}
return hostname === lowercaseDomain
})) {
if (!anchor.attr('target')) {
anchor.attr('target', '_blank').attr('rel', 'noopener')
}
return
}
const noteURL = window.location.pathname.split('/').pop()
const urlPath = window.urlpath ? `/${window.urlpath}` : ''
anchor.attr('href', `${window.location.origin}${urlPath}/_link?url=${encodeURIComponent(parsed.href)}&note=${noteURL}`)
const warningURL = `${window.location.origin}${urlPath}/_link?url=${encodeURIComponent(parsed.href)}`
anchor.attr('href', warningURL)
anchor.on('click', event => {
event.preventDefault()
window.open(warningURL, '_blank')
})
})
}
window.rewriteExternalLinks = rewriteExternalLinks