-
Notifications
You must be signed in to change notification settings - Fork 308
feat: add copy markdowm/text button for docs pages #1521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GuangmingLuo
merged 14 commits into
cloudwego:main
from
solarhell:feat/copy-fulltext-button
Mar 17, 2026
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f403c1f
feat: add copy fulltext button for docs pages
solarhell b4b56ac
fix: handle mermaid SVGs and images in plain text copy
solarhell c09dfc2
fix: add copy fulltext button to docs list template
solarhell 78b35ac
fix: resolve double JSON encoding of RawContent for markdown copy
solarhell 0e4b64d
refactor: use data attributes instead of JSON for copy button metadata
solarhell 1e20168
feat: add toast notification and checkmark icon on copy success
solarhell b8ac7c0
docs: add copy button preview screenshots
solarhell 5e6350d
chore: remove temporary screenshot files
solarhell eea61ed
Revert "chore: remove temporary screenshot files"
solarhell 1168316
refactor: use Bootstrap 4 split button dropdown for copy button
solarhell 1a333bc
chore: remove screenshots directory
solarhell 5de2093
refactor: remove deprecated execCommand fallback for clipboard copy
solarhell bd57418
fix: improve plain text whitespace collapsing and prevent HTML escapi…
solarhell c32e5ae
Merge branch 'main' into feat/copy-fulltext-button
solarhell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| (function () { | ||
| 'use strict'; | ||
|
|
||
| var container = document.getElementById('copy-fulltext'); | ||
| if (!container) return; | ||
|
|
||
| var toggleBtn = document.getElementById('copy-fulltext-toggle'); | ||
| var defaultBtn = document.getElementById('copy-fulltext-default'); | ||
| var dropdown = document.getElementById('copy-fulltext-dropdown'); | ||
| var options = dropdown.querySelectorAll('.copy-fulltext__option'); | ||
|
|
||
| toggleBtn.addEventListener('click', function (e) { | ||
| e.stopPropagation(); | ||
| if (dropdown.classList.contains('copy-fulltext__dropdown--open')) { | ||
| closeDropdown(); | ||
| } else { | ||
| openDropdown(); | ||
| } | ||
| }); | ||
|
|
||
| function openDropdown() { | ||
| dropdown.classList.add('copy-fulltext__dropdown--open'); | ||
| toggleBtn.querySelector('i').classList.replace('fa-chevron-up', 'fa-chevron-down'); | ||
| } | ||
|
|
||
| function closeDropdown() { | ||
| dropdown.classList.remove('copy-fulltext__dropdown--open'); | ||
| toggleBtn.querySelector('i').classList.replace('fa-chevron-down', 'fa-chevron-up'); | ||
| } | ||
|
|
||
| document.addEventListener('click', function (e) { | ||
| if (!container.contains(e.target)) { | ||
| closeDropdown(); | ||
| } | ||
| }); | ||
|
|
||
| // Default button copies markdown (most useful for LLM) | ||
| defaultBtn.addEventListener('click', function () { | ||
| copyContent('markdown'); | ||
| }); | ||
|
|
||
| options.forEach(function (opt) { | ||
| opt.addEventListener('click', function () { | ||
| copyContent(this.getAttribute('data-copy-type')); | ||
| closeDropdown(); | ||
| }); | ||
| }); | ||
|
|
||
| var toast = document.getElementById('copy-fulltext-toast'); | ||
|
|
||
| function getSourceData() { | ||
| var mdEl = document.getElementById('copy-fulltext-markdown'); | ||
| return { | ||
| title: container.getAttribute('data-title') || '', | ||
| url: container.getAttribute('data-url') || '', | ||
| successMarkdown: container.getAttribute('data-success-markdown') || 'Copied as Markdown', | ||
| successText: container.getAttribute('data-success-text') || 'Copied as plain text', | ||
| markdown: mdEl ? mdEl.textContent : '' | ||
| }; | ||
| } | ||
|
|
||
| function getPlainText() { | ||
| var contentEl = document.querySelector('.td-content'); | ||
| if (!contentEl) return ''; | ||
| var clone = contentEl.cloneNode(true); | ||
|
|
||
| // Remove UI elements that shouldn't be copied | ||
| var removals = clone.querySelectorAll('.copy-fulltext, .td-page-meta, script, style, .feedback--container'); | ||
| removals.forEach(function (el) { el.remove(); }); | ||
|
|
||
| // Replace mermaid SVGs with placeholder (SVG textContent is gibberish) | ||
| clone.querySelectorAll('pre.mermaid, .mermaid').forEach(function (el) { | ||
| var placeholder = document.createElement('p'); | ||
| placeholder.textContent = '[diagram]'; | ||
| el.replaceWith(placeholder); | ||
| }); | ||
|
|
||
| // Replace images with their alt text | ||
| clone.querySelectorAll('img').forEach(function (img) { | ||
| var alt = img.getAttribute('alt'); | ||
| if (alt) { | ||
| var text = document.createElement('span'); | ||
| text.textContent = '[image: ' + alt + ']'; | ||
| img.replaceWith(text); | ||
| } else { | ||
| img.remove(); | ||
| } | ||
| }); | ||
|
|
||
| return clone.textContent.replace(/\n{3,}/g, '\n\n').trim(); | ||
| } | ||
|
|
||
| function copyContent(type) { | ||
| var data = getSourceData(); | ||
| var text = ''; | ||
| var actualType = type; | ||
|
|
||
| if (type === 'markdown' && data && data.markdown) { | ||
| text = '# ' + data.title + '\n\n' + data.markdown; | ||
| if (data.url) { | ||
| text += '\n\n---\nSource: ' + data.url; | ||
| } | ||
| } else { | ||
| actualType = 'text'; | ||
| text = getPlainText(); | ||
| } | ||
|
|
||
| copyToClipboard(text, data, actualType); | ||
| } | ||
|
|
||
| function copyToClipboard(text, data, type) { | ||
| if (navigator.clipboard && navigator.clipboard.writeText) { | ||
| navigator.clipboard.writeText(text).then(function () { | ||
| showFeedback(data, type); | ||
| }).catch(function () { | ||
| fallbackCopy(text, data, type); | ||
| }); | ||
| } else { | ||
| fallbackCopy(text, data, type); | ||
| } | ||
| } | ||
|
|
||
| function fallbackCopy(text, data, type) { | ||
| var textarea = document.createElement('textarea'); | ||
| textarea.value = text; | ||
| textarea.style.position = 'fixed'; | ||
| textarea.style.opacity = '0'; | ||
| document.body.appendChild(textarea); | ||
| textarea.select(); | ||
| try { | ||
| document.execCommand('copy'); | ||
| showFeedback(data, type); | ||
| } catch (e) { | ||
| // silent fail | ||
| } | ||
| document.body.removeChild(textarea); | ||
| } | ||
|
|
||
| function showFeedback(data, type) { | ||
| // Swap icon to checkmark | ||
| var icon = defaultBtn.querySelector('i'); | ||
| icon.classList.replace('fa-copy', 'fa-check'); | ||
| defaultBtn.classList.add('copy-fulltext__btn--success'); | ||
|
|
||
| // Show toast | ||
| var toastText = type === 'markdown' ? data.successMarkdown : data.successText; | ||
| toast.textContent = '✅ ' + toastText; | ||
| toast.classList.add('copy-fulltext__toast--visible'); | ||
|
|
||
| setTimeout(function () { | ||
| icon.classList.replace('fa-check', 'fa-copy'); | ||
| defaultBtn.classList.remove('copy-fulltext__btn--success'); | ||
| toast.classList.remove('copy-fulltext__toast--visible'); | ||
| }, 2000); | ||
| } | ||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| .copy-fulltext { | ||
| position: relative; | ||
| display: inline-flex; | ||
| float: right; | ||
| margin-top: 0.25rem; | ||
|
|
||
| &__main { | ||
| display: inline-flex; | ||
| align-items: stretch; | ||
| border: 1px solid $gray-300; | ||
| border-radius: $border-radius; | ||
| background: $white; | ||
| overflow: hidden; | ||
| } | ||
|
|
||
| &__btn { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| gap: 0.375rem; | ||
| padding: 0.375rem 0.75rem; | ||
| border: none; | ||
| background: transparent; | ||
| color: $gray-700; | ||
| font-size: 0.875rem; | ||
| cursor: pointer; | ||
| white-space: nowrap; | ||
| transition: background-color 0.15s, color 0.15s; | ||
|
|
||
| &:hover { | ||
| background-color: $gray-100; | ||
| color: $gray-900; | ||
| } | ||
|
|
||
| &--success { | ||
| color: $success !important; | ||
| } | ||
| } | ||
|
|
||
| &__toggle { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| padding: 0.375rem 0.5rem; | ||
| border: none; | ||
| border-left: 1px solid $gray-300; | ||
| background: transparent; | ||
| color: $gray-500; | ||
| cursor: pointer; | ||
| transition: background-color 0.15s; | ||
|
|
||
| &:hover { | ||
| background-color: $gray-100; | ||
| } | ||
|
|
||
| i { | ||
| font-size: 0.625rem; | ||
| } | ||
| } | ||
|
|
||
| &__toast { | ||
| display: none; | ||
| position: absolute; | ||
| bottom: 100%; | ||
| left: 50%; | ||
| transform: translateX(-50%); | ||
| margin-bottom: 0.5rem; | ||
| padding: 0.5rem 0.75rem; | ||
| background: $white; | ||
| border: 1px solid $gray-300; | ||
| border-radius: $border-radius; | ||
| box-shadow: 0 4px 12px rgba($black, 0.1); | ||
| font-size: 0.875rem; | ||
| color: $gray-700; | ||
| white-space: nowrap; | ||
| z-index: 11; | ||
|
|
||
| &--visible { | ||
| display: block; | ||
| } | ||
| } | ||
|
|
||
| &__dropdown { | ||
| display: none; | ||
| position: absolute; | ||
| top: 100%; | ||
| right: 0; | ||
| margin-top: 0.25rem; | ||
| min-width: 140px; | ||
| background: $white; | ||
| border: 1px solid $gray-300; | ||
| border-radius: $border-radius; | ||
| box-shadow: 0 4px 12px rgba($black, 0.1); | ||
| z-index: 10; | ||
| overflow: hidden; | ||
|
|
||
| &--open { | ||
| display: block; | ||
| } | ||
| } | ||
|
|
||
| &__option { | ||
| display: block; | ||
| width: 100%; | ||
| padding: 0.5rem 0.75rem; | ||
| border: none; | ||
| background: transparent; | ||
| color: $gray-700; | ||
| font-size: 0.875rem; | ||
| text-align: left; | ||
| cursor: pointer; | ||
| transition: background-color 0.15s; | ||
|
|
||
| &:hover { | ||
| background-color: $gray-100; | ||
| } | ||
|
|
||
| & + & { | ||
| border-top: 1px solid $gray-200; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| {{/* Copy full text button with dropdown for markdown / plain text */}} | ||
| <div class="copy-fulltext" id="copy-fulltext" | ||
| data-title="{{ .Title }}" | ||
| data-url="{{ .Permalink }}" | ||
| data-success-markdown="{{ T "copy_success_markdown" }}" | ||
| data-success-text="{{ T "copy_success_text" }}"> | ||
| <div class="copy-fulltext__main"> | ||
| <button class="copy-fulltext__btn" id="copy-fulltext-default" title="{{ T "copy_full_text" }}"> | ||
| <i class="fa-regular fa-copy"></i> | ||
| <span>{{ T "copy_full_text" }}</span> | ||
| </button> | ||
| <button class="copy-fulltext__toggle" id="copy-fulltext-toggle" aria-label="More copy options"> | ||
| <i class="fa-solid fa-chevron-up"></i> | ||
| </button> | ||
| </div> | ||
| <div class="copy-fulltext__toast" id="copy-fulltext-toast"></div> | ||
| <div class="copy-fulltext__dropdown" id="copy-fulltext-dropdown"> | ||
| <button class="copy-fulltext__option" data-copy-type="markdown"> | ||
| {{ T "copy_markdown" }} | ||
| </button> | ||
| <button class="copy-fulltext__option" data-copy-type="text"> | ||
| {{ T "copy_plain_text" }} | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| {{/* Embed raw markdown for copy-as-markdown */}} | ||
| <script id="copy-fulltext-markdown" type="text/plain">{{ .RawContent }}</script> | ||
|
solarhell marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
solarhell marked this conversation as resolved.
Outdated
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.