diff --git a/js/copy-to-llm.js b/js/copy-to-llm.js index d309565dd..c8b7cf194 100644 --- a/js/copy-to-llm.js +++ b/js/copy-to-llm.js @@ -18,14 +18,41 @@ return route.split('/').filter(Boolean).join('-'); } + function getScopeUrl() { + try { + const scope = window.__md_scope; + if (scope instanceof URL) { + return scope; + } + return new URL('.', window.location); + } catch (error) { + return null; + } + } + + function stripBasePath(pathname) { + const scopeUrl = getScopeUrl(); + if (!scopeUrl) { + return pathname; + } + const basePath = scopeUrl.pathname.replace(/\/+$/, ''); + if (!basePath || basePath === '/') { + return pathname; + } + if (pathname.startsWith(basePath)) { + const stripped = pathname.slice(basePath.length); + return stripped || '/'; + } + return pathname; + } + function getPageSlug() { - return buildSlugFromPath(window.location.pathname); + return buildSlugFromPath(stripBasePath(window.location.pathname)); } function getMarkdownUrl(slug) { - const host = window.location ? window.location.host : ''; - const protocol = window.location ? window.location.protocol : 'https:'; - return `${protocol}//${host}/ai/pages/${slug}.md`; + const baseUrl = getScopeUrl() || new URL(window.location.href); + return new URL(`ai/pages/${slug}.md`, baseUrl).href; } const NO_MARKDOWN_MESSAGE = 'No Markdown file available.'; @@ -255,6 +282,12 @@ dropdownMenu.setAttribute('role', 'menu'); dropdownMenu.setAttribute('aria-labelledby', 'dropdown-button'); dropdownMenu.innerHTML = ` +