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

@@ -1,10 +1,6 @@
module.exports = { module.exports = {
serveLinkWarningPage: function (req, res) { serveLinkWarningPage: function (req, res) {
let targetURL = typeof req.query.url === 'string' ? req.query.url : '' let targetURL = typeof req.query.url === 'string' ? req.query.url : ''
let noteURL = typeof req.query.note === 'string' ? req.query.note : ''
if (noteURL !== '' && !/^[\w-]+$/.test(noteURL)) {
noteURL = ''
}
let valid = false let valid = false
try { try {
targetURL = decodeURIComponent(targetURL) targetURL = decodeURIComponent(targetURL)
@@ -20,8 +16,8 @@ module.exports = {
res.render('link.ejs', { res.render('link.ejs', {
title: 'External link', title: 'External link',
valid, valid,
noteURL,
targetURL, targetURL,
cspNonce: res.locals.nonce,
opengraph: [] opengraph: []
}) })
} }

View File

@@ -132,7 +132,7 @@
"The content of that page is unrelated to HedgeDoc. Do not enter your HedgeDoc credentials there.": "Der Inhalt dieser Seite ist unabhängig von HedgeDoc. Dort keine HedgeDoc-Zugangsdaten eingeben.", "The content of that page is unrelated to HedgeDoc. Do not enter your HedgeDoc credentials there.": "Der Inhalt dieser Seite ist unabhängig von HedgeDoc. Dort keine HedgeDoc-Zugangsdaten eingeben.",
"Do you want to continue?": "Fortfahren?", "Do you want to continue?": "Fortfahren?",
"Continue to external page": "Weiter zur externen Seite", "Continue to external page": "Weiter zur externen Seite",
"Back to your note": "Zurück zur Notiz", "Close": "Schließen",
"Invalid link": "Ungültiger Link", "Invalid link": "Ungültiger Link",
"The provided link is not a valid URL.": "Die angegebene URL ist nicht gültig." "The provided link is not a valid URL.": "Die angegebene URL ist nicht gültig."
} }

View File

@@ -132,7 +132,7 @@
"Invalid link": "Invalid link", "Invalid link": "Invalid link",
"Do you want to continue?": "Do you want to continue?", "Do you want to continue?": "Do you want to continue?",
"Continue to external page": "Continue to external page", "Continue to external page": "Continue to external page",
"Back to your note": "Back to your note", "Close": "Close",
"The provided link is not a valid URL.": "The provided link is not a valid URL.", "The provided link is not a valid URL.": "The provided link is not a valid URL.",
"The content of that page is unrelated to HedgeDoc. Do not enter your HedgeDoc credentials there.": "The content of that page is unrelated to HedgeDoc. Do not enter your HedgeDoc credentials there." "The content of that page is unrelated to HedgeDoc. Do not enter your HedgeDoc credentials there.": "The content of that page is unrelated to HedgeDoc. Do not enter your HedgeDoc credentials there."
} }

View File

@@ -6,6 +6,10 @@
- Automatically skip the login modal if no form-based login and exactly one external login provider is configured - Automatically skip the login modal if no form-based login and exactly one external login provider is configured
### Bugfixes
- All links will be opened in new tabs instead of the same tab as the note. This was changed when the external link warning was implemented and now the old behavior is restored.
## <i class="fa fa-tag"></i> 1.11.1 <i class="fa fa-calendar-o"></i> 2026-07-24 ## <i class="fa fa-tag"></i> 1.11.1 <i class="fa fa-calendar-o"></i> 2026-07-24
### Security fixes ### Security fixes

View File

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

View File

@@ -27,15 +27,18 @@
<p><%= __('Do you want to continue?') %></p> <p><%= __('Do you want to continue?') %></p>
<p> <p>
<a class="btn btn-primary" href="<%- targetURL %>"><%= __('Continue to external page') %></a> <a class="btn btn-primary" href="<%- targetURL %>"><%= __('Continue to external page') %></a>
<a class="btn btn-default" href="<%- serverURL %>/<%- noteURL %>"><%= __('Back to your note') %></a> <a class="btn btn-default" id="close-button"><%= __('Close') %></a>
</p> </p>
<% } else { %> <% } else { %>
<p class="text-danger"><strong><%= __('The provided link is not a valid URL.') %></strong></p> <p class="text-danger"><strong><%= __('The provided link is not a valid URL.') %></strong></p>
<p> <p>
<a class="btn btn-default" href="<%- serverURL %>/<%- noteURL %>"><%= __('Back to your note') %></a> <a class="btn btn-default" id="close-button"><%= __('Close') %></a>
</p> </p>
<% } %> <% } %>
</div> </div>
</div> </div>
</body> </body>
<script nonce="<%= cspNonce %>">
document.getElementById("close-button").addEventListener('click', _ => window.close())
</script>
</html> </html>