From f107cc441311b85a250de84644578501b403cc39 Mon Sep 17 00:00:00 2001 From: AakashGC Date: Mon, 18 Mar 2024 00:02:24 +1100 Subject: [PATCH] added ability to redirect to external pages --- .../assets/scripts/index.js | 38 +++++++++++++++++++ .../theme/quantecon_book_theme/layout.html | 11 ++++-- .../theme/quantecon_book_theme/theme.conf | 1 + 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/quantecon_book_theme/assets/scripts/index.js b/src/quantecon_book_theme/assets/scripts/index.js index 2f28dd6..9b35742 100644 --- a/src/quantecon_book_theme/assets/scripts/index.js +++ b/src/quantecon_book_theme/assets/scripts/index.js @@ -376,6 +376,44 @@ document.addEventListener("DOMContentLoaded", function () { ); })(); + /** + * Redirect to a URL and paste the content in admonition + */ + (function () { + let redirect = document.getElementById("redirect"); + console.log(redirect, "redirect url"); + if (redirect) { + window.location.href = + redirect.getAttribute("data-url") + + "?redirected_from=" + + window.location.pathname + + "&content=" + + redirect.getAttribute("data-content"); + } + })(); + + /** + * Get the path redirected from + */ + (function () { + let redirectedFrom = window.location.search; + if (redirectedFrom.includes("redirected_from")) { + // Create a URLSearchParams object from the query string + var searchParams = new URLSearchParams(redirectedFrom); + // Get the value of a particular variable from the query string + var redirectedFromUrl = searchParams.get("redirected_from"); + var redirectedFromContent = searchParams.get("content"); + if (redirectedFromContent) { + var admonition = document.getElementsByClassName( + "redirect-admonition", + )[0]; + admonition.classList.remove("hidden"); + var para = admonition.getElementsByTagName("p")[1]; + para.innerHTML = redirectedFromContent; + } + } + })(); + /** * Add authors to the heading of toc page */ diff --git a/src/quantecon_book_theme/theme/quantecon_book_theme/layout.html b/src/quantecon_book_theme/theme/quantecon_book_theme/layout.html index 5532d52..93975be 100644 --- a/src/quantecon_book_theme/theme/quantecon_book_theme/layout.html +++ b/src/quantecon_book_theme/theme/quantecon_book_theme/layout.html @@ -85,9 +85,14 @@
- + {%- if pagename in theme_redirects %} +
+ {%- endif %}
- +
@@ -99,8 +104,6 @@ On this page
{%- endif %} - -