Skip to content

Commit

Permalink
Improve error handling and xref support
Browse files Browse the repository at this point in the history
Malformed links could break the "preserve query string" function which could in turn stop querystring replacement for most of the page

Catches any error attempted to replace querystring elements in a link and logs as an error

To minimize errors, query string replacement on links is done *before* attempting to preserve query strings

xrefs (of type a.page) automatically have querystring preserved in their links
  • Loading branch information
hatmarch committed Aug 2, 2021
1 parent 579a7a7 commit fde8477
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions preview-src/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ And this also should not: [.no-query-replace]`%USER%`

You should see USER replacement here: %USER% but not here: [.no-query-replace]#%USER%#

=== Xrefs

xref:index.adoc#link-testing[This,role="page"] is an xref that needs the query string preserved.

== Cu solet

Expand Down
27 changes: 21 additions & 6 deletions src/js/07-userparams-behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ document.addEventListener('DOMContentLoaded', function () {
// If there is a query string, we need to fix up the page to make sure the query string is properly preserved
var desiredQueryString = new URLSearchParams(window.location.search)
if (desiredQueryString.toString()) {
preserveQueryString(desiredQueryString)

// If there are query parameters (searchparams) in the current window location
// then iterate over all them replacing text and link-hrefs that contain them
for (var k of desiredQueryString.keys()) {
replaceParamsInNodes(document.body, k, desiredQueryString.get(k))
}

// NOTE: Once all links have been query string replaced, then attempt to preserve the query string
// for all references
preserveQueryString(desiredQueryString)

}

function preserveQueryString (desiredQueryString) {
Expand All @@ -23,17 +28,27 @@ document.addEventListener('DOMContentLoaded', function () {
if (paramLinks) {
paramLinks.forEach(appendQueryStringToHref)
}

// Handle xrefs, which are anchor elements generated by antora with the page attribute
var xrefLinks = document.querySelectorAll('a.page')
if (xrefLinks) {
xrefLinks.forEach(appendQueryStringToHref)
}
}

function appendQueryStringToHref (el) {
// NOTE: desiredQueryString captured from above
if (desiredQueryString.toString()) {
var hrefURL = new URL(el.href)
for (var k of desiredQueryString.keys()) {
hrefURL.searchParams.set(k, desiredQueryString.get(k))
}
try {
var hrefURL = new URL(el.href)
for (var k of desiredQueryString.keys()) {
hrefURL.searchParams.set(k, desiredQueryString.get(k))
}

el.href = hrefURL.toString()
el.href = hrefURL.toString()
} catch (err) {
console.error('Unable to append query string to element [' + el.innerHTML + ']: ' + err)
}
}
}

Expand Down

0 comments on commit fde8477

Please sign in to comment.