diff --git a/_static/js/redirect.js b/_static/js/redirect.js new file mode 100644 index 00000000..80cd573b --- /dev/null +++ b/_static/js/redirect.js @@ -0,0 +1,37 @@ +// As part of the migration of the docs-core repo into dashpay/docs the docs-core pages need to be +// redirect docs.dash.org for anyone still accessing docs via outdated links. This script +// accomplishes the redirects without having to manually define through the readthedocs console. + +document.addEventListener('DOMContentLoaded', function () { + // Get the current path and host + var currentPath = window.location.pathname; // e.g., /projects/core/en/some-branch/ + var currentHash = window.location.hash; // This captures any anchor (e.g., #section1) + var newBaseURL = 'https://docs.dash.org'; + + // Insert a breakpoint for debugging if Developer Tools are open + debugger; + + // Transform the path by replacing only the start of the path + // Remove '/projects/core/' and replace 'en' with 'develop/docs/core' + var replacements = [ + { pattern: /^\/projects\/core\/en\/develop\/docs\//, replacement: '/en/develop/docs/core/' }, + { pattern: /^\/projects\/core\/en\/develop\//, replacement: '/en/develop/docs/core/' }, + { pattern: /^\/projects\/core\/en\/latest\/docs\//, replacement: '/en/latest/docs/core/' }, + { pattern: /^\/projects\/core\/en\/latest\//, replacement: '/en/latest/docs/core/' } + ]; + + var transformedPath = currentPath; // Start with the original path + + for (var i = 0; i < replacements.length; i++) { + if (replacements[i].pattern.test(currentPath)) { + transformedPath = currentPath.replace(replacements[i].pattern, replacements[i].replacement); + break; // Stop after the first match + } + } + + // Only redirect if the transformed path is different from the original path + if (transformedPath !== currentPath) { + var newURL = newBaseURL + transformedPath + currentHash; + window.location.replace(newURL); + } +}); diff --git a/conf.py b/conf.py index 1a7dc6b6..65407e52 100644 --- a/conf.py +++ b/conf.py @@ -159,3 +159,4 @@ def setup(app): app.add_js_file('js/pydata-search-close.js') + app.add_js_file('js/redirect.js')