|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var container = document.getElementById('copy-fulltext'); |
| 5 | + if (!container) return; |
| 6 | + |
| 7 | + var defaultBtn = document.getElementById('copy-fulltext-default'); |
| 8 | + var options = container.querySelectorAll('.dropdown-item'); |
| 9 | + var toast = document.getElementById('copy-fulltext-toast'); |
| 10 | + |
| 11 | + // Default button copies markdown (most useful for LLM) |
| 12 | + defaultBtn.addEventListener('click', function () { |
| 13 | + copyContent('markdown'); |
| 14 | + }); |
| 15 | + |
| 16 | + options.forEach(function (opt) { |
| 17 | + opt.addEventListener('click', function () { |
| 18 | + copyContent(this.getAttribute('data-copy-type')); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + function getSourceData() { |
| 23 | + var mdEl = document.getElementById('copy-fulltext-markdown'); |
| 24 | + return { |
| 25 | + title: container.getAttribute('data-title') || '', |
| 26 | + url: container.getAttribute('data-url') || '', |
| 27 | + successMarkdown: container.getAttribute('data-success-markdown') || 'Copied as Markdown', |
| 28 | + successText: container.getAttribute('data-success-text') || 'Copied as plain text', |
| 29 | + markdown: mdEl ? mdEl.textContent : '' |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + function getPlainText() { |
| 34 | + var contentEl = document.querySelector('.td-content'); |
| 35 | + if (!contentEl) return ''; |
| 36 | + var clone = contentEl.cloneNode(true); |
| 37 | + |
| 38 | + // Remove UI elements that shouldn't be copied |
| 39 | + var removals = clone.querySelectorAll('.copy-fulltext, .td-page-meta, script, style, .feedback--container'); |
| 40 | + removals.forEach(function (el) { el.remove(); }); |
| 41 | + |
| 42 | + // Replace mermaid SVGs with placeholder (SVG textContent is gibberish) |
| 43 | + clone.querySelectorAll('pre.mermaid, .mermaid').forEach(function (el) { |
| 44 | + var placeholder = document.createElement('p'); |
| 45 | + placeholder.textContent = '[diagram]'; |
| 46 | + el.replaceWith(placeholder); |
| 47 | + }); |
| 48 | + |
| 49 | + // Replace images with their alt text |
| 50 | + clone.querySelectorAll('img').forEach(function (img) { |
| 51 | + var alt = img.getAttribute('alt'); |
| 52 | + if (alt) { |
| 53 | + var text = document.createElement('span'); |
| 54 | + text.textContent = '[image: ' + alt + ']'; |
| 55 | + img.replaceWith(text); |
| 56 | + } else { |
| 57 | + img.remove(); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + return clone.textContent.replace(/(\s*\n){3,}/g, '\n\n').trim(); |
| 62 | + } |
| 63 | + |
| 64 | + function copyContent(type) { |
| 65 | + var data = getSourceData(); |
| 66 | + var text = ''; |
| 67 | + var actualType = type; |
| 68 | + |
| 69 | + if (type === 'markdown' && data && data.markdown) { |
| 70 | + text = '# ' + data.title + '\n\n' + data.markdown; |
| 71 | + if (data.url) { |
| 72 | + text += '\n\n---\nSource: ' + data.url; |
| 73 | + } |
| 74 | + } else { |
| 75 | + actualType = 'text'; |
| 76 | + text = getPlainText(); |
| 77 | + } |
| 78 | + |
| 79 | + copyToClipboard(text, data, actualType); |
| 80 | + } |
| 81 | + |
| 82 | + function copyToClipboard(text, data, type) { |
| 83 | + navigator.clipboard.writeText(text).then(function () { |
| 84 | + showFeedback(data, type); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + function showFeedback(data, type) { |
| 89 | + // Swap icon to checkmark |
| 90 | + var icon = defaultBtn.querySelector('i'); |
| 91 | + icon.classList.replace('fa-copy', 'fa-check'); |
| 92 | + defaultBtn.classList.add('copy-fulltext__btn--success'); |
| 93 | + |
| 94 | + // Show toast |
| 95 | + var toastText = type === 'markdown' ? data.successMarkdown : data.successText; |
| 96 | + toast.textContent = '✅ ' + toastText; |
| 97 | + toast.classList.add('copy-fulltext__toast--visible'); |
| 98 | + |
| 99 | + setTimeout(function () { |
| 100 | + icon.classList.replace('fa-check', 'fa-copy'); |
| 101 | + defaultBtn.classList.remove('copy-fulltext__btn--success'); |
| 102 | + toast.classList.remove('copy-fulltext__toast--visible'); |
| 103 | + }, 2000); |
| 104 | + } |
| 105 | +})(); |
0 commit comments