diff --git a/docs/_static/js/overwrite_links.js b/docs/_static/js/overwrite_links.js new file mode 100644 index 0000000..25e0ad9 --- /dev/null +++ b/docs/_static/js/overwrite_links.js @@ -0,0 +1,38 @@ + // Replace oldDomain with newDomain + const oldDomain = 'canonical-chisel-migration.readthedocs-hosted.com'; + const newDomain = 'ubuntu.com/chisel/docs/'; + + function escapeRegExp(value) { + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + + function overwriteMatchingAnchorUrls(container) { + if (!container) return; + + const anchors = container.querySelectorAll('a[href], link[href]'); + const oldDomainRegex = new RegExp(escapeRegExp(oldDomain), 'g'); + + anchors.forEach(anchor => { + anchor.href = anchor.href.replace(oldDomainRegex, newDomain); + }); + } + + overwriteMatchingAnchorUrls(document.querySelector('header')); + + // Use a MutationObserver to wait for the RTD flyout element to appear in the DOM + const observer = new MutationObserver(function(mutations, obs) { + + const rtdFlyout = document.querySelector('readthedocs-flyout'); + if (!rtdFlyout) return; + + obs.disconnect(); + + rtdFlyout.addEventListener('click', function() { + const shadowRoot = rtdFlyout.shadowRoot; + if (!shadowRoot) return; + + overwriteMatchingAnchorUrls(shadowRoot); + }); + }); + + observer.observe(document.body, { childList: true, subtree: true }); \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 8acba1f..402747b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -31,7 +31,9 @@ # Sidebar documentation title; best kept reasonably short # # TODO: To include a version number, add it here (hardcoded or automated). -# + +version = f"{os.environ.get('READTHEDOCS_VERSION', 'local')}" + # TODO: To disable the title, set to an empty string. html_title = project + " documentation" @@ -70,7 +72,7 @@ # NOTE: The Open Graph Protocol (OGP) enhances page display in a social graph # and is used by social media platforms; see https://ogp.me/ -ogp_site_url = "https://documentation.ubuntu.com/chisel/" +ogp_site_url = f"https://ubuntu.com/chisel/docs//{version}/" # Preview name of the documentation website @@ -176,7 +178,7 @@ # TODO: If your documentation is hosted on https://docs.ubuntu.com/, # uncomment and update as needed. -slug = "chisel" +slug = 'chisel/docs' # Or '//docs' ####################### # Sitemap configuration: https://sphinx-sitemap.readthedocs.io/ @@ -184,7 +186,7 @@ # Use RTD canonical URL to ensure duplicate pages have a specific canonical URL -html_baseurl = os.environ.get("READTHEDOCS_CANONICAL_URL", "/") +html_baseurl = f"https://ubuntu.com/chisel/docs//{version}/" # URL scheme. Add language and version scheme elements. # When configured with RTD variables, check for RTD environment so manual runs succeed: @@ -314,6 +316,7 @@ html_js_files = [ 'bundle.js', + "js/overwrite_links.js", ] @@ -392,4 +395,3 @@ def setup(app): app.add_css_file("tasklist.css") app.add_js_file("tasklist.js") #checkbox-enabling JS -