diff --git a/docs/_static/js/overwrite_links.js b/docs/_static/js/overwrite_links.js new file mode 100644 index 00000000..d6c5c3ed --- /dev/null +++ b/docs/_static/js/overwrite_links.js @@ -0,0 +1,53 @@ +// Replaces rtd-address with new-address in links + +const rtd_address = 'http://canonical-real-time-ubuntu-documentation.readthedocs-hosted.com' + +const new_address = 'ubuntu.com/real-time/docs'; + +function escapeRegExp(value) { + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + +function overwriteMatchingAnchorUrls(container) { + if (!container) return; + + const rtd_addressRegex = new RegExp(escapeRegExp(rtd_address), 'g'); + container.querySelectorAll('a[href], link[href]').forEach((anchor) => { + anchor.href = anchor.href.replace(rtd_addressRegex, new_address); + }); +} + +function patchFlyout() { + const rtdFlyout = document.querySelector('readthedocs-flyout'); + if (!rtdFlyout) return false; + + overwriteMatchingAnchorUrls(rtdFlyout); + overwriteMatchingAnchorUrls(rtdFlyout.shadowRoot); + + rtdFlyout.addEventListener('click', () => { + overwriteMatchingAnchorUrls(rtdFlyout); + overwriteMatchingAnchorUrls(rtdFlyout.shadowRoot); + }); + + return true; +} + +function init() { + overwriteMatchingAnchorUrls(document.querySelector('header')); + + if (patchFlyout()) return; + + const observer = new MutationObserver(() => { + if (patchFlyout()) { + observer.disconnect(); + } + }); + + observer.observe(document.body, { childList: true, subtree: true }); +} + +if (document.body) { + init(); +} else { + document.addEventListener('DOMContentLoaded', init); +} \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 1ef9ab10..b34ec013 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -35,7 +35,7 @@ html_title = project + " documentation" # Documentation website URL -ogp_site_url = os.environ.get("READTHEDOCS_CANONICAL_URL", "/") +ogp_site_url = "https://ubuntu.com/real-time/docs/" # Preview name of the documentation website # TODO: To use a different name for the project in previews, update the next line. @@ -112,17 +112,18 @@ # Project slug # TODO: If your documentation is hosted on https://documentation.ubuntu.com/, # uncomment and set to the RTD slug. -slug = 'real-time' +slug = 'real-time/docs' ####################### # Sitemap configuration: https://sphinx-sitemap.readthedocs.io/ ####################### # Use RTD canonical URL to ensure duplicate pages have a specific canonical URL -html_baseurl = os.environ.get("READTHEDOCS_CANONICAL_URL", "/") +html_baseurl = "https://ubuntu.com/real-time/docs/" # sphinx-sitemap uses html_baseurl to generate the full URL for each page: sitemap_url_scheme = "{link}" +sitemap_filename = "doc-sitemap.xml" # Include `lastmod` dates in the sitemap: sitemap_show_lastmod = True @@ -267,6 +268,7 @@ # Adds custom JavaScript files, located remotely or in 'html_static_path'. html_js_files = [ "js/bundle.js", + "js/overwrite_links.js", ] # Appends extra markup to the end of every document written in reST