Skip to content

Commit 0618f9a

Browse files
Merge pull request #494 from kinde-oss/docs/copy-to-md-for-ai-llm
Added "Open with ChatGPT" and "Copy for AI" buttons to doc pages
2 parents ef16001 + 1a9adaa commit 0618f9a

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

src/components/ContributionGuides.astro

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
---
22
import {Icon} from "astro-icon/components";
3+
import { getEntry } from 'astro:content';
4+
5+
// Get the current page path and always use docs.kinde.com as the hostname
6+
const currentPath = Astro.url.pathname;
7+
const productionUrl = `https://docs.kinde.com${currentPath}`;
8+
const chatGptUrl = `https://chat.openai.com/?q=${encodeURIComponent(`Can you summarize this page: ${productionUrl}`)}`;
9+
10+
// Get the raw markdown content
11+
let rawMarkdown = '';
12+
try {
13+
// Convert the path to the content ID (remove leading slash and trailing slash)
14+
const contentId = currentPath.replace(/^\/|\/$/g, '');
15+
const entry = await getEntry('docs', contentId);
16+
if (entry) {
17+
const body = await entry.body;
18+
rawMarkdown = body || '';
19+
}
20+
} catch (error) {
21+
console.error('Error getting markdown content:', error);
22+
}
323
---
424

525
<aside
@@ -12,4 +32,125 @@ import {Icon} from "astro-icon/components";
1232
<Icon name="book" size={16} />
1333
<h3 class="text-sm font-medium dark:!font-normal">Contribute</h3>
1434
</a>
35+
36+
<a
37+
href={chatGptUrl}
38+
target="_blank"
39+
rel="noopener noreferrer"
40+
class={`flex items-center justify-start gap-1 mt-4 text-kinde-grey-900 hover:opacity-90 dark:text-white plausible-event-name=Open+with+ChatGPT plausible-event-url=${currentPath}`}
41+
>
42+
<Icon name="talk-1" size={16} />
43+
<h3 class="text-sm font-medium dark:!font-normal">Open with ChatGPT</h3>
44+
</a>
45+
46+
<button
47+
id="copy-markdown-button"
48+
class={`flex items-center justify-start gap-1 mt-4 text-kinde-grey-900 hover:opacity-90 dark:text-white bg-transparent border-none cursor-pointer w-full text-left p-0 plausible-event-name=Copy+for+AI plausible-event-url=${currentPath}`}
49+
data-markdown={rawMarkdown}
50+
>
51+
<Icon name="sparkle" size={16} />
52+
<h3 class="text-sm font-medium dark:!font-normal">Copy for AI</h3>
53+
</button>
1554
</aside>
55+
56+
<script>
57+
document.getElementById('copy-markdown-button')?.addEventListener('click', async function() {
58+
try {
59+
// Get the raw markdown from the data attribute
60+
const rawMarkdown = this.getAttribute('data-markdown');
61+
if (!rawMarkdown) {
62+
console.error('No markdown content available');
63+
return;
64+
}
65+
66+
// Get the page title
67+
const titleElement = document.querySelector('h1');
68+
const title = titleElement?.textContent || 'Kinde Documentation';
69+
70+
// Create markdown content with metadata
71+
const markdownContent = `# ${title}
72+
73+
Source: ${window.location.href}
74+
75+
${rawMarkdown}
76+
77+
---
78+
*This content was copied from the Kinde documentation page. You can use this markdown content with any AI LLM to get help with this documentation.*`;
79+
80+
// Copy to clipboard
81+
await navigator.clipboard.writeText(markdownContent);
82+
83+
// Show success notification
84+
const button = this as HTMLButtonElement;
85+
const titleElement2 = button.querySelector('h3');
86+
if (titleElement2) {
87+
const originalText = titleElement2.textContent;
88+
titleElement2.textContent = 'Markdown copied!';
89+
90+
// Show a notification
91+
const notification = document.createElement('div');
92+
notification.style.cssText = `
93+
position: fixed;
94+
top: 20px;
95+
right: 20px;
96+
background: #10b981;
97+
color: white;
98+
padding: 12px 16px;
99+
border-radius: 8px;
100+
font-size: 14px;
101+
z-index: 10000;
102+
max-width: 300px;
103+
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
104+
`;
105+
notification.innerHTML = `
106+
<div style="font-weight: 600; margin-bottom: 4px;">Markdown copied to clipboard!</div>
107+
<div style="font-size: 12px;">You can now paste this into any AI LLM.</div>
108+
`;
109+
document.body.appendChild(notification);
110+
111+
// Remove notification after 4 seconds
112+
setTimeout(() => {
113+
if (notification.parentNode) {
114+
notification.parentNode.removeChild(notification);
115+
}
116+
}, 4000);
117+
118+
// Reset button text after 3 seconds
119+
setTimeout(() => {
120+
if (titleElement2) {
121+
titleElement2.textContent = originalText;
122+
}
123+
}, 3000);
124+
}
125+
126+
} catch (error) {
127+
console.error('Error copying markdown:', error);
128+
// Show error notification
129+
const notification = document.createElement('div');
130+
notification.style.cssText = `
131+
position: fixed;
132+
top: 20px;
133+
right: 20px;
134+
background: #ef4444;
135+
color: white;
136+
padding: 12px 16px;
137+
border-radius: 8px;
138+
font-size: 14px;
139+
z-index: 10000;
140+
max-width: 300px;
141+
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
142+
`;
143+
notification.innerHTML = `
144+
<div style="font-weight: 600; margin-bottom: 4px;">Error copying markdown</div>
145+
<div style="font-size: 12px;">Please try again.</div>
146+
`;
147+
document.body.appendChild(notification);
148+
149+
setTimeout(() => {
150+
if (notification.parentNode) {
151+
notification.parentNode.removeChild(notification);
152+
}
153+
}, 4000);
154+
}
155+
});
156+
</script>

0 commit comments

Comments
 (0)