Skip to content

Commit

Permalink
Merge pull request #13 from hatmarch/master
Browse files Browse the repository at this point in the history
Improve error handling and xref support
  • Loading branch information
blues-man authored Aug 5, 2021
2 parents 26b3779 + 75899ad commit e3cddb8
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 e3cddb8

Please sign in to comment.