| 1 |
!function(){"use strict";window.BetterDocsCodeSnippet=window.BetterDocsCodeSnippet||{};const e=window.BetterDocsCodeSnippetConfig||{},t={highlightJsUrl:e.hljsUrl||"",highlightJsCssLight:e.cssLight||"",highlightJsCssDark:e.cssDark||"",copySuccessTimeout:2e3,retryAttempts:3,retryDelay:1e3};let o=!1,n=!1,c=!1;function s(e){if(document.querySelector(`link[href="${e}"]`))return;const t=document.createElement("link");t.rel="stylesheet",t.href=e,t.onerror=function(){console.warn("BetterDocs Code Snippet: Failed to load stylesheet:",e)},document.head.appendChild(t)}function i(e){if(o)return void(e&&e(!0));if(n){const t=setInterval((function(){!o&&n||(clearInterval(t),e&&e(o))}),100);return}if(!t.highlightJsUrl)return void(e&&e(!1));n=!0;const c=document.querySelectorAll(".betterdocs-code-snippet-wrapper.theme-dark"),i=document.querySelectorAll(".betterdocs-code-snippet-wrapper.theme-light");c.length>0&&s(t.highlightJsCssDark),i.length>0&&s(t.highlightJsCssLight),function(e,t){const o=document.createElement("script");o.src=e,o.onload=t,o.onerror=function(){console.warn("BetterDocs Code Snippet: Failed to load script:",e),t&&t(!1)},document.head.appendChild(o)}(t.highlightJsUrl,(function(t){n=!1,t&&window.hljs?(o=!0,console.log("BetterDocs Code Snippet: Highlight.js loaded successfully")):console.warn("BetterDocs Code Snippet: Failed to load Highlight.js"),e&&e(o)}))}function r(){if(o&&window.hljs){try{window.hljs.registerAliases&&window.hljs.registerAliases("curl",{languageName:"bash"})}catch(e){}document.querySelectorAll(".betterdocs-code-snippet-code > code:not(.hljs)").forEach((function(e){try{window.hljs.highlightElement(e)}catch(e){console.warn("BetterDocs Code Snippet: Error highlighting code block:",e)}}))}}function d(e,t){const o=document.createElement("textarea");o.value=e,o.style.position="fixed",o.style.left="-999999px",o.style.top="-999999px",document.body.appendChild(o);try{o.focus(),o.select();const e=document.execCommand("copy");t&&t(e)}catch(e){console.warn("BetterDocs Code Snippet: Fallback copy failed:",e),t&&t(!1)}finally{document.body.removeChild(o)}}function a(e){const t=e.closest(".betterdocs-code-snippet-copy-container");return t?t.querySelector(".betterdocs-code-snippet-tooltip"):null}function l(e,t,o,n){const c=a(e);clearTimeout(e._betterDocsResetTimer),e.classList.remove("is-copied","is-error"),e.classList.add(t),c&&(c.textContent=o,c.classList.add("show",t)),e._betterDocsResetTimer=setTimeout((function(){e.classList.remove("is-copied","is-error"),c&&(c.textContent=c.dataset.copyLabel||"Copy",c.classList.remove("is-copied","is-error"),e.matches(":hover")||e.matches(":focus-visible")||c.classList.remove("show"))}),n)}function u(e){const o=a(e);l(e,"is-error",o?o.dataset.errorLabel||"Copy failed":"",t.copySuccessTimeout)}function p(e){const o=e.querySelector(".betterdocs-code-snippet-copy-button");o&&(o._betterDocsCopyInitialized||(o._betterDocsCopyInitialized=!0,function(e){const t=a(e);if(!t)return;const o=function(){t.classList.add("show")},n=function(){e.classList.contains("is-copied")||e.classList.contains("is-error")||t.classList.remove("show")};e.addEventListener("mouseenter",o),e.addEventListener("focus",o),e.addEventListener("mouseleave",n),e.addEventListener("blur",n)}(o),o._betterDocsClickHandler=function(n){n.preventDefault();const c=function(e){const t=e.querySelector(".betterdocs-code-snippet-panel.is-active")||e.querySelector(".betterdocs-code-snippet-panel")||e;return t.querySelector(".betterdocs-code-snippet-code code")||t.querySelector(".betterdocs-code-snippet-code")}(e),s=c&&(c.textContent||c.innerText)||"";if(!s.trim())return console.warn("BetterDocs Code Snippet: no code to copy"),void u(o);var i,r;i=s,r=function(n){if(n){!function(e){const o=a(e);l(e,"is-copied",o?o.dataset.copiedLabel||"Copied!":"",t.copySuccessTimeout)}(o);const n=new CustomEvent("betterdocs-code-copied",{detail:{snippet:e,code:s,language:e.dataset.language}});document.dispatchEvent(n)}else console.error("BetterDocs Code Snippet: Failed to copy code to clipboard"),u(o)},navigator.clipboard&&window.isSecureContext?navigator.clipboard.writeText(i).then((function(){r&&r(!0)})).catch((function(e){console.warn("BetterDocs Code Snippet: Clipboard API failed:",e),d(i,r)})):d(i,r)},o.addEventListener("click",o._betterDocsClickHandler)))}function h(){const e=document.querySelectorAll(".betterdocs-code-snippet-wrapper");0!==e.length&&(i((function(e){e&&r()})),e.forEach((function(e){"true"===e.dataset.copyButton&&p(e),"true"===e.dataset.multiLang&&function(e){const t=e.querySelector(".betterdocs-code-lang-dropdown");if(!t||t._betterDocsInitialized)return;t._betterDocsInitialized=!0;const o=t.querySelector(".betterdocs-code-lang-toggle"),n=t.querySelector(".betterdocs-code-lang-current"),c=t.querySelectorAll(".betterdocs-code-lang-option"),s=function(){t.classList.remove("is-open"),o&&o.setAttribute("aria-expanded","false")};o&&o.addEventListener("click",(function(e){e.stopPropagation();const n=!t.classList.contains("is-open");t.classList.toggle("is-open",n),o.setAttribute("aria-expanded",n?"true":"false")})),c.forEach((function(t){t.addEventListener("click",(function(){const o=t.dataset.langIndex;!function(e,t){const o=e.querySelectorAll(".betterdocs-code-snippet-panel"),n=e.querySelectorAll(".betterdocs-code-lang-option");o.forEach((function(o){const n=String(o.dataset.langIndex)===String(t);if(o.classList.toggle("is-active",n),n){o.removeAttribute("hidden");const t=o.querySelector(".betterdocs-code-snippet-code");t&&(e.dataset.language=(t.className.match(/language-([\w-]+)/)||[])[1]||e.dataset.language)}else o.setAttribute("hidden","")})),n.forEach((function(e){e.classList.toggle("is-active",String(e.dataset.langIndex)===String(t))}))}(e,o),n&&(n.innerHTML=t.innerHTML),s()}))})),document.addEventListener("click",(function(e){t.contains(e.target)||s()})),t.addEventListener("keydown",(function(e){"Escape"===e.key&&s()}))}(e)})),c=!0)}function g(){c=!1,h()}window.BetterDocsCodeSnippet={init:h,reinit:g,initCopyButton:p,applySyntaxHighlighting:r,loadHighlightJs:i},"loading"===document.readyState?document.addEventListener("DOMContentLoaded",h):h(),window.MutationObserver&&new MutationObserver((function(e){let t=!1;e.forEach((function(e){"childList"===e.type&&e.addedNodes.forEach((function(e){1===e.nodeType&&(e.classList&&e.classList.contains("betterdocs-code-snippet-wrapper")||e.querySelector&&e.querySelector(".betterdocs-code-snippet-wrapper"))&&(t=!0)}))})),t&&setTimeout(g,100)})).observe(document.body,{childList:!0,subtree:!0})}(); |