|
| 1 | +module.exports = function aiChatButtonsPlugin(context, options) { |
| 2 | + return { |
| 3 | + name: 'docusaurus-plugin-ai-chat-buttons', |
| 4 | + |
| 5 | + injectHtmlTags() { |
| 6 | + return { |
| 7 | + headTags: [ |
| 8 | + { |
| 9 | + tagName: 'style', |
| 10 | + innerHTML: ` |
| 11 | + .ai-chat-buttons { |
| 12 | + position: fixed; |
| 13 | + bottom: 80px; |
| 14 | + right: 20px; |
| 15 | + display: flex; |
| 16 | + flex-direction: column; |
| 17 | + gap: 10px; |
| 18 | + z-index: 999; |
| 19 | + } |
| 20 | +
|
| 21 | + .ai-chat-button { |
| 22 | + display: flex; |
| 23 | + align-items: center; |
| 24 | + gap: 8px; |
| 25 | + padding: 10px 16px; |
| 26 | + background: var(--ifm-background-surface-color); |
| 27 | + border: 1px solid var(--ifm-color-emphasis-300); |
| 28 | + border-radius: 8px; |
| 29 | + color: var(--ifm-font-color-base); |
| 30 | + text-decoration: none; |
| 31 | + font-size: 14px; |
| 32 | + font-weight: 500; |
| 33 | + box-shadow: 0 2px 8px rgba(0,0,0,0.1); |
| 34 | + transition: all 0.2s ease; |
| 35 | + cursor: pointer; |
| 36 | + white-space: nowrap; |
| 37 | + } |
| 38 | +
|
| 39 | + .ai-chat-button:hover { |
| 40 | + transform: translateY(-2px); |
| 41 | + box-shadow: 0 4px 12px rgba(0,0,0,0.15); |
| 42 | + text-decoration: none; |
| 43 | + color: var(--ifm-font-color-base); |
| 44 | + border-color: var(--ifm-color-primary); |
| 45 | + } |
| 46 | +
|
| 47 | + .ai-chat-button svg { |
| 48 | + width: 18px; |
| 49 | + height: 18px; |
| 50 | + flex-shrink: 0; |
| 51 | + } |
| 52 | +
|
| 53 | + @media (max-width: 996px) { |
| 54 | + .ai-chat-buttons { |
| 55 | + bottom: 60px; |
| 56 | + right: 10px; |
| 57 | + } |
| 58 | +
|
| 59 | + .ai-chat-button { |
| 60 | + padding: 8px 12px; |
| 61 | + font-size: 13px; |
| 62 | + } |
| 63 | + } |
| 64 | + `, |
| 65 | + }, |
| 66 | + ], |
| 67 | + postBodyTags: [ |
| 68 | + { |
| 69 | + tagName: 'script', |
| 70 | + innerHTML: ` |
| 71 | + (function() { |
| 72 | + function createAIChatButtons() { |
| 73 | + if (document.querySelector('.ai-chat-buttons')) return; |
| 74 | +
|
| 75 | + const container = document.createElement('div'); |
| 76 | + container.className = 'ai-chat-buttons'; |
| 77 | +
|
| 78 | + const buttons = [ |
| 79 | + { |
| 80 | + name: 'ChatGPT', |
| 81 | + icon: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M22.282 9.821a5.985 5.985 0 0 0-.516-4.91 6.046 6.046 0 0 0-6.51-2.9A6.065 6.065 0 0 0 4.981 4.18a5.985 5.985 0 0 0-3.998 2.9 6.046 6.046 0 0 0 .743 7.097 5.98 5.98 0 0 0 .51 4.911 6.051 6.051 0 0 0 6.515 2.9A5.985 5.985 0 0 0 13.26 24a6.056 6.056 0 0 0 5.772-4.206 5.99 5.99 0 0 0 3.997-2.9 6.056 6.056 0 0 0-.747-7.073zM13.26 22.43a4.476 4.476 0 0 1-2.876-1.04l.141-.081 4.779-2.758a.795.795 0 0 0 .392-.681v-6.737l2.02 1.168a.071.071 0 0 1 .038.052v5.583a4.504 4.504 0 0 1-4.494 4.494zM3.6 18.304a4.47 4.47 0 0 1-.535-3.014l.142.085 4.783 2.759a.771.771 0 0 0 .78 0l5.843-3.369v2.332a.08.08 0 0 1-.033.062L9.74 19.95a4.5 4.5 0 0 1-6.14-1.646zM2.34 7.896a4.485 4.485 0 0 1 2.366-1.973V11.6a.766.766 0 0 0 .388.676l5.815 3.355-2.02 1.168a.076.076 0 0 1-.071 0l-4.83-2.786A4.504 4.504 0 0 1 2.34 7.896zm16.597 3.855l-5.833-3.387L15.119 7.2a.076.076 0 0 1 .071 0l4.83 2.791a4.494 4.494 0 0 1-.676 8.105v-5.678a.79.79 0 0 0-.407-.667zm2.01-3.023l-.141-.085-4.774-2.782a.776.776 0 0 0-.785 0L9.409 9.23V6.897a.066.066 0 0 1 .028-.061l4.83-2.787a4.5 4.5 0 0 1 6.68 4.66zm-12.64 4.135l-2.02-1.164a.08.08 0 0 1-.038-.057V6.075a4.5 4.5 0 0 1 7.375-3.453l-.142.08-4.778 2.758a.795.795 0 0 0-.393.681zm1.097-2.365l2.602-1.5 2.607 1.5v2.999l-2.597 1.5-2.607-1.5z"/></svg>', |
| 82 | + url: 'https://chatgpt.com' |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'Claude', |
| 86 | + icon: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M14.5 2.5h-5l-7 19h5l1.5-4h6l1.5 4h5l-7-19zm-3.5 11l1.5-4 1.5 4h-3z"/></svg>', |
| 87 | + url: 'https://claude.ai' |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'Gemini', |
| 91 | + icon: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2L2 7v10l10 5 10-5V7L12 2zm0 2.18L19.82 8 12 11.82 4.18 8 12 4.18zM4 9.47l7 3.5v7.85l-7-3.5V9.47zm16 0v7.85l-7 3.5v-7.85l7-3.5z"/></svg>', |
| 92 | + url: 'https://gemini.google.com' |
| 93 | + } |
| 94 | + ]; |
| 95 | +
|
| 96 | + buttons.forEach(btn => { |
| 97 | + const link = document.createElement('a'); |
| 98 | + link.className = 'ai-chat-button'; |
| 99 | + link.href = '#'; |
| 100 | + link.innerHTML = btn.icon + '<span>' + btn.name + '</span>'; |
| 101 | + link.onclick = function(e) { |
| 102 | + e.preventDefault(); |
| 103 | + const currentUrl = window.location.href; |
| 104 | + const pageTitle = document.title; |
| 105 | +
|
| 106 | + // Get the markdown source URL |
| 107 | + const markdownUrl = currentUrl.replace(/\\/$/, '') + '.md'; |
| 108 | +
|
| 109 | + let prompt = ''; |
| 110 | + if (btn.name === 'ChatGPT') { |
| 111 | + prompt = \`I'm reading this page: "\${pageTitle}" at \${currentUrl}. Can you help me understand it better?\`; |
| 112 | + window.open(\`\${btn.url}/?q=\${encodeURIComponent(prompt)}\`, '_blank'); |
| 113 | + } else if (btn.name === 'Claude') { |
| 114 | + prompt = \`I'm reading this documentation page: "\${pageTitle}" at \${currentUrl}. Can you help me understand it better?\`; |
| 115 | + window.open(\`\${btn.url}/new?q=\${encodeURIComponent(prompt)}\`, '_blank'); |
| 116 | + } else if (btn.name === 'Gemini') { |
| 117 | + prompt = \`I'm reading this page: "\${pageTitle}" at \${currentUrl}. Can you help me understand it better?\`; |
| 118 | + window.open(\`\${btn.url}/app?q=\${encodeURIComponent(prompt)}\`, '_blank'); |
| 119 | + } |
| 120 | + }; |
| 121 | + container.appendChild(link); |
| 122 | + }); |
| 123 | +
|
| 124 | + document.body.appendChild(container); |
| 125 | + } |
| 126 | +
|
| 127 | + // Create buttons when page loads |
| 128 | + if (document.readyState === 'loading') { |
| 129 | + document.addEventListener('DOMContentLoaded', createAIChatButtons); |
| 130 | + } else { |
| 131 | + createAIChatButtons(); |
| 132 | + } |
| 133 | +
|
| 134 | + // Recreate on navigation (for SPA routing) |
| 135 | + let lastPath = window.location.pathname; |
| 136 | + setInterval(function() { |
| 137 | + if (window.location.pathname !== lastPath) { |
| 138 | + lastPath = window.location.pathname; |
| 139 | + const existing = document.querySelector('.ai-chat-buttons'); |
| 140 | + if (existing) existing.remove(); |
| 141 | + createAIChatButtons(); |
| 142 | + } |
| 143 | + }, 500); |
| 144 | + })(); |
| 145 | + `, |
| 146 | + }, |
| 147 | + ], |
| 148 | + }; |
| 149 | + }, |
| 150 | + }; |
| 151 | +}; |
0 commit comments