Skip to content

Commit 3d33ae1

Browse files
committed
adds view page in markdown to UI element
1 parent 21af91c commit 3d33ae1

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

js/copy-to-llm.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,41 @@
1818
return route.split('/').filter(Boolean).join('-');
1919
}
2020

21+
function getScopeUrl() {
22+
try {
23+
const scope = window.__md_scope;
24+
if (scope instanceof URL) {
25+
return scope;
26+
}
27+
return new URL('.', window.location);
28+
} catch (error) {
29+
return null;
30+
}
31+
}
32+
33+
function stripBasePath(pathname) {
34+
const scopeUrl = getScopeUrl();
35+
if (!scopeUrl) {
36+
return pathname;
37+
}
38+
const basePath = scopeUrl.pathname.replace(/\/+$/, '');
39+
if (!basePath || basePath === '/') {
40+
return pathname;
41+
}
42+
if (pathname.startsWith(basePath)) {
43+
const stripped = pathname.slice(basePath.length);
44+
return stripped || '/';
45+
}
46+
return pathname;
47+
}
48+
2149
function getPageSlug() {
22-
return buildSlugFromPath(window.location.pathname);
50+
return buildSlugFromPath(stripBasePath(window.location.pathname));
2351
}
2452

2553
function getMarkdownUrl(slug) {
26-
const host = window.location ? window.location.host : '';
27-
const protocol = window.location ? window.location.protocol : 'https:';
28-
return `${protocol}//${host}/ai/pages/${slug}.md`;
54+
const baseUrl = getScopeUrl() || new URL(window.location.href);
55+
return new URL(`ai/pages/${slug}.md`, baseUrl).href;
2956
}
3057

3158
const NO_MARKDOWN_MESSAGE = 'No Markdown file available.';
@@ -255,6 +282,12 @@
255282
dropdownMenu.setAttribute('role', 'menu');
256283
dropdownMenu.setAttribute('aria-labelledby', 'dropdown-button');
257284
dropdownMenu.innerHTML = `
285+
<button class="copy-to-llm-dropdown-item" data-action="view-markdown" role="menuitem" tabindex="-1">
286+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
287+
<path d="M8 2c1.981 0 3.671.992 4.933 2.078 1.27 1.091 2.187 2.345 2.637 3.023a1.62 1.62 0 0 1 0 1.798c-.45.678-1.367 1.932-2.637 3.023C11.67 13.008 9.981 14 8 14c-1.981 0-3.671-.992-4.933-2.078C1.797 10.83.88 9.576.43 8.898a1.62 1.62 0 0 1 0-1.798c.45-.677 1.367-1.931 2.637-3.022C4.33 2.992 6.019 2 8 2ZM1.679 7.932a.12.12 0 0 0 0 .136c.411.622 1.241 1.75 2.366 2.717C5.176 11.758 6.527 12.5 8 12.5c1.473 0 2.825-.742 3.955-1.715 1.124-.967 1.954-2.096 2.366-2.717a.12.12 0 0 0 0-.136c-.412-.621-1.242-1.75-2.366-2.717C10.824 4.242 9.473 3.5 8 3.5c-1.473 0-2.825.742-3.955 1.715-1.124.967-1.954 2.096-2.366 2.717ZM8 10a2 2 0 1 1-.001-3.999A2 2 0 0 1 8 10Z"/>
288+
</svg>
289+
<span>View Page in Markdown</span>
290+
</button>
258291
<button class="copy-to-llm-dropdown-item" data-action="download-markdown" role="menuitem" tabindex="-1">
259292
<svg class="octicon" aria-hidden="true" width="16" height="16" viewBox="0 0 16 16">
260293
<path d="M2.75 14A1.75 1.75 0 0 1 1 12.25v-2.5a.75.75 0 0 1 1.5 0v2.5c0 .138.112.25.25.25h10.5a.25.25 0 0 0 .25-.25v-2.5a.75.75 0 0 1 1.5 0v2.5A1.75 1.75 0 0 1 13.25 14Z"/>
@@ -289,10 +322,20 @@
289322
return { container, copyButton, dropdownButton, dropdownMenu };
290323
}
291324

292-
// Mount UI next to the first H1 and wire handlers (skip if already rendered).
325+
// Mount UI next to the first H1 (skip if already rendered or on the home page).
293326
function addSectionCopyButtons() {
327+
const slug = getPageSlug();
328+
const isHomePage = !slug || slug === 'index';
329+
if (isHomePage) {
330+
return;
331+
}
332+
333+
if (document.querySelector('.copy-to-llm-split-container')) {
334+
return;
335+
}
336+
294337
const mainTitle = document.querySelector('.md-content h1');
295-
if (mainTitle && !document.querySelector('.copy-to-llm-split-container')) {
338+
if (mainTitle) {
296339
const wrapper = document.createElement('div');
297340
wrapper.className = 'h1-copy-wrapper';
298341
mainTitle.parentNode.insertBefore(wrapper, mainTitle);
@@ -422,6 +465,12 @@
422465

423466
// Each dropdown option maps to one of the shared helpers or a new-tab prompt.
424467
switch (action) {
468+
case 'view-markdown': {
469+
trackButtonClick('view_page_markdown');
470+
const mdUrl = getMarkdownUrl(slug);
471+
window.open(mdUrl, '_blank', 'noopener');
472+
break;
473+
}
425474
case 'download-markdown': {
426475
trackButtonClick('download_page_markdown');
427476
const result = await downloadMarkdown(slug, `${slug}.md`);
@@ -494,4 +543,4 @@
494543
} else {
495544
initialize();
496545
}
497-
})();
546+
})();

0 commit comments

Comments
 (0)