From cc79e5f13b4f0668c300e3740254ebccc13bb0ba Mon Sep 17 00:00:00 2001 From: Eugene Kotsarev Date: Wed, 12 Jun 2024 18:23:01 +0300 Subject: [PATCH] add internal link auto replacement for iframe resizer --- docusaurus.config.js | 4 ++++ static/js/with-iframe-resizer.js | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 static/js/with-iframe-resizer.js diff --git a/docusaurus.config.js b/docusaurus.config.js index c792c9cff..bb9199c9c 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -157,6 +157,10 @@ export default { src: 'https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.11/iframeResizer.contentWindow.min.js', defer: true, }, + { + src: '/js/with-iframe-resizer.js', + defer: true, + }, // { // src: 'https://cdn.jsdelivr.net/npm/@iframe-resizer/child', // defer: true, diff --git a/static/js/with-iframe-resizer.js b/static/js/with-iframe-resizer.js new file mode 100644 index 000000000..05a805c95 --- /dev/null +++ b/static/js/with-iframe-resizer.js @@ -0,0 +1,38 @@ +document.addEventListener('DOMContentLoaded', () => { + const params = new Proxy(new URLSearchParams(window.location.search), { + get: (searchParams, prop) => searchParams.get(prop), + }) + const isVaraTheme = params['docusaurus-data-theme-vara'] // Get the value of "docusaurus-data-theme-vara" + + if (isVaraTheme) { + const anchors = document.body.querySelectorAll('main a[href^="/docs"]') + + const transformLinks = (interval) => { + const anchors = document.body.querySelectorAll('main a[href^="/docs"]') + + if (anchors.length) { + Array.from(anchors).forEach(a => { + a.setAttribute('target', '_blank') + a.addEventListener('click', (e) => { + e.preventDefault() + window.open(e.target.href, '_blank') + }) + }) + clearInterval(interval) + } + } + + if (!anchors.length) { + let count = 0 + let interval + interval = setInterval(function () { + if (count === 10) { + clearInterval(interval) + return + } + transformLinks(interval) + count++ + }, 1000) + } + } +})