@seoengine.ai/next-llm-ready 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +858 -0
- package/dist/api/index.cjs +624 -0
- package/dist/api/index.cjs.map +1 -0
- package/dist/api/index.d.cts +295 -0
- package/dist/api/index.d.ts +295 -0
- package/dist/api/index.js +613 -0
- package/dist/api/index.js.map +1 -0
- package/dist/hooks/index.cjs +619 -0
- package/dist/hooks/index.cjs.map +1 -0
- package/dist/hooks/index.d.cts +257 -0
- package/dist/hooks/index.d.ts +257 -0
- package/dist/hooks/index.js +611 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/index.cjs +1609 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +850 -0
- package/dist/index.d.ts +850 -0
- package/dist/index.js +1576 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.cjs +398 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +134 -0
- package/dist/server/index.d.ts +134 -0
- package/dist/server/index.js +390 -0
- package/dist/server/index.js.map +1 -0
- package/dist/styles.css +855 -0
- package/package.json +118 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1609 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
var server = require('next/server');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
10
|
+
|
|
11
|
+
// src/components/CopyButton.tsx
|
|
12
|
+
|
|
13
|
+
// src/utils/clipboard.ts
|
|
14
|
+
async function copyToClipboard(text) {
|
|
15
|
+
if (navigator.clipboard && typeof navigator.clipboard.writeText === "function") {
|
|
16
|
+
try {
|
|
17
|
+
await navigator.clipboard.writeText(text);
|
|
18
|
+
return true;
|
|
19
|
+
} catch (err) {
|
|
20
|
+
console.warn("Clipboard API failed, using fallback:", err);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return fallbackCopyToClipboard(text);
|
|
24
|
+
}
|
|
25
|
+
function fallbackCopyToClipboard(text) {
|
|
26
|
+
const textarea = document.createElement("textarea");
|
|
27
|
+
textarea.value = text;
|
|
28
|
+
textarea.style.position = "fixed";
|
|
29
|
+
textarea.style.top = "-9999px";
|
|
30
|
+
textarea.style.left = "-9999px";
|
|
31
|
+
textarea.style.opacity = "0";
|
|
32
|
+
textarea.style.pointerEvents = "none";
|
|
33
|
+
textarea.style.fontSize = "16px";
|
|
34
|
+
document.body.appendChild(textarea);
|
|
35
|
+
textarea.focus();
|
|
36
|
+
textarea.select();
|
|
37
|
+
try {
|
|
38
|
+
const successful = document.execCommand("copy");
|
|
39
|
+
document.body.removeChild(textarea);
|
|
40
|
+
return successful;
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error("Fallback copy failed:", err);
|
|
43
|
+
document.body.removeChild(textarea);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/utils/download.ts
|
|
49
|
+
var defaultOptions = {
|
|
50
|
+
filename: "content",
|
|
51
|
+
extension: "md",
|
|
52
|
+
includeMetadata: false
|
|
53
|
+
};
|
|
54
|
+
function downloadAsFile(content, options = {}) {
|
|
55
|
+
const opts = { ...defaultOptions, ...options };
|
|
56
|
+
const filename = sanitizeFilename(opts.filename || "content");
|
|
57
|
+
const fullFilename = `${filename}.${opts.extension}`;
|
|
58
|
+
const mimeType = opts.extension === "md" ? "text/markdown" : "text/plain";
|
|
59
|
+
const blob = new Blob([content], { type: `${mimeType};charset=utf-8` });
|
|
60
|
+
const url = URL.createObjectURL(blob);
|
|
61
|
+
const link = document.createElement("a");
|
|
62
|
+
link.href = url;
|
|
63
|
+
link.download = fullFilename;
|
|
64
|
+
document.body.appendChild(link);
|
|
65
|
+
link.click();
|
|
66
|
+
document.body.removeChild(link);
|
|
67
|
+
URL.revokeObjectURL(url);
|
|
68
|
+
}
|
|
69
|
+
function sanitizeFilename(name) {
|
|
70
|
+
return name.toLowerCase().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "").substring(0, 100);
|
|
71
|
+
}
|
|
72
|
+
function generateFilename(title) {
|
|
73
|
+
const base = sanitizeFilename(title);
|
|
74
|
+
const date = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
75
|
+
return `${base}-${date}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/utils/html-to-markdown.ts
|
|
79
|
+
var defaultOptions2 = {
|
|
80
|
+
preserveLineBreaks: true,
|
|
81
|
+
convertImages: true,
|
|
82
|
+
convertLinks: true,
|
|
83
|
+
stripScripts: true,
|
|
84
|
+
customHandlers: {}
|
|
85
|
+
};
|
|
86
|
+
function htmlToMarkdown(html, options = {}) {
|
|
87
|
+
const opts = { ...defaultOptions2, ...options };
|
|
88
|
+
if (typeof window === "undefined") {
|
|
89
|
+
return serverSideConvert(html, opts);
|
|
90
|
+
}
|
|
91
|
+
const doc = new DOMParser().parseFromString(html, "text/html");
|
|
92
|
+
return convertNode(doc.body, opts);
|
|
93
|
+
}
|
|
94
|
+
function serverSideConvert(html, opts) {
|
|
95
|
+
let markdown = html;
|
|
96
|
+
if (opts.stripScripts) {
|
|
97
|
+
markdown = markdown.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
|
|
98
|
+
markdown = markdown.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "");
|
|
99
|
+
}
|
|
100
|
+
markdown = markdown.replace(/<h1[^>]*>(.*?)<\/h1>/gi, "\n# $1\n");
|
|
101
|
+
markdown = markdown.replace(/<h2[^>]*>(.*?)<\/h2>/gi, "\n## $1\n");
|
|
102
|
+
markdown = markdown.replace(/<h3[^>]*>(.*?)<\/h3>/gi, "\n### $1\n");
|
|
103
|
+
markdown = markdown.replace(/<h4[^>]*>(.*?)<\/h4>/gi, "\n#### $1\n");
|
|
104
|
+
markdown = markdown.replace(/<h5[^>]*>(.*?)<\/h5>/gi, "\n##### $1\n");
|
|
105
|
+
markdown = markdown.replace(/<h6[^>]*>(.*?)<\/h6>/gi, "\n###### $1\n");
|
|
106
|
+
markdown = markdown.replace(/<(strong|b)[^>]*>(.*?)<\/\1>/gi, "**$2**");
|
|
107
|
+
markdown = markdown.replace(/<(em|i)[^>]*>(.*?)<\/\1>/gi, "*$2*");
|
|
108
|
+
if (opts.convertLinks) {
|
|
109
|
+
markdown = markdown.replace(/<a[^>]*href=["']([^"']*)["'][^>]*>(.*?)<\/a>/gi, "[$2]($1)");
|
|
110
|
+
}
|
|
111
|
+
if (opts.convertImages) {
|
|
112
|
+
markdown = markdown.replace(
|
|
113
|
+
/<img[^>]*src=["']([^"']*)["'][^>]*alt=["']([^"']*)["'][^>]*\/?>/gi,
|
|
114
|
+
""
|
|
115
|
+
);
|
|
116
|
+
markdown = markdown.replace(
|
|
117
|
+
/<img[^>]*alt=["']([^"']*)["'][^>]*src=["']([^"']*)["'][^>]*\/?>/gi,
|
|
118
|
+
""
|
|
119
|
+
);
|
|
120
|
+
markdown = markdown.replace(/<img[^>]*src=["']([^"']*)["'][^>]*\/?>/gi, "");
|
|
121
|
+
}
|
|
122
|
+
markdown = markdown.replace(/<ul[^>]*>/gi, "\n");
|
|
123
|
+
markdown = markdown.replace(/<\/ul>/gi, "\n");
|
|
124
|
+
markdown = markdown.replace(/<ol[^>]*>/gi, "\n");
|
|
125
|
+
markdown = markdown.replace(/<\/ol>/gi, "\n");
|
|
126
|
+
markdown = markdown.replace(/<li[^>]*>(.*?)<\/li>/gi, "- $1\n");
|
|
127
|
+
markdown = markdown.replace(/<blockquote[^>]*>(.*?)<\/blockquote>/gis, (_, content) => {
|
|
128
|
+
return "\n> " + content.trim().replace(/\n/g, "\n> ") + "\n";
|
|
129
|
+
});
|
|
130
|
+
markdown = markdown.replace(/<pre[^>]*><code[^>]*>(.*?)<\/code><\/pre>/gis, "\n```\n$1\n```\n");
|
|
131
|
+
markdown = markdown.replace(/<code[^>]*>(.*?)<\/code>/gi, "`$1`");
|
|
132
|
+
markdown = markdown.replace(/<p[^>]*>(.*?)<\/p>/gis, "\n$1\n");
|
|
133
|
+
markdown = markdown.replace(/<br\s*\/?>/gi, "\n");
|
|
134
|
+
markdown = markdown.replace(/<hr[^>]*\/?>/gi, "\n---\n");
|
|
135
|
+
markdown = markdown.replace(/<[^>]+>/g, "");
|
|
136
|
+
markdown = decodeHTMLEntities(markdown);
|
|
137
|
+
markdown = markdown.replace(/\n{3,}/g, "\n\n");
|
|
138
|
+
markdown = markdown.trim();
|
|
139
|
+
return markdown;
|
|
140
|
+
}
|
|
141
|
+
function convertNode(node, opts) {
|
|
142
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
143
|
+
return node.textContent || "";
|
|
144
|
+
}
|
|
145
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
148
|
+
const element = node;
|
|
149
|
+
const tagName = element.tagName.toLowerCase();
|
|
150
|
+
if (opts.customHandlers?.[tagName]) {
|
|
151
|
+
return opts.customHandlers[tagName](element);
|
|
152
|
+
}
|
|
153
|
+
if (opts.stripScripts && (tagName === "script" || tagName === "style")) {
|
|
154
|
+
return "";
|
|
155
|
+
}
|
|
156
|
+
const childContent = Array.from(element.childNodes).map((child) => convertNode(child, opts)).join("");
|
|
157
|
+
switch (tagName) {
|
|
158
|
+
case "h1":
|
|
159
|
+
return `
|
|
160
|
+
# ${childContent.trim()}
|
|
161
|
+
`;
|
|
162
|
+
case "h2":
|
|
163
|
+
return `
|
|
164
|
+
## ${childContent.trim()}
|
|
165
|
+
`;
|
|
166
|
+
case "h3":
|
|
167
|
+
return `
|
|
168
|
+
### ${childContent.trim()}
|
|
169
|
+
`;
|
|
170
|
+
case "h4":
|
|
171
|
+
return `
|
|
172
|
+
#### ${childContent.trim()}
|
|
173
|
+
`;
|
|
174
|
+
case "h5":
|
|
175
|
+
return `
|
|
176
|
+
##### ${childContent.trim()}
|
|
177
|
+
`;
|
|
178
|
+
case "h6":
|
|
179
|
+
return `
|
|
180
|
+
###### ${childContent.trim()}
|
|
181
|
+
`;
|
|
182
|
+
case "p":
|
|
183
|
+
return `
|
|
184
|
+
${childContent.trim()}
|
|
185
|
+
`;
|
|
186
|
+
case "br":
|
|
187
|
+
return opts.preserveLineBreaks ? "\n" : " ";
|
|
188
|
+
case "strong":
|
|
189
|
+
case "b":
|
|
190
|
+
return `**${childContent}**`;
|
|
191
|
+
case "em":
|
|
192
|
+
case "i":
|
|
193
|
+
return `*${childContent}*`;
|
|
194
|
+
case "u":
|
|
195
|
+
return `_${childContent}_`;
|
|
196
|
+
case "s":
|
|
197
|
+
case "strike":
|
|
198
|
+
case "del":
|
|
199
|
+
return `~~${childContent}~~`;
|
|
200
|
+
case "a":
|
|
201
|
+
if (opts.convertLinks) {
|
|
202
|
+
const href = element.getAttribute("href") || "";
|
|
203
|
+
return `[${childContent}](${href})`;
|
|
204
|
+
}
|
|
205
|
+
return childContent;
|
|
206
|
+
case "img":
|
|
207
|
+
if (opts.convertImages) {
|
|
208
|
+
const src = element.getAttribute("src") || "";
|
|
209
|
+
const alt = element.getAttribute("alt") || "";
|
|
210
|
+
return ``;
|
|
211
|
+
}
|
|
212
|
+
return "";
|
|
213
|
+
case "ul":
|
|
214
|
+
return `
|
|
215
|
+
${childContent}
|
|
216
|
+
`;
|
|
217
|
+
case "ol":
|
|
218
|
+
return `
|
|
219
|
+
${childContent}
|
|
220
|
+
`;
|
|
221
|
+
case "li":
|
|
222
|
+
return `- ${childContent.trim()}
|
|
223
|
+
`;
|
|
224
|
+
case "blockquote":
|
|
225
|
+
return `
|
|
226
|
+
> ${childContent.trim().replace(/\n/g, "\n> ")}
|
|
227
|
+
`;
|
|
228
|
+
case "pre":
|
|
229
|
+
const codeElement = element.querySelector("code");
|
|
230
|
+
const lang = codeElement?.className.match(/language-(\w+)/)?.[1] || "";
|
|
231
|
+
const code = codeElement?.textContent || childContent;
|
|
232
|
+
return `
|
|
233
|
+
\`\`\`${lang}
|
|
234
|
+
${code.trim()}
|
|
235
|
+
\`\`\`
|
|
236
|
+
`;
|
|
237
|
+
case "code":
|
|
238
|
+
if (element.parentElement?.tagName.toLowerCase() === "pre") {
|
|
239
|
+
return childContent;
|
|
240
|
+
}
|
|
241
|
+
return `\`${childContent}\``;
|
|
242
|
+
case "hr":
|
|
243
|
+
return "\n---\n";
|
|
244
|
+
case "table":
|
|
245
|
+
return convertTable(element);
|
|
246
|
+
case "div":
|
|
247
|
+
case "section":
|
|
248
|
+
case "article":
|
|
249
|
+
case "main":
|
|
250
|
+
case "aside":
|
|
251
|
+
case "header":
|
|
252
|
+
case "footer":
|
|
253
|
+
case "nav":
|
|
254
|
+
return childContent;
|
|
255
|
+
default:
|
|
256
|
+
return childContent;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function convertTable(table) {
|
|
260
|
+
const rows = table.querySelectorAll("tr");
|
|
261
|
+
if (rows.length === 0) return "";
|
|
262
|
+
let markdown = "\n";
|
|
263
|
+
let headerProcessed = false;
|
|
264
|
+
rows.forEach((row, index) => {
|
|
265
|
+
const cells = row.querySelectorAll("th, td");
|
|
266
|
+
const rowContent = Array.from(cells).map((cell) => cell.textContent?.trim() || "").join(" | ");
|
|
267
|
+
markdown += `| ${rowContent} |
|
|
268
|
+
`;
|
|
269
|
+
if (!headerProcessed && (row.querySelector("th") || index === 0)) {
|
|
270
|
+
const separator = Array.from(cells).map(() => "---").join(" | ");
|
|
271
|
+
markdown += `| ${separator} |
|
|
272
|
+
`;
|
|
273
|
+
headerProcessed = true;
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
return markdown + "\n";
|
|
277
|
+
}
|
|
278
|
+
function decodeHTMLEntities(text) {
|
|
279
|
+
const entities = {
|
|
280
|
+
"&": "&",
|
|
281
|
+
"<": "<",
|
|
282
|
+
">": ">",
|
|
283
|
+
""": '"',
|
|
284
|
+
"'": "'",
|
|
285
|
+
"'": "'",
|
|
286
|
+
" ": " ",
|
|
287
|
+
"—": "\u2014",
|
|
288
|
+
"–": "\u2013",
|
|
289
|
+
"…": "\u2026",
|
|
290
|
+
"©": "\xA9",
|
|
291
|
+
"®": "\xAE",
|
|
292
|
+
"™": "\u2122"
|
|
293
|
+
};
|
|
294
|
+
let result = text;
|
|
295
|
+
for (const [entity, char] of Object.entries(entities)) {
|
|
296
|
+
result = result.replace(new RegExp(entity, "g"), char);
|
|
297
|
+
}
|
|
298
|
+
result = result.replace(/&#(\d+);/g, (_, num) => String.fromCharCode(parseInt(num, 10)));
|
|
299
|
+
result = result.replace(
|
|
300
|
+
/&#x([0-9a-f]+);/gi,
|
|
301
|
+
(_, hex) => String.fromCharCode(parseInt(hex, 16))
|
|
302
|
+
);
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
function countWords(text) {
|
|
306
|
+
return text.replace(/[^\w\s]/g, "").split(/\s+/).filter((word) => word.length > 0).length;
|
|
307
|
+
}
|
|
308
|
+
function calculateReadingTime(text, wordsPerMinute = 200) {
|
|
309
|
+
const words = countWords(text);
|
|
310
|
+
return Math.ceil(words / wordsPerMinute);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// src/hooks/use-llm-copy.ts
|
|
314
|
+
function generateMarkdownFromContent(content) {
|
|
315
|
+
if (typeof content === "string") {
|
|
316
|
+
return content;
|
|
317
|
+
}
|
|
318
|
+
const parts = [];
|
|
319
|
+
if (content.promptPrefix?.trim()) {
|
|
320
|
+
parts.push(content.promptPrefix.trim());
|
|
321
|
+
parts.push("");
|
|
322
|
+
}
|
|
323
|
+
parts.push(`# ${content.title}`);
|
|
324
|
+
parts.push("");
|
|
325
|
+
if (content.excerpt) {
|
|
326
|
+
parts.push(`> ${content.excerpt}`);
|
|
327
|
+
parts.push("");
|
|
328
|
+
}
|
|
329
|
+
parts.push("---");
|
|
330
|
+
parts.push(`- **Source**: ${content.url}`);
|
|
331
|
+
if (content.date) {
|
|
332
|
+
parts.push(`- **Date**: ${content.date}`);
|
|
333
|
+
}
|
|
334
|
+
if (content.author) {
|
|
335
|
+
parts.push(`- **Author**: ${content.author}`);
|
|
336
|
+
}
|
|
337
|
+
if (content.categories?.length) {
|
|
338
|
+
parts.push(`- **Categories**: ${content.categories.join(", ")}`);
|
|
339
|
+
}
|
|
340
|
+
if (content.tags?.length) {
|
|
341
|
+
parts.push(`- **Tags**: ${content.tags.join(", ")}`);
|
|
342
|
+
}
|
|
343
|
+
parts.push("---");
|
|
344
|
+
parts.push("");
|
|
345
|
+
if (content.content) {
|
|
346
|
+
const contentMarkdown = content.content.includes("<") && content.content.includes(">") ? htmlToMarkdown(content.content) : content.content;
|
|
347
|
+
parts.push(contentMarkdown);
|
|
348
|
+
}
|
|
349
|
+
return parts.join("\n").trim();
|
|
350
|
+
}
|
|
351
|
+
function useLLMCopy(options) {
|
|
352
|
+
const [isCopying, setIsCopying] = React.useState(false);
|
|
353
|
+
const [isSuccess, setIsSuccess] = React.useState(false);
|
|
354
|
+
const [error, setError] = React.useState(null);
|
|
355
|
+
const markdown = React.useMemo(() => {
|
|
356
|
+
return generateMarkdownFromContent(options.content);
|
|
357
|
+
}, [options.content]);
|
|
358
|
+
const trackEvent = React.useCallback(
|
|
359
|
+
(action) => {
|
|
360
|
+
if (options.onAnalytics) {
|
|
361
|
+
const contentId = typeof options.content === "string" ? void 0 : options.content.url;
|
|
362
|
+
options.onAnalytics({
|
|
363
|
+
action,
|
|
364
|
+
contentId,
|
|
365
|
+
url: typeof window !== "undefined" ? window.location.href : void 0,
|
|
366
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
[options]
|
|
371
|
+
);
|
|
372
|
+
const copy = React.useCallback(async () => {
|
|
373
|
+
setIsCopying(true);
|
|
374
|
+
setError(null);
|
|
375
|
+
try {
|
|
376
|
+
const success = await copyToClipboard(markdown);
|
|
377
|
+
if (success) {
|
|
378
|
+
setIsSuccess(true);
|
|
379
|
+
trackEvent("copy");
|
|
380
|
+
options.onSuccess?.("copy");
|
|
381
|
+
setTimeout(() => setIsSuccess(false), 2e3);
|
|
382
|
+
return true;
|
|
383
|
+
} else {
|
|
384
|
+
throw new Error("Failed to copy to clipboard");
|
|
385
|
+
}
|
|
386
|
+
} catch (err) {
|
|
387
|
+
const error2 = err instanceof Error ? err : new Error("Copy failed");
|
|
388
|
+
setError(error2);
|
|
389
|
+
options.onError?.(error2);
|
|
390
|
+
return false;
|
|
391
|
+
} finally {
|
|
392
|
+
setIsCopying(false);
|
|
393
|
+
}
|
|
394
|
+
}, [markdown, options, trackEvent]);
|
|
395
|
+
const view = React.useCallback(() => {
|
|
396
|
+
trackEvent("view");
|
|
397
|
+
options.onSuccess?.("view");
|
|
398
|
+
return markdown;
|
|
399
|
+
}, [markdown, options, trackEvent]);
|
|
400
|
+
const download = React.useCallback(
|
|
401
|
+
(filename) => {
|
|
402
|
+
const title = typeof options.content === "string" ? "content" : options.content.title;
|
|
403
|
+
downloadAsFile(markdown, {
|
|
404
|
+
filename: filename || generateFilename(title),
|
|
405
|
+
extension: "md"
|
|
406
|
+
});
|
|
407
|
+
trackEvent("download");
|
|
408
|
+
options.onSuccess?.("download");
|
|
409
|
+
},
|
|
410
|
+
[markdown, options, trackEvent]
|
|
411
|
+
);
|
|
412
|
+
return {
|
|
413
|
+
copy,
|
|
414
|
+
view,
|
|
415
|
+
download,
|
|
416
|
+
markdown,
|
|
417
|
+
isCopying,
|
|
418
|
+
isSuccess,
|
|
419
|
+
error
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function CopyButton({
|
|
423
|
+
content,
|
|
424
|
+
text = "Copy",
|
|
425
|
+
position = "inline",
|
|
426
|
+
keyboardShortcut = true,
|
|
427
|
+
className = "",
|
|
428
|
+
toastMessage = "Copied!",
|
|
429
|
+
toastDuration = 2e3,
|
|
430
|
+
onCopy,
|
|
431
|
+
onError,
|
|
432
|
+
onAnalytics,
|
|
433
|
+
style,
|
|
434
|
+
disabled = false
|
|
435
|
+
}) {
|
|
436
|
+
const [showToast, setShowToast] = React.useState(false);
|
|
437
|
+
const { copy, isCopying, isSuccess, error } = useLLMCopy({
|
|
438
|
+
content,
|
|
439
|
+
onSuccess: (action) => {
|
|
440
|
+
setShowToast(true);
|
|
441
|
+
setTimeout(() => setShowToast(false), toastDuration);
|
|
442
|
+
onCopy?.(action);
|
|
443
|
+
},
|
|
444
|
+
onError,
|
|
445
|
+
onAnalytics
|
|
446
|
+
});
|
|
447
|
+
React__default.default.useEffect(() => {
|
|
448
|
+
if (!keyboardShortcut) return;
|
|
449
|
+
const handleKeyDown = (e) => {
|
|
450
|
+
const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
|
|
451
|
+
const modKey = isMac ? e.metaKey : e.ctrlKey;
|
|
452
|
+
if (modKey && e.shiftKey && e.key.toLowerCase() === "c") {
|
|
453
|
+
e.preventDefault();
|
|
454
|
+
copy();
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
458
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
459
|
+
}, [keyboardShortcut, copy]);
|
|
460
|
+
const buttonClasses = [
|
|
461
|
+
"llm-ready-copy-btn",
|
|
462
|
+
`llm-ready-position-${position}`,
|
|
463
|
+
isCopying && "llm-ready-copying",
|
|
464
|
+
isSuccess && "llm-ready-success",
|
|
465
|
+
error && "llm-ready-error",
|
|
466
|
+
className
|
|
467
|
+
].filter(Boolean).join(" ");
|
|
468
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-container", style, children: [
|
|
469
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
470
|
+
"button",
|
|
471
|
+
{
|
|
472
|
+
type: "button",
|
|
473
|
+
className: buttonClasses,
|
|
474
|
+
onClick: copy,
|
|
475
|
+
disabled: disabled || isCopying,
|
|
476
|
+
"aria-label": text,
|
|
477
|
+
title: keyboardShortcut ? `${text} (${navigator.platform.includes("Mac") ? "\u2318" : "Ctrl"}+Shift+C)` : text,
|
|
478
|
+
children: [
|
|
479
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
480
|
+
"svg",
|
|
481
|
+
{
|
|
482
|
+
width: "16",
|
|
483
|
+
height: "16",
|
|
484
|
+
viewBox: "0 0 24 24",
|
|
485
|
+
fill: "none",
|
|
486
|
+
stroke: "currentColor",
|
|
487
|
+
strokeWidth: "2",
|
|
488
|
+
strokeLinecap: "round",
|
|
489
|
+
strokeLinejoin: "round",
|
|
490
|
+
className: "llm-ready-icon",
|
|
491
|
+
children: isSuccess ? /* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "20 6 9 17 4 12" }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
492
|
+
/* @__PURE__ */ jsxRuntime.jsx("rect", { x: "9", y: "9", width: "13", height: "13", rx: "2", ry: "2" }),
|
|
493
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" })
|
|
494
|
+
] })
|
|
495
|
+
}
|
|
496
|
+
),
|
|
497
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "llm-ready-btn-text", children: isSuccess ? toastMessage : text })
|
|
498
|
+
]
|
|
499
|
+
}
|
|
500
|
+
),
|
|
501
|
+
showToast && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "llm-ready-toast llm-ready-toast--visible", role: "status", "aria-live": "polite", children: toastMessage })
|
|
502
|
+
] });
|
|
503
|
+
}
|
|
504
|
+
var defaultMenuItems = [
|
|
505
|
+
{
|
|
506
|
+
id: "copy",
|
|
507
|
+
label: "Copy to clipboard",
|
|
508
|
+
action: "copy",
|
|
509
|
+
icon: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
|
|
510
|
+
/* @__PURE__ */ jsxRuntime.jsx("rect", { x: "9", y: "9", width: "13", height: "13", rx: "2", ry: "2" }),
|
|
511
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" })
|
|
512
|
+
] }),
|
|
513
|
+
shortcut: "\u2318+Shift+C"
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
id: "view",
|
|
517
|
+
label: "View markdown",
|
|
518
|
+
action: "view",
|
|
519
|
+
icon: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
|
|
520
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" }),
|
|
521
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "12", cy: "12", r: "3" })
|
|
522
|
+
] })
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
id: "download",
|
|
526
|
+
label: "Download .md file",
|
|
527
|
+
action: "download",
|
|
528
|
+
icon: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
|
|
529
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" }),
|
|
530
|
+
/* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "7 10 12 15 17 10" }),
|
|
531
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "12", y1: "15", x2: "12", y2: "3" })
|
|
532
|
+
] })
|
|
533
|
+
}
|
|
534
|
+
];
|
|
535
|
+
function CopyDropdown({
|
|
536
|
+
content,
|
|
537
|
+
text = "Copy",
|
|
538
|
+
menuItems = defaultMenuItems,
|
|
539
|
+
position = "inline",
|
|
540
|
+
keyboardShortcut = true,
|
|
541
|
+
className = "",
|
|
542
|
+
toastMessage = "Copied!",
|
|
543
|
+
toastDuration = 2e3,
|
|
544
|
+
onCopy,
|
|
545
|
+
onError,
|
|
546
|
+
onAnalytics,
|
|
547
|
+
style,
|
|
548
|
+
disabled = false
|
|
549
|
+
}) {
|
|
550
|
+
const [isOpen, setIsOpen] = React.useState(false);
|
|
551
|
+
const [showToast, setShowToast] = React.useState(false);
|
|
552
|
+
const [showModal, setShowModal] = React.useState(false);
|
|
553
|
+
const containerRef = React.useRef(null);
|
|
554
|
+
const dropdownRef = React.useRef(null);
|
|
555
|
+
const { copy, view, download, markdown, isCopying, isSuccess } = useLLMCopy({
|
|
556
|
+
content,
|
|
557
|
+
onSuccess: (action) => {
|
|
558
|
+
if (action === "copy") {
|
|
559
|
+
setShowToast(true);
|
|
560
|
+
setTimeout(() => setShowToast(false), toastDuration);
|
|
561
|
+
}
|
|
562
|
+
onCopy?.(action);
|
|
563
|
+
},
|
|
564
|
+
onError,
|
|
565
|
+
onAnalytics
|
|
566
|
+
});
|
|
567
|
+
React.useEffect(() => {
|
|
568
|
+
const handleClickOutside = (e) => {
|
|
569
|
+
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
570
|
+
setIsOpen(false);
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
document.addEventListener("click", handleClickOutside);
|
|
574
|
+
return () => document.removeEventListener("click", handleClickOutside);
|
|
575
|
+
}, []);
|
|
576
|
+
React.useEffect(() => {
|
|
577
|
+
if (!keyboardShortcut) return;
|
|
578
|
+
const handleKeyDown = (e) => {
|
|
579
|
+
if (e.key === "Escape" && isOpen) {
|
|
580
|
+
setIsOpen(false);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
|
|
584
|
+
const modKey = isMac ? e.metaKey : e.ctrlKey;
|
|
585
|
+
if (modKey && e.shiftKey && e.key.toLowerCase() === "c") {
|
|
586
|
+
e.preventDefault();
|
|
587
|
+
copy();
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
591
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
592
|
+
}, [keyboardShortcut, copy, isOpen]);
|
|
593
|
+
const handleItemClick = React.useCallback(
|
|
594
|
+
(item) => {
|
|
595
|
+
setIsOpen(false);
|
|
596
|
+
if (typeof item.action === "function") {
|
|
597
|
+
item.action();
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
switch (item.action) {
|
|
601
|
+
case "copy":
|
|
602
|
+
copy();
|
|
603
|
+
break;
|
|
604
|
+
case "view":
|
|
605
|
+
view();
|
|
606
|
+
setShowModal(true);
|
|
607
|
+
break;
|
|
608
|
+
case "download":
|
|
609
|
+
download();
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
[copy, view, download]
|
|
614
|
+
);
|
|
615
|
+
const handleDropdownKeyDown = React.useCallback(
|
|
616
|
+
(e) => {
|
|
617
|
+
if (!isOpen) return;
|
|
618
|
+
const items = dropdownRef.current?.querySelectorAll("button");
|
|
619
|
+
if (!items) return;
|
|
620
|
+
const currentIndex = Array.from(items).indexOf(document.activeElement);
|
|
621
|
+
switch (e.key) {
|
|
622
|
+
case "ArrowDown":
|
|
623
|
+
e.preventDefault();
|
|
624
|
+
const nextIndex = currentIndex < items.length - 1 ? currentIndex + 1 : 0;
|
|
625
|
+
items[nextIndex]?.focus();
|
|
626
|
+
break;
|
|
627
|
+
case "ArrowUp":
|
|
628
|
+
e.preventDefault();
|
|
629
|
+
const prevIndex = currentIndex > 0 ? currentIndex - 1 : items.length - 1;
|
|
630
|
+
items[prevIndex]?.focus();
|
|
631
|
+
break;
|
|
632
|
+
case "Escape":
|
|
633
|
+
setIsOpen(false);
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
[isOpen]
|
|
638
|
+
);
|
|
639
|
+
const containerClasses = [
|
|
640
|
+
"llm-ready-container",
|
|
641
|
+
`llm-ready-position-${position}`,
|
|
642
|
+
className
|
|
643
|
+
].filter(Boolean).join(" ");
|
|
644
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
645
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { ref: containerRef, className: containerClasses, style, children: [
|
|
646
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-btn-group", children: [
|
|
647
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
648
|
+
"button",
|
|
649
|
+
{
|
|
650
|
+
type: "button",
|
|
651
|
+
className: `llm-ready-copy-btn ${isCopying ? "llm-ready-copying" : ""} ${isSuccess ? "llm-ready-success" : ""}`,
|
|
652
|
+
onClick: copy,
|
|
653
|
+
disabled: disabled || isCopying,
|
|
654
|
+
"aria-label": text,
|
|
655
|
+
children: [
|
|
656
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
657
|
+
"svg",
|
|
658
|
+
{
|
|
659
|
+
width: "16",
|
|
660
|
+
height: "16",
|
|
661
|
+
viewBox: "0 0 24 24",
|
|
662
|
+
fill: "none",
|
|
663
|
+
stroke: "currentColor",
|
|
664
|
+
strokeWidth: "2",
|
|
665
|
+
className: "llm-ready-icon",
|
|
666
|
+
children: isSuccess ? /* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "20 6 9 17 4 12" }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
667
|
+
/* @__PURE__ */ jsxRuntime.jsx("rect", { x: "9", y: "9", width: "13", height: "13", rx: "2", ry: "2" }),
|
|
668
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" })
|
|
669
|
+
] })
|
|
670
|
+
}
|
|
671
|
+
),
|
|
672
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "llm-ready-btn-text", children: isSuccess ? toastMessage : text })
|
|
673
|
+
]
|
|
674
|
+
}
|
|
675
|
+
),
|
|
676
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
677
|
+
"button",
|
|
678
|
+
{
|
|
679
|
+
type: "button",
|
|
680
|
+
className: "llm-ready-dropdown-btn",
|
|
681
|
+
onClick: () => setIsOpen(!isOpen),
|
|
682
|
+
"aria-expanded": isOpen,
|
|
683
|
+
"aria-haspopup": "menu",
|
|
684
|
+
"aria-label": "More options",
|
|
685
|
+
disabled,
|
|
686
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
687
|
+
"svg",
|
|
688
|
+
{
|
|
689
|
+
width: "16",
|
|
690
|
+
height: "16",
|
|
691
|
+
viewBox: "0 0 24 24",
|
|
692
|
+
fill: "none",
|
|
693
|
+
stroke: "currentColor",
|
|
694
|
+
strokeWidth: "2",
|
|
695
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "6 9 12 15 18 9" })
|
|
696
|
+
}
|
|
697
|
+
)
|
|
698
|
+
}
|
|
699
|
+
)
|
|
700
|
+
] }),
|
|
701
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
702
|
+
"div",
|
|
703
|
+
{
|
|
704
|
+
ref: dropdownRef,
|
|
705
|
+
className: `llm-ready-dropdown ${isOpen ? "llm-ready-dropdown--open" : ""}`,
|
|
706
|
+
role: "menu",
|
|
707
|
+
"aria-hidden": !isOpen,
|
|
708
|
+
onKeyDown: handleDropdownKeyDown,
|
|
709
|
+
children: menuItems.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
710
|
+
"button",
|
|
711
|
+
{
|
|
712
|
+
type: "button",
|
|
713
|
+
className: "llm-ready-dropdown-item",
|
|
714
|
+
role: "menuitem",
|
|
715
|
+
onClick: () => handleItemClick(item),
|
|
716
|
+
tabIndex: isOpen ? 0 : -1,
|
|
717
|
+
children: [
|
|
718
|
+
item.icon && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "llm-ready-dropdown-icon", children: item.icon }),
|
|
719
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "llm-ready-dropdown-label", children: item.label }),
|
|
720
|
+
item.shortcut && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "llm-ready-dropdown-shortcut", children: item.shortcut })
|
|
721
|
+
]
|
|
722
|
+
},
|
|
723
|
+
item.id
|
|
724
|
+
))
|
|
725
|
+
}
|
|
726
|
+
),
|
|
727
|
+
showToast && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "llm-ready-toast llm-ready-toast--visible", role: "status", children: toastMessage })
|
|
728
|
+
] }),
|
|
729
|
+
showModal && /* @__PURE__ */ jsxRuntime.jsx(
|
|
730
|
+
MarkdownModal,
|
|
731
|
+
{
|
|
732
|
+
content: markdown,
|
|
733
|
+
onClose: () => setShowModal(false),
|
|
734
|
+
onCopy: copy,
|
|
735
|
+
onDownload: download
|
|
736
|
+
}
|
|
737
|
+
)
|
|
738
|
+
] });
|
|
739
|
+
}
|
|
740
|
+
function MarkdownModal({
|
|
741
|
+
content,
|
|
742
|
+
onClose,
|
|
743
|
+
onCopy,
|
|
744
|
+
onDownload
|
|
745
|
+
}) {
|
|
746
|
+
const modalRef = React.useRef(null);
|
|
747
|
+
React.useEffect(() => {
|
|
748
|
+
const handleKeyDown = (e) => {
|
|
749
|
+
if (e.key === "Escape") onClose();
|
|
750
|
+
};
|
|
751
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
752
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
753
|
+
}, [onClose]);
|
|
754
|
+
React.useEffect(() => {
|
|
755
|
+
const modal = modalRef.current;
|
|
756
|
+
if (!modal) return;
|
|
757
|
+
const focusable = modal.querySelectorAll(
|
|
758
|
+
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
759
|
+
);
|
|
760
|
+
const first = focusable[0];
|
|
761
|
+
const last = focusable[focusable.length - 1];
|
|
762
|
+
first?.focus();
|
|
763
|
+
const handleTab = (e) => {
|
|
764
|
+
if (e.key !== "Tab") return;
|
|
765
|
+
if (e.shiftKey && document.activeElement === first) {
|
|
766
|
+
e.preventDefault();
|
|
767
|
+
last?.focus();
|
|
768
|
+
} else if (!e.shiftKey && document.activeElement === last) {
|
|
769
|
+
e.preventDefault();
|
|
770
|
+
first?.focus();
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
modal.addEventListener("keydown", handleTab);
|
|
774
|
+
return () => modal.removeEventListener("keydown", handleTab);
|
|
775
|
+
}, []);
|
|
776
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-modal", role: "dialog", "aria-modal": "true", "aria-label": "Markdown content", children: [
|
|
777
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "llm-ready-modal-backdrop", onClick: onClose }),
|
|
778
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { ref: modalRef, className: "llm-ready-modal-content", children: [
|
|
779
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-modal-header", children: [
|
|
780
|
+
/* @__PURE__ */ jsxRuntime.jsx("h2", { className: "llm-ready-modal-title", children: "Markdown Content" }),
|
|
781
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
782
|
+
"button",
|
|
783
|
+
{
|
|
784
|
+
type: "button",
|
|
785
|
+
className: "llm-ready-modal-close",
|
|
786
|
+
onClick: onClose,
|
|
787
|
+
"aria-label": "Close modal",
|
|
788
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
|
|
789
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "18", y1: "6", x2: "6", y2: "18" }),
|
|
790
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "6", y1: "6", x2: "18", y2: "18" })
|
|
791
|
+
] })
|
|
792
|
+
}
|
|
793
|
+
)
|
|
794
|
+
] }),
|
|
795
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "llm-ready-modal-body", children: /* @__PURE__ */ jsxRuntime.jsx("pre", { className: "llm-ready-markdown-content", children: content }) }),
|
|
796
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-modal-footer", children: [
|
|
797
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: "llm-ready-btn-secondary", onClick: onClose, children: "Close" }),
|
|
798
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: "llm-ready-btn-secondary", onClick: onDownload, children: "Download" }),
|
|
799
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: "llm-ready-btn-primary", onClick: onCopy, children: "Copy" })
|
|
800
|
+
] })
|
|
801
|
+
] })
|
|
802
|
+
] });
|
|
803
|
+
}
|
|
804
|
+
function useTOC(options) {
|
|
805
|
+
const {
|
|
806
|
+
contentRef,
|
|
807
|
+
levels = ["h2", "h3", "h4"],
|
|
808
|
+
rootMargin = "-100px 0px -80% 0px",
|
|
809
|
+
threshold = 0
|
|
810
|
+
} = options;
|
|
811
|
+
const [headings, setHeadings] = React.useState([]);
|
|
812
|
+
const [activeId, setActiveId] = React.useState(null);
|
|
813
|
+
React.useEffect(() => {
|
|
814
|
+
if (!contentRef?.current) return;
|
|
815
|
+
const container = contentRef.current;
|
|
816
|
+
const selector = levels.join(", ");
|
|
817
|
+
const elements = container.querySelectorAll(selector);
|
|
818
|
+
const extracted = [];
|
|
819
|
+
let index = 0;
|
|
820
|
+
elements.forEach((el) => {
|
|
821
|
+
const tagName = el.tagName.toLowerCase();
|
|
822
|
+
const level = parseInt(tagName.charAt(1), 10);
|
|
823
|
+
const text = el.textContent?.trim() || "";
|
|
824
|
+
let id = el.id;
|
|
825
|
+
if (!id) {
|
|
826
|
+
id = generateHeadingId(text, index);
|
|
827
|
+
el.id = id;
|
|
828
|
+
}
|
|
829
|
+
extracted.push({ id, text, level });
|
|
830
|
+
index++;
|
|
831
|
+
});
|
|
832
|
+
setHeadings(extracted);
|
|
833
|
+
}, [contentRef, levels]);
|
|
834
|
+
React.useEffect(() => {
|
|
835
|
+
if (!contentRef?.current || headings.length === 0) return;
|
|
836
|
+
const container = contentRef.current;
|
|
837
|
+
const observerOptions = {
|
|
838
|
+
root: null,
|
|
839
|
+
rootMargin,
|
|
840
|
+
threshold
|
|
841
|
+
};
|
|
842
|
+
const observer = new IntersectionObserver((entries) => {
|
|
843
|
+
entries.forEach((entry) => {
|
|
844
|
+
if (entry.isIntersecting) {
|
|
845
|
+
setActiveId(entry.target.id);
|
|
846
|
+
}
|
|
847
|
+
});
|
|
848
|
+
}, observerOptions);
|
|
849
|
+
headings.forEach((heading) => {
|
|
850
|
+
const element = container.querySelector(`#${CSS.escape(heading.id)}`);
|
|
851
|
+
if (element) {
|
|
852
|
+
observer.observe(element);
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
return () => observer.disconnect();
|
|
856
|
+
}, [contentRef, headings, rootMargin, threshold]);
|
|
857
|
+
const scrollTo = React.useCallback((id) => {
|
|
858
|
+
const element = document.getElementById(id);
|
|
859
|
+
if (element) {
|
|
860
|
+
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
861
|
+
setActiveId(id);
|
|
862
|
+
if (typeof window !== "undefined") {
|
|
863
|
+
window.history.pushState(null, "", `#${id}`);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
}, []);
|
|
867
|
+
return { headings, activeId, scrollTo };
|
|
868
|
+
}
|
|
869
|
+
function generateHeadingId(text, index) {
|
|
870
|
+
const slug = text.toLowerCase().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "").substring(0, 50);
|
|
871
|
+
return slug || `heading-${index}`;
|
|
872
|
+
}
|
|
873
|
+
function buildNestedTOC(headings) {
|
|
874
|
+
if (headings.length === 0) return [];
|
|
875
|
+
const result = [];
|
|
876
|
+
const stack = [];
|
|
877
|
+
for (const heading of headings) {
|
|
878
|
+
const item = { ...heading, children: [] };
|
|
879
|
+
while (stack.length > 0 && stack[stack.length - 1].level >= heading.level) {
|
|
880
|
+
stack.pop();
|
|
881
|
+
}
|
|
882
|
+
if (stack.length === 0) {
|
|
883
|
+
result.push(item);
|
|
884
|
+
} else {
|
|
885
|
+
const parent = stack[stack.length - 1].heading;
|
|
886
|
+
if (!parent.children) parent.children = [];
|
|
887
|
+
parent.children.push(item);
|
|
888
|
+
}
|
|
889
|
+
stack.push({ heading: item, level: heading.level });
|
|
890
|
+
}
|
|
891
|
+
return result;
|
|
892
|
+
}
|
|
893
|
+
function useTOCKeyboard(headings, activeId, scrollTo) {
|
|
894
|
+
React.useEffect(() => {
|
|
895
|
+
const handleKeyDown = (e) => {
|
|
896
|
+
if (headings.length === 0) return;
|
|
897
|
+
const currentIndex = headings.findIndex((h) => h.id === activeId);
|
|
898
|
+
if (e.key === "ArrowDown" && e.altKey) {
|
|
899
|
+
e.preventDefault();
|
|
900
|
+
const nextIndex = Math.min(currentIndex + 1, headings.length - 1);
|
|
901
|
+
scrollTo(headings[nextIndex].id);
|
|
902
|
+
} else if (e.key === "ArrowUp" && e.altKey) {
|
|
903
|
+
e.preventDefault();
|
|
904
|
+
const prevIndex = Math.max(currentIndex - 1, 0);
|
|
905
|
+
scrollTo(headings[prevIndex].id);
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
909
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
910
|
+
}, [headings, activeId, scrollTo]);
|
|
911
|
+
}
|
|
912
|
+
function TOC({
|
|
913
|
+
content,
|
|
914
|
+
contentRef,
|
|
915
|
+
headings: providedHeadings,
|
|
916
|
+
position = "right",
|
|
917
|
+
levels = ["h2", "h3", "h4"],
|
|
918
|
+
title = "On This Page",
|
|
919
|
+
sticky = true,
|
|
920
|
+
stickyOffset = 80,
|
|
921
|
+
className = "",
|
|
922
|
+
smoothScroll = true,
|
|
923
|
+
highlightActive = true,
|
|
924
|
+
collapsible = true,
|
|
925
|
+
defaultCollapsed = false,
|
|
926
|
+
style
|
|
927
|
+
}) {
|
|
928
|
+
const [isCollapsed, setIsCollapsed] = React.useState(defaultCollapsed);
|
|
929
|
+
const { headings: dynamicHeadings, activeId, scrollTo } = useTOC({
|
|
930
|
+
contentRef: contentRef || { current: null },
|
|
931
|
+
levels
|
|
932
|
+
});
|
|
933
|
+
const headings = providedHeadings || dynamicHeadings;
|
|
934
|
+
useTOCKeyboard(headings, activeId, scrollTo);
|
|
935
|
+
const handleClick = React.useCallback(
|
|
936
|
+
(e, id) => {
|
|
937
|
+
e.preventDefault();
|
|
938
|
+
if (smoothScroll) {
|
|
939
|
+
scrollTo(id);
|
|
940
|
+
} else {
|
|
941
|
+
const element = document.getElementById(id);
|
|
942
|
+
if (element) {
|
|
943
|
+
element.scrollIntoView();
|
|
944
|
+
window.history.pushState(null, "", `#${id}`);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
},
|
|
948
|
+
[scrollTo, smoothScroll]
|
|
949
|
+
);
|
|
950
|
+
if (headings.length === 0) {
|
|
951
|
+
return null;
|
|
952
|
+
}
|
|
953
|
+
const tocClasses = [
|
|
954
|
+
"llm-ready-toc",
|
|
955
|
+
`llm-ready-toc-${position}`,
|
|
956
|
+
sticky && "llm-ready-toc-sticky",
|
|
957
|
+
isCollapsed && "llm-ready-toc-collapsed",
|
|
958
|
+
className
|
|
959
|
+
].filter(Boolean).join(" ");
|
|
960
|
+
const tocStyle = {
|
|
961
|
+
...style,
|
|
962
|
+
...sticky && { top: stickyOffset }
|
|
963
|
+
};
|
|
964
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("nav", { className: tocClasses, style: tocStyle, "aria-label": "Table of contents", children: [
|
|
965
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-toc-header", children: [
|
|
966
|
+
/* @__PURE__ */ jsxRuntime.jsx("h3", { className: "llm-ready-toc-title", children: title }),
|
|
967
|
+
collapsible && /* @__PURE__ */ jsxRuntime.jsx(
|
|
968
|
+
"button",
|
|
969
|
+
{
|
|
970
|
+
type: "button",
|
|
971
|
+
className: "llm-ready-toc-toggle",
|
|
972
|
+
onClick: () => setIsCollapsed(!isCollapsed),
|
|
973
|
+
"aria-expanded": !isCollapsed,
|
|
974
|
+
"aria-label": isCollapsed ? "Expand table of contents" : "Collapse table of contents",
|
|
975
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
976
|
+
"svg",
|
|
977
|
+
{
|
|
978
|
+
width: "16",
|
|
979
|
+
height: "16",
|
|
980
|
+
viewBox: "0 0 24 24",
|
|
981
|
+
fill: "none",
|
|
982
|
+
stroke: "currentColor",
|
|
983
|
+
strokeWidth: "2",
|
|
984
|
+
style: { transform: isCollapsed ? "rotate(180deg)" : "none" },
|
|
985
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "6 9 12 15 18 9" })
|
|
986
|
+
}
|
|
987
|
+
)
|
|
988
|
+
}
|
|
989
|
+
)
|
|
990
|
+
] }),
|
|
991
|
+
!isCollapsed && /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "llm-ready-toc-list", children: headings.map((heading) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
992
|
+
TOCItem,
|
|
993
|
+
{
|
|
994
|
+
heading,
|
|
995
|
+
activeId,
|
|
996
|
+
highlightActive,
|
|
997
|
+
onClick: handleClick
|
|
998
|
+
},
|
|
999
|
+
heading.id
|
|
1000
|
+
)) })
|
|
1001
|
+
] });
|
|
1002
|
+
}
|
|
1003
|
+
function TOCItem({
|
|
1004
|
+
heading,
|
|
1005
|
+
activeId,
|
|
1006
|
+
highlightActive,
|
|
1007
|
+
onClick
|
|
1008
|
+
}) {
|
|
1009
|
+
const isActive = highlightActive && heading.id === activeId;
|
|
1010
|
+
const itemClasses = [
|
|
1011
|
+
"llm-ready-toc-item",
|
|
1012
|
+
`llm-ready-toc-level-${heading.level}`,
|
|
1013
|
+
isActive && "llm-ready-toc-active"
|
|
1014
|
+
].filter(Boolean).join(" ");
|
|
1015
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("li", { className: itemClasses, children: [
|
|
1016
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1017
|
+
"a",
|
|
1018
|
+
{
|
|
1019
|
+
href: `#${heading.id}`,
|
|
1020
|
+
className: "llm-ready-toc-link",
|
|
1021
|
+
onClick: (e) => onClick(e, heading.id),
|
|
1022
|
+
"aria-current": isActive ? "location" : void 0,
|
|
1023
|
+
children: heading.text
|
|
1024
|
+
}
|
|
1025
|
+
),
|
|
1026
|
+
heading.children && heading.children.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "llm-ready-toc-nested", children: heading.children.map((child) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
1027
|
+
TOCItem,
|
|
1028
|
+
{
|
|
1029
|
+
heading: child,
|
|
1030
|
+
activeId,
|
|
1031
|
+
highlightActive,
|
|
1032
|
+
onClick
|
|
1033
|
+
},
|
|
1034
|
+
child.id
|
|
1035
|
+
)) })
|
|
1036
|
+
] });
|
|
1037
|
+
}
|
|
1038
|
+
function TOCMobile(props) {
|
|
1039
|
+
const [isOpen, setIsOpen] = React.useState(false);
|
|
1040
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
1041
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
1042
|
+
"button",
|
|
1043
|
+
{
|
|
1044
|
+
type: "button",
|
|
1045
|
+
className: "llm-ready-toc-mobile-toggle",
|
|
1046
|
+
onClick: () => setIsOpen(true),
|
|
1047
|
+
"aria-label": "Open table of contents",
|
|
1048
|
+
children: [
|
|
1049
|
+
/* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
|
|
1050
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "3", y1: "12", x2: "21", y2: "12" }),
|
|
1051
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "3", y1: "6", x2: "21", y2: "6" }),
|
|
1052
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "3", y1: "18", x2: "21", y2: "18" })
|
|
1053
|
+
] }),
|
|
1054
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: "Contents" })
|
|
1055
|
+
]
|
|
1056
|
+
}
|
|
1057
|
+
),
|
|
1058
|
+
isOpen && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-toc-mobile-panel", children: [
|
|
1059
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "llm-ready-toc-mobile-backdrop", onClick: () => setIsOpen(false) }),
|
|
1060
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-toc-mobile-content", children: [
|
|
1061
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "llm-ready-toc-mobile-header", children: [
|
|
1062
|
+
/* @__PURE__ */ jsxRuntime.jsx("h3", { children: "Contents" }),
|
|
1063
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1064
|
+
"button",
|
|
1065
|
+
{
|
|
1066
|
+
type: "button",
|
|
1067
|
+
className: "llm-ready-toc-mobile-close",
|
|
1068
|
+
onClick: () => setIsOpen(false),
|
|
1069
|
+
"aria-label": "Close",
|
|
1070
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
|
|
1071
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "18", y1: "6", x2: "6", y2: "18" }),
|
|
1072
|
+
/* @__PURE__ */ jsxRuntime.jsx("line", { x1: "6", y1: "6", x2: "18", y2: "18" })
|
|
1073
|
+
] })
|
|
1074
|
+
}
|
|
1075
|
+
)
|
|
1076
|
+
] }),
|
|
1077
|
+
/* @__PURE__ */ jsxRuntime.jsx(TOC, { ...props, sticky: false, collapsible: false })
|
|
1078
|
+
] })
|
|
1079
|
+
] })
|
|
1080
|
+
] });
|
|
1081
|
+
}
|
|
1082
|
+
function LLMBadge({
|
|
1083
|
+
text = "AI Ready",
|
|
1084
|
+
showTooltip = true,
|
|
1085
|
+
tooltipContent = "This content is optimized for AI assistants like ChatGPT and Claude",
|
|
1086
|
+
size = "md",
|
|
1087
|
+
className = ""
|
|
1088
|
+
}) {
|
|
1089
|
+
const [isTooltipVisible, setIsTooltipVisible] = React.useState(false);
|
|
1090
|
+
const badgeClasses = [
|
|
1091
|
+
"llm-ready-badge",
|
|
1092
|
+
`llm-ready-badge-${size}`,
|
|
1093
|
+
className
|
|
1094
|
+
].filter(Boolean).join(" ");
|
|
1095
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
1096
|
+
"div",
|
|
1097
|
+
{
|
|
1098
|
+
className: badgeClasses,
|
|
1099
|
+
onMouseEnter: () => showTooltip && setIsTooltipVisible(true),
|
|
1100
|
+
onMouseLeave: () => setIsTooltipVisible(false),
|
|
1101
|
+
onFocus: () => showTooltip && setIsTooltipVisible(true),
|
|
1102
|
+
onBlur: () => setIsTooltipVisible(false),
|
|
1103
|
+
tabIndex: showTooltip ? 0 : -1,
|
|
1104
|
+
role: showTooltip ? "tooltip" : void 0,
|
|
1105
|
+
"aria-describedby": showTooltip ? "llm-badge-tooltip" : void 0,
|
|
1106
|
+
children: [
|
|
1107
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
1108
|
+
"svg",
|
|
1109
|
+
{
|
|
1110
|
+
width: size === "sm" ? 12 : size === "lg" ? 18 : 14,
|
|
1111
|
+
height: size === "sm" ? 12 : size === "lg" ? 18 : 14,
|
|
1112
|
+
viewBox: "0 0 24 24",
|
|
1113
|
+
fill: "none",
|
|
1114
|
+
stroke: "currentColor",
|
|
1115
|
+
strokeWidth: "2",
|
|
1116
|
+
className: "llm-ready-badge-icon",
|
|
1117
|
+
children: [
|
|
1118
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "12", cy: "12", r: "3" }),
|
|
1119
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M12 2v4m0 12v4M2 12h4m12 0h4" }),
|
|
1120
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "m4.93 4.93 2.83 2.83m8.48 8.48 2.83 2.83m0-14.14-2.83 2.83m-8.48 8.48-2.83 2.83" })
|
|
1121
|
+
]
|
|
1122
|
+
}
|
|
1123
|
+
),
|
|
1124
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "llm-ready-badge-text", children: text }),
|
|
1125
|
+
showTooltip && isTooltipVisible && /* @__PURE__ */ jsxRuntime.jsx("div", { id: "llm-badge-tooltip", className: "llm-ready-badge-tooltip", role: "tooltip", children: tooltipContent })
|
|
1126
|
+
]
|
|
1127
|
+
}
|
|
1128
|
+
);
|
|
1129
|
+
}
|
|
1130
|
+
function useAnalytics(options = {}) {
|
|
1131
|
+
const {
|
|
1132
|
+
endpoint = "/api/llm-analytics",
|
|
1133
|
+
includeUrl = true,
|
|
1134
|
+
includeTimestamp = true,
|
|
1135
|
+
metadata = {}
|
|
1136
|
+
} = options;
|
|
1137
|
+
const pendingRef = React.useRef(/* @__PURE__ */ new Set());
|
|
1138
|
+
const track = React.useCallback(
|
|
1139
|
+
async (event) => {
|
|
1140
|
+
const key = `${event.action}-${event.contentId || ""}-${Date.now()}`;
|
|
1141
|
+
if (pendingRef.current.has(key)) {
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
pendingRef.current.add(key);
|
|
1145
|
+
try {
|
|
1146
|
+
const payload = {
|
|
1147
|
+
...event,
|
|
1148
|
+
...includeUrl && { url: event.url || window.location.href },
|
|
1149
|
+
...includeTimestamp && { timestamp: event.timestamp || (/* @__PURE__ */ new Date()).toISOString() },
|
|
1150
|
+
metadata: { ...metadata, ...event.metadata }
|
|
1151
|
+
};
|
|
1152
|
+
if (navigator.sendBeacon) {
|
|
1153
|
+
const blob = new Blob([JSON.stringify(payload)], { type: "application/json" });
|
|
1154
|
+
navigator.sendBeacon(endpoint, blob);
|
|
1155
|
+
} else {
|
|
1156
|
+
await fetch(endpoint, {
|
|
1157
|
+
method: "POST",
|
|
1158
|
+
headers: { "Content-Type": "application/json" },
|
|
1159
|
+
body: JSON.stringify(payload),
|
|
1160
|
+
keepalive: true
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1163
|
+
} catch (error) {
|
|
1164
|
+
console.error("Analytics tracking failed:", error);
|
|
1165
|
+
} finally {
|
|
1166
|
+
pendingRef.current.delete(key);
|
|
1167
|
+
}
|
|
1168
|
+
},
|
|
1169
|
+
[endpoint, includeUrl, includeTimestamp, metadata]
|
|
1170
|
+
);
|
|
1171
|
+
const trackCopy = React.useCallback(() => {
|
|
1172
|
+
return track({ action: "copy" });
|
|
1173
|
+
}, [track]);
|
|
1174
|
+
const trackView = React.useCallback(() => {
|
|
1175
|
+
return track({ action: "view" });
|
|
1176
|
+
}, [track]);
|
|
1177
|
+
const trackDownload = React.useCallback(() => {
|
|
1178
|
+
return track({ action: "download" });
|
|
1179
|
+
}, [track]);
|
|
1180
|
+
return { track, trackCopy, trackView, trackDownload };
|
|
1181
|
+
}
|
|
1182
|
+
function createAnalyticsTracker(endpoint) {
|
|
1183
|
+
return async function trackEvent(action, contentId, metadata) {
|
|
1184
|
+
const payload = {
|
|
1185
|
+
action,
|
|
1186
|
+
contentId,
|
|
1187
|
+
url: typeof window !== "undefined" ? window.location.href : void 0,
|
|
1188
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1189
|
+
metadata
|
|
1190
|
+
};
|
|
1191
|
+
try {
|
|
1192
|
+
if (typeof navigator !== "undefined" && navigator.sendBeacon) {
|
|
1193
|
+
const blob = new Blob([JSON.stringify(payload)], { type: "application/json" });
|
|
1194
|
+
navigator.sendBeacon(endpoint, blob);
|
|
1195
|
+
} else if (typeof fetch !== "undefined") {
|
|
1196
|
+
await fetch(endpoint, {
|
|
1197
|
+
method: "POST",
|
|
1198
|
+
headers: { "Content-Type": "application/json" },
|
|
1199
|
+
body: JSON.stringify(payload),
|
|
1200
|
+
keepalive: true
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
} catch (error) {
|
|
1204
|
+
console.error("Analytics tracking failed:", error);
|
|
1205
|
+
}
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
// src/server/generate-markdown.ts
|
|
1210
|
+
function generateMarkdown(content) {
|
|
1211
|
+
const parts = [];
|
|
1212
|
+
if (content.promptPrefix?.trim()) {
|
|
1213
|
+
parts.push(content.promptPrefix.trim());
|
|
1214
|
+
parts.push("");
|
|
1215
|
+
}
|
|
1216
|
+
parts.push(`# ${content.title}`);
|
|
1217
|
+
parts.push("");
|
|
1218
|
+
if (content.excerpt) {
|
|
1219
|
+
parts.push(`> ${content.excerpt}`);
|
|
1220
|
+
parts.push("");
|
|
1221
|
+
}
|
|
1222
|
+
parts.push("---");
|
|
1223
|
+
parts.push(`- **Source**: ${content.url}`);
|
|
1224
|
+
if (content.date) {
|
|
1225
|
+
parts.push(`- **Date**: ${content.date}`);
|
|
1226
|
+
}
|
|
1227
|
+
if (content.modifiedDate) {
|
|
1228
|
+
parts.push(`- **Modified**: ${content.modifiedDate}`);
|
|
1229
|
+
}
|
|
1230
|
+
if (content.author) {
|
|
1231
|
+
parts.push(`- **Author**: ${content.author}`);
|
|
1232
|
+
}
|
|
1233
|
+
if (content.categories?.length) {
|
|
1234
|
+
parts.push(`- **Categories**: ${content.categories.join(", ")}`);
|
|
1235
|
+
}
|
|
1236
|
+
if (content.tags?.length) {
|
|
1237
|
+
parts.push(`- **Tags**: ${content.tags.join(", ")}`);
|
|
1238
|
+
}
|
|
1239
|
+
if (content.readingTime) {
|
|
1240
|
+
parts.push(`- **Reading Time**: ${content.readingTime} min`);
|
|
1241
|
+
}
|
|
1242
|
+
parts.push("---");
|
|
1243
|
+
parts.push("");
|
|
1244
|
+
if (content.content) {
|
|
1245
|
+
const contentMarkdown = isHTML(content.content) ? htmlToMarkdown(content.content) : content.content;
|
|
1246
|
+
parts.push(contentMarkdown);
|
|
1247
|
+
}
|
|
1248
|
+
const markdown = parts.join("\n").trim();
|
|
1249
|
+
const wordCount = countWords(markdown);
|
|
1250
|
+
const readingTime = calculateReadingTime(markdown);
|
|
1251
|
+
const headings = extractMarkdownHeadings(markdown);
|
|
1252
|
+
return {
|
|
1253
|
+
markdown,
|
|
1254
|
+
wordCount,
|
|
1255
|
+
readingTime,
|
|
1256
|
+
headings
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
function generateMarkdownString(content) {
|
|
1260
|
+
return generateMarkdown(content).markdown;
|
|
1261
|
+
}
|
|
1262
|
+
function isHTML(str) {
|
|
1263
|
+
return /<[a-z][\s\S]*>/i.test(str);
|
|
1264
|
+
}
|
|
1265
|
+
function extractMarkdownHeadings(markdown) {
|
|
1266
|
+
const headings = [];
|
|
1267
|
+
const lines = markdown.split("\n");
|
|
1268
|
+
let index = 0;
|
|
1269
|
+
for (const line of lines) {
|
|
1270
|
+
const match = line.match(/^(#{1,6})\s+(.+)$/);
|
|
1271
|
+
if (match) {
|
|
1272
|
+
const level = match[1].length;
|
|
1273
|
+
const text = match[2].trim();
|
|
1274
|
+
const id = generateSlug(text, index);
|
|
1275
|
+
headings.push({ id, text, level });
|
|
1276
|
+
index++;
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
return headings;
|
|
1280
|
+
}
|
|
1281
|
+
function generateSlug(text, index) {
|
|
1282
|
+
const slug = text.toLowerCase().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
1283
|
+
return slug || `heading-${index}`;
|
|
1284
|
+
}
|
|
1285
|
+
function contentToPlainText(content) {
|
|
1286
|
+
const markdown = generateMarkdownString(content);
|
|
1287
|
+
return markdown.replace(/^#+\s+/gm, "").replace(/\*\*([^*]+)\*\*/g, "$1").replace(/\*([^*]+)\*/g, "$1").replace(/`([^`]+)`/g, "$1").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/!\[([^\]]*)\]\([^)]+\)/g, "").replace(/^[-*+]\s+/gm, "").replace(/^>\s+/gm, "").replace(/^---$/gm, "").replace(/\n{2,}/g, "\n\n").trim();
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// src/server/generate-llms-txt.ts
|
|
1291
|
+
function generateLLMsTxt(config) {
|
|
1292
|
+
const parts = [];
|
|
1293
|
+
parts.push(`# ${config.siteName}`);
|
|
1294
|
+
parts.push("");
|
|
1295
|
+
if (config.siteDescription) {
|
|
1296
|
+
parts.push(`> ${config.siteDescription}`);
|
|
1297
|
+
parts.push("");
|
|
1298
|
+
}
|
|
1299
|
+
if (config.headerText) {
|
|
1300
|
+
parts.push(config.headerText);
|
|
1301
|
+
} else {
|
|
1302
|
+
parts.push("This file provides LLM-friendly access to the content on this website.");
|
|
1303
|
+
}
|
|
1304
|
+
parts.push("");
|
|
1305
|
+
parts.push("---");
|
|
1306
|
+
parts.push(`- **Site URL**: ${config.siteUrl}`);
|
|
1307
|
+
parts.push("- **Format**: Append `?llm=1` to any page URL to get markdown");
|
|
1308
|
+
parts.push("---");
|
|
1309
|
+
parts.push("");
|
|
1310
|
+
if (config.content.length > 0) {
|
|
1311
|
+
parts.push("## Available Content");
|
|
1312
|
+
parts.push("");
|
|
1313
|
+
for (const item of config.content) {
|
|
1314
|
+
const llmUrl = appendQueryParam(item.url, "llm", "1");
|
|
1315
|
+
parts.push(`### [${item.title}](${llmUrl})`);
|
|
1316
|
+
if (item.type) {
|
|
1317
|
+
parts.push(`- **Type**: ${item.type}`);
|
|
1318
|
+
}
|
|
1319
|
+
if (item.date) {
|
|
1320
|
+
parts.push(`- **Date**: ${item.date}`);
|
|
1321
|
+
}
|
|
1322
|
+
if (item.description) {
|
|
1323
|
+
parts.push(`- **Description**: ${item.description}`);
|
|
1324
|
+
}
|
|
1325
|
+
parts.push(`- **Markdown URL**: ${llmUrl}`);
|
|
1326
|
+
parts.push("");
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
parts.push("---");
|
|
1330
|
+
parts.push("");
|
|
1331
|
+
if (config.footerText) {
|
|
1332
|
+
parts.push(config.footerText);
|
|
1333
|
+
} else {
|
|
1334
|
+
parts.push("*Generated by [next-llm-ready](https://seoengine.ai) - Make your content AI-ready*");
|
|
1335
|
+
}
|
|
1336
|
+
return parts.join("\n");
|
|
1337
|
+
}
|
|
1338
|
+
function appendQueryParam(url, key, value) {
|
|
1339
|
+
const separator = url.includes("?") ? "&" : "?";
|
|
1340
|
+
return `${url}${separator}${key}=${value}`;
|
|
1341
|
+
}
|
|
1342
|
+
function createLLMsTxtHandler(options) {
|
|
1343
|
+
return async function handler(request) {
|
|
1344
|
+
try {
|
|
1345
|
+
const siteConfig = await options.getSiteConfig();
|
|
1346
|
+
const content = await options.getContent();
|
|
1347
|
+
const config = {
|
|
1348
|
+
siteName: siteConfig.siteName,
|
|
1349
|
+
siteDescription: siteConfig.siteDescription,
|
|
1350
|
+
siteUrl: siteConfig.siteUrl,
|
|
1351
|
+
content,
|
|
1352
|
+
headerText: options.headerText,
|
|
1353
|
+
footerText: options.footerText
|
|
1354
|
+
};
|
|
1355
|
+
const llmsTxt = generateLLMsTxt(config);
|
|
1356
|
+
return new server.NextResponse(llmsTxt, {
|
|
1357
|
+
status: 200,
|
|
1358
|
+
headers: {
|
|
1359
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
1360
|
+
"Cache-Control": options.cacheControl || "public, max-age=3600",
|
|
1361
|
+
"X-Robots-Tag": "noindex"
|
|
1362
|
+
}
|
|
1363
|
+
});
|
|
1364
|
+
} catch (error) {
|
|
1365
|
+
console.error("Error generating llms.txt:", error);
|
|
1366
|
+
return new server.NextResponse("Error generating llms.txt", { status: 500 });
|
|
1367
|
+
}
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
function createLLMsTxtPageHandler(options) {
|
|
1371
|
+
return async function handler(req, res) {
|
|
1372
|
+
if (req.method !== "GET") {
|
|
1373
|
+
res.status(405).end("Method Not Allowed");
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
try {
|
|
1377
|
+
const siteConfig = await options.getSiteConfig();
|
|
1378
|
+
const content = await options.getContent();
|
|
1379
|
+
const config = {
|
|
1380
|
+
siteName: siteConfig.siteName,
|
|
1381
|
+
siteDescription: siteConfig.siteDescription,
|
|
1382
|
+
siteUrl: siteConfig.siteUrl,
|
|
1383
|
+
content,
|
|
1384
|
+
headerText: options.headerText,
|
|
1385
|
+
footerText: options.footerText
|
|
1386
|
+
};
|
|
1387
|
+
const llmsTxt = generateLLMsTxt(config);
|
|
1388
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
1389
|
+
res.setHeader("Cache-Control", options.cacheControl || "public, max-age=3600");
|
|
1390
|
+
res.setHeader("X-Robots-Tag", "noindex");
|
|
1391
|
+
res.status(200).end(llmsTxt);
|
|
1392
|
+
} catch (error) {
|
|
1393
|
+
console.error("Error generating llms.txt:", error);
|
|
1394
|
+
res.status(500).end("Error generating llms.txt");
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
}
|
|
1398
|
+
function withLLMParam(options) {
|
|
1399
|
+
return async function middleware(request) {
|
|
1400
|
+
const url = new URL(request.url);
|
|
1401
|
+
const llmParam = url.searchParams.get("llm");
|
|
1402
|
+
if (llmParam !== "1") {
|
|
1403
|
+
return void 0;
|
|
1404
|
+
}
|
|
1405
|
+
try {
|
|
1406
|
+
const pathname = url.pathname;
|
|
1407
|
+
const content = await options.getContent(pathname);
|
|
1408
|
+
if (!content) {
|
|
1409
|
+
return new server.NextResponse("Content not found", { status: 404 });
|
|
1410
|
+
}
|
|
1411
|
+
const markdown = generateMarkdownString(content);
|
|
1412
|
+
return new server.NextResponse(markdown, {
|
|
1413
|
+
status: 200,
|
|
1414
|
+
headers: {
|
|
1415
|
+
"Content-Type": "text/markdown; charset=utf-8",
|
|
1416
|
+
"Cache-Control": options.cacheControl || "public, max-age=3600",
|
|
1417
|
+
"X-Robots-Tag": "noindex",
|
|
1418
|
+
...options.cors && {
|
|
1419
|
+
"Access-Control-Allow-Origin": "*",
|
|
1420
|
+
"Access-Control-Allow-Methods": "GET"
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
});
|
|
1424
|
+
} catch (error) {
|
|
1425
|
+
console.error("Error generating markdown:", error);
|
|
1426
|
+
return new server.NextResponse("Error generating markdown", { status: 500 });
|
|
1427
|
+
}
|
|
1428
|
+
};
|
|
1429
|
+
}
|
|
1430
|
+
function createMarkdownHandler(options) {
|
|
1431
|
+
return async function handler(request, { params }) {
|
|
1432
|
+
try {
|
|
1433
|
+
const slug = params?.slug || "";
|
|
1434
|
+
const content = await options.getContent(slug);
|
|
1435
|
+
if (!content) {
|
|
1436
|
+
return new server.NextResponse("Content not found", {
|
|
1437
|
+
status: 404,
|
|
1438
|
+
headers: { "Content-Type": "text/plain" }
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
const markdown = generateMarkdownString(content);
|
|
1442
|
+
return new server.NextResponse(markdown, {
|
|
1443
|
+
status: 200,
|
|
1444
|
+
headers: {
|
|
1445
|
+
"Content-Type": "text/markdown; charset=utf-8",
|
|
1446
|
+
"Cache-Control": options.cacheControl || "public, max-age=3600",
|
|
1447
|
+
"X-Robots-Tag": "noindex",
|
|
1448
|
+
...options.cors && {
|
|
1449
|
+
"Access-Control-Allow-Origin": "*",
|
|
1450
|
+
"Access-Control-Allow-Methods": "GET"
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
});
|
|
1454
|
+
} catch (error) {
|
|
1455
|
+
console.error("Error generating markdown:", error);
|
|
1456
|
+
return new server.NextResponse("Error generating markdown", { status: 500 });
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
function hasLLMParam(request) {
|
|
1461
|
+
const url = new URL(request.url);
|
|
1462
|
+
return url.searchParams.get("llm") === "1";
|
|
1463
|
+
}
|
|
1464
|
+
function createInMemoryStorage() {
|
|
1465
|
+
const events = [];
|
|
1466
|
+
return {
|
|
1467
|
+
save: async (event) => {
|
|
1468
|
+
events.push(event);
|
|
1469
|
+
},
|
|
1470
|
+
getAll: async () => [...events],
|
|
1471
|
+
clear: async () => {
|
|
1472
|
+
events.length = 0;
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1475
|
+
}
|
|
1476
|
+
function createAnalyticsHandler(options) {
|
|
1477
|
+
const requestCounts = /* @__PURE__ */ new Map();
|
|
1478
|
+
return async function handler(request) {
|
|
1479
|
+
if (request.method === "OPTIONS") {
|
|
1480
|
+
return new server.NextResponse(null, {
|
|
1481
|
+
status: 204,
|
|
1482
|
+
headers: {
|
|
1483
|
+
"Access-Control-Allow-Origin": "*",
|
|
1484
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
1485
|
+
"Access-Control-Allow-Headers": "Content-Type"
|
|
1486
|
+
}
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
if (request.method !== "POST") {
|
|
1490
|
+
return new server.NextResponse("Method Not Allowed", { status: 405 });
|
|
1491
|
+
}
|
|
1492
|
+
if (options.rateLimit) {
|
|
1493
|
+
const clientIp = request.headers.get("x-forwarded-for") || "unknown";
|
|
1494
|
+
const now = Date.now();
|
|
1495
|
+
const windowMs = 6e4;
|
|
1496
|
+
const clientData = requestCounts.get(clientIp) || { count: 0, resetAt: now + windowMs };
|
|
1497
|
+
if (now > clientData.resetAt) {
|
|
1498
|
+
clientData.count = 0;
|
|
1499
|
+
clientData.resetAt = now + windowMs;
|
|
1500
|
+
}
|
|
1501
|
+
clientData.count++;
|
|
1502
|
+
requestCounts.set(clientIp, clientData);
|
|
1503
|
+
if (clientData.count > options.rateLimit) {
|
|
1504
|
+
return new server.NextResponse("Rate limit exceeded", {
|
|
1505
|
+
status: 429,
|
|
1506
|
+
headers: {
|
|
1507
|
+
"Retry-After": Math.ceil((clientData.resetAt - now) / 1e3).toString()
|
|
1508
|
+
}
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
try {
|
|
1513
|
+
const body = await request.json();
|
|
1514
|
+
const event = {
|
|
1515
|
+
action: body.action || "copy",
|
|
1516
|
+
contentId: body.contentId,
|
|
1517
|
+
url: body.url || request.headers.get("referer"),
|
|
1518
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1519
|
+
metadata: body.metadata
|
|
1520
|
+
};
|
|
1521
|
+
await options.storage.save(event);
|
|
1522
|
+
const headers = {
|
|
1523
|
+
"Content-Type": "application/json"
|
|
1524
|
+
};
|
|
1525
|
+
if (options.cors) {
|
|
1526
|
+
headers["Access-Control-Allow-Origin"] = "*";
|
|
1527
|
+
}
|
|
1528
|
+
return server.NextResponse.json({ success: true, event }, { headers });
|
|
1529
|
+
} catch (error) {
|
|
1530
|
+
console.error("Analytics tracking error:", error);
|
|
1531
|
+
return server.NextResponse.json({ success: false, error: "Failed to track event" }, { status: 500 });
|
|
1532
|
+
}
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
function createAnalyticsPageHandler(options) {
|
|
1536
|
+
return async function handler(req, res) {
|
|
1537
|
+
if (req.method !== "POST") {
|
|
1538
|
+
res.status(405).end("Method Not Allowed");
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
try {
|
|
1542
|
+
const body = req.body;
|
|
1543
|
+
const event = {
|
|
1544
|
+
action: body?.action || "copy",
|
|
1545
|
+
contentId: body?.contentId,
|
|
1546
|
+
url: body?.url || req.headers?.referer,
|
|
1547
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1548
|
+
metadata: body?.metadata
|
|
1549
|
+
};
|
|
1550
|
+
await options.storage.save(event);
|
|
1551
|
+
if (options.cors) {
|
|
1552
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
1553
|
+
}
|
|
1554
|
+
res.status(200).json({ success: true, event });
|
|
1555
|
+
} catch (error) {
|
|
1556
|
+
console.error("Analytics tracking error:", error);
|
|
1557
|
+
res.status(500).json({ success: false, error: "Failed to track event" });
|
|
1558
|
+
}
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
async function aggregateEvents(storage) {
|
|
1562
|
+
const events = await storage.getAll();
|
|
1563
|
+
const counts = {
|
|
1564
|
+
copy: 0,
|
|
1565
|
+
view: 0,
|
|
1566
|
+
download: 0
|
|
1567
|
+
};
|
|
1568
|
+
for (const event of events) {
|
|
1569
|
+
if (event.action in counts) {
|
|
1570
|
+
counts[event.action]++;
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
return counts;
|
|
1574
|
+
}
|
|
1575
|
+
async function getEventsForContent(storage, contentId) {
|
|
1576
|
+
const events = await storage.getAll();
|
|
1577
|
+
return events.filter((event) => event.contentId === contentId);
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
exports.CopyButton = CopyButton;
|
|
1581
|
+
exports.CopyDropdown = CopyDropdown;
|
|
1582
|
+
exports.LLMBadge = LLMBadge;
|
|
1583
|
+
exports.TOC = TOC;
|
|
1584
|
+
exports.TOCMobile = TOCMobile;
|
|
1585
|
+
exports.aggregateEvents = aggregateEvents;
|
|
1586
|
+
exports.buildNestedTOC = buildNestedTOC;
|
|
1587
|
+
exports.contentToPlainText = contentToPlainText;
|
|
1588
|
+
exports.copyToClipboard = copyToClipboard;
|
|
1589
|
+
exports.createAnalyticsHandler = createAnalyticsHandler;
|
|
1590
|
+
exports.createAnalyticsPageHandler = createAnalyticsPageHandler;
|
|
1591
|
+
exports.createAnalyticsTracker = createAnalyticsTracker;
|
|
1592
|
+
exports.createInMemoryStorage = createInMemoryStorage;
|
|
1593
|
+
exports.createLLMsTxtHandler = createLLMsTxtHandler;
|
|
1594
|
+
exports.createLLMsTxtPageHandler = createLLMsTxtPageHandler;
|
|
1595
|
+
exports.createMarkdownHandler = createMarkdownHandler;
|
|
1596
|
+
exports.downloadAsFile = downloadAsFile;
|
|
1597
|
+
exports.generateLLMsTxt = generateLLMsTxt;
|
|
1598
|
+
exports.generateMarkdown = generateMarkdown;
|
|
1599
|
+
exports.generateMarkdownString = generateMarkdownString;
|
|
1600
|
+
exports.getEventsForContent = getEventsForContent;
|
|
1601
|
+
exports.hasLLMParam = hasLLMParam;
|
|
1602
|
+
exports.htmlToMarkdown = htmlToMarkdown;
|
|
1603
|
+
exports.useAnalytics = useAnalytics;
|
|
1604
|
+
exports.useLLMCopy = useLLMCopy;
|
|
1605
|
+
exports.useTOC = useTOC;
|
|
1606
|
+
exports.useTOCKeyboard = useTOCKeyboard;
|
|
1607
|
+
exports.withLLMParam = withLLMParam;
|
|
1608
|
+
//# sourceMappingURL=index.cjs.map
|
|
1609
|
+
//# sourceMappingURL=index.cjs.map
|