Last active
January 6, 2019 00:04
-
-
Save gduverger/452f00dc195612edd6733ee6cde0d5d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I wrote an Ivy (https://darrenmulholland.com/docs/ivy/) extension | |
# to allow multiple paths to the same node by using the keyword | |
# `redirects` in `./src/foo.md` (e.g., `redirects: bar, foo-bar`). | |
import ivy | |
import copy | |
@ivy.hooks.register('render_page') | |
def copy_page(page): | |
node = page['node'] | |
redirects = node.get('redirects') | |
if redirects is not None: | |
node['redirects'] = None | |
redirects = redirects.split(',') | |
for redirect in redirects: | |
_node = copy.deepcopy(node) | |
_page = ivy.pages.Page(_node) | |
_page['redirect'] = redirect.strip() | |
_page.render() | |
@ivy.hooks.register('page_path') | |
def replace_path(slugs, page): | |
node = page['node'] | |
redirect = page.get('redirect') | |
if redirect is not None: | |
return slugs.replace(node.slug, redirect) | |
return slugs | |
@ivy.hooks.register('page_html') | |
def redirect_html(html, page): | |
node = page['node'] | |
redirect = page.get('redirect') | |
if redirect is not None: | |
return """<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="refresh" content="0; url='{url}'"> | |
<script type="text/javascript"> | |
window.location.href = "{url}" | |
</script> | |
<title>Page Redirection</title> | |
</head> | |
<body> | |
If you are not redirected automatically, click this <a href='{url}'>link</a>. | |
</body> | |
</html>\n""".format(url=node.url) | |
return html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment