Skip to content

Commit bdc3a0a

Browse files
committed
clean up contents, prettier
1 parent 3d31131 commit bdc3a0a

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

js/copy-to-llm.js

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
// Copy to LLM functionality
2-
// From: https://github.com/leonardocustodio/mkdocs-copy-to-llm/blob/main/mkdocs_copy_to_llm/assets/js/copy-to-llm.js
1+
/* Copy to LLM functionality
2+
Modified from: https://github.com/leonardocustodio/mkdocs-copy-to-llm/blob/main/mkdocs_copy_to_llm/assets/js/copy-to-llm.js
33
4-
/*
5-
This script adds per-page LLM functionality UI controller that:
4+
This script adds per-page LLM functionality UI controller that:
65
- Renders a split button next to the main heading that copies, downloads, or opens Markdown in ChatGPT or Claude.
7-
- Emits analytics via GA (TODO: needs wired up to work)
8-
*/
6+
- Emits analytics via GA (TODO: needs wired up to work).
7+
*/
98

109
(function () {
1110
'use strict';
12-
/* protects script from crashing when it's evaluated in env w/o a browser
13-
(build, etc.) no window = no DOM = script quietly exits */
11+
/* Protects script from crashing when evaluated in env w/o a browser
12+
(build, etc.). No window = no DOM = script quietly exits. */
1413
if (typeof window === 'undefined') {
1514
return;
1615
}
17-
// All config data lives in `llms_config.json` file.
16+
// All config data lives in `llms_config.json` file.
1817
const CONFIG_URL = '/scripts/llms_config.json';
1918

2019
const state = {
2120
// When loadConfig() fetches /scripts/llms_config.json, the parsed JSON lands here.
2221
config: null,
23-
/* If multiple callers hit ready() before the first fetch resolves, they all share this promise instead of firing duplicate network requests. */
22+
/* If multiple callers hit ready() before the first fetch resolves, they all share this promise instead of firing duplicate network requests. */
2423
configPromise: null,
2524
// Derived once from window.location.origin, trimmed of trailing slashes.
2625
siteBase: window.location ? window.location.origin.replace(/\/+$/, '') : '',
27-
/** After the config loads, computeRemoteBase(config) may set this to a raw GitHub URL (pulling repository.org, repository.repo, etc.). When present, it’s the highest-priority candidate in getSlugCandidates() for finding Markdown artifacts.*/
28-
remoteBase: ''
26+
/* After the config loads, computeRemoteBase(config) may set this to a raw GitHub URL (pulling repository.org, repository.repo, etc.). When present, it’s the highest-priority candidate in getSlugCandidates() for finding Markdown artifacts.*/
27+
remoteBase: '',
2928
};
3029

3130
// Called each time a URL is built from a file path/slug.
@@ -58,7 +57,7 @@
5857

5958
return path || '/';
6059
}
61-
// Called by getPageSlug() after normalizePathname() to build and return the slug
60+
// Called by getPageSlug() after normalizePathname() to build and return the slug.
6261
function buildSlugFromPath(pathname) {
6362
if (!pathname || pathname === '/') {
6463
return 'index';
@@ -111,11 +110,24 @@
111110
const outputs = config?.outputs || {};
112111
const files = outputs.files || {};
113112

114-
if (repository.host === 'github' && repository.org && repository.repo && repository.default_branch) {
113+
if (
114+
repository.host === 'github' &&
115+
repository.org &&
116+
repository.repo &&
117+
repository.default_branch
118+
) {
115119
const pagesDir = stripSlashes(files.pages_dir || 'pages');
116-
const fallbackArtifacts = joinUrl(stripSlashes(outputs.public_root || 'ai'), pagesDir);
117-
const artifactsPath = stripSlashes(repository.ai_artifacts_path || fallbackArtifacts);
118-
return joinUrl(`https://raw.githubusercontent.com/${repository.org}/${repository.repo}/${repository.default_branch}`, artifactsPath);
120+
const fallbackArtifacts = joinUrl(
121+
stripSlashes(outputs.public_root || 'ai'),
122+
pagesDir
123+
);
124+
const artifactsPath = stripSlashes(
125+
repository.ai_artifacts_path || fallbackArtifacts
126+
);
127+
return joinUrl(
128+
`https://raw.githubusercontent.com/${repository.org}/${repository.repo}/${repository.default_branch}`,
129+
artifactsPath
130+
);
119131
}
120132

121133
return '';
@@ -132,9 +144,7 @@
132144
return state.configPromise;
133145
}
134146

135-
const configUrl = CONFIG_URL;
136-
137-
state.configPromise = fetch(configUrl, { credentials: 'omit' })
147+
state.configPromise = fetch(CONFIG_URL, { credentials: 'omit' })
138148
.then((response) => {
139149
if (!response.ok) {
140150
throw new Error(`Failed to load config (${response.status})`);
@@ -164,7 +174,7 @@
164174
return loadConfig();
165175
}
166176

167-
// Trigger config preload without blocking UI
177+
// Trigger config preload without blocking UI.
168178
ready();
169179

170180
// Compute the local site-relative path for Markdown artifacts (`/ai/pages/...`).
@@ -201,7 +211,9 @@
201211
if (localBase) {
202212
candidates.push(joinUrl(localBase, `${normalizedSlug}.md`));
203213
if (state.siteBase) {
204-
candidates.push(joinUrl(state.siteBase, joinUrl(localBase, `${normalizedSlug}.md`)));
214+
candidates.push(
215+
joinUrl(state.siteBase, joinUrl(localBase, `${normalizedSlug}.md`))
216+
);
205217
}
206218
}
207219

@@ -292,29 +304,22 @@
292304
}
293305

294306
function trackCopyEvent(eventType, contentLength) {
295-
sendAnalytics(
296-
'copy_to_llm',
297-
{
298-
event_label: eventType,
299-
value: contentLength,
300-
}
301-
);
307+
sendAnalytics('copy_to_llm', {
308+
event_label: eventType,
309+
value: contentLength,
310+
});
302311
}
303312

304313
// Lightweight GA event wrapper for button clicks (download/open/chat etc.).
305314
function trackButtonClick(eventType) {
306-
sendAnalytics(
307-
'copy_to_llm_click',
308-
{
309-
event_label: eventType,
310-
}
311-
);
315+
sendAnalytics('copy_to_llm_click', {
316+
event_label: eventType,
317+
});
312318
}
313319

314320
// ---------- Page helpers ----------
315-
316-
// If fetching Markdown fails, we fall back to scraping the rendered HTML content.
317-
// '.md-content__inner .md-typeset' is the default class for <article> elements
321+
322+
/* If fetching Markdown fails, we fall back to scraping the rendered HTML content: '.md-content__inner .md-typeset' is the default class for <article> elements. */
318323
function getFallbackPageContent() {
319324
const articleContent = document.querySelector(
320325
'.md-content__inner .md-typeset'
@@ -647,7 +652,7 @@
647652
return;
648653
}
649654

650-
await ready();
655+
await ready();
651656
const action = item.dataset.action;
652657
const slug = getPageSlug();
653658

0 commit comments

Comments
 (0)