Skip to content

Commit

Permalink
Add redirect support
Browse files Browse the repository at this point in the history
Add a new 'redirect' layout that can be used to as a target of
an Apache HTTPD rewrite rule in order to perform redirects based
on a page or anchor.

Redirect pages can be written in asciidoc in the following form:

	:page-layout: redirect

	* xref:<new-location>[<previous-page><#previous-anchor>]
	...

Closes gh-221
  • Loading branch information
philwebb committed Mar 26, 2024
1 parent 443b5ea commit 2a282d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions preview-src/redirect.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:page-layout: redirect

* xref:index.adoc[oldpage]
* xref:baz/index.adoc[#old]
* xref:samples/index.adoc[oldpage#old]
24 changes: 24 additions & 0 deletions src/js/vendor/redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
;(function () {
'use strict'

window.addEventListener('load', onChange)
window.addEventListener('hashchange', onChange)

function onChange () {
const params = new URLSearchParams(window.location.search)
const page = params.get('page') || ''
const fragment = window.location.hash
const pageAndFragment = page + ((fragment.length === 1) ? '' : fragment)
let foundForFragment
let foundForPageAndFragment
const candidates = document.querySelector('body ul')
if (candidates) {
for (const candidate of candidates.children) {
const anchorElement = candidate.querySelector('a')
if (anchorElement.text === pageAndFragment) foundForPageAndFragment = anchorElement.href
if (anchorElement.text === fragment) foundForFragment = anchorElement.href
}
}
window.location.replace(foundForPageAndFragment || foundForFragment || 'index.html')
}
})()
7 changes: 7 additions & 0 deletions src/layouts/redirect.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<head>
<style>
body { display: none; }
</style>
</head>
{{{page.contents}}}
<script id="redirect" async src="{{{uiRootPath}}}/js/vendor/redirect.js"></script>

0 comments on commit 2a282d3

Please sign in to comment.