Skip to content

Add copy button to code blocks - #54

Merged
patakuti merged 3 commits into
mainfrom
feature/copy-button
Jun 18, 2026
Merged

Add copy button to code blocks#54
patakuti merged 3 commits into
mainfrom
feature/copy-button

Conversation

@patakuti

@patakuti patakuti commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add a copy button to each <pre><code> block, displayed at the top-right corner on hover
  • Clicking copies the code to the clipboard and shows "Copied!" for 2 seconds
  • Styled for all themes (GitHub, Simple, Dark); hidden during printing

Implementation

New file internal/template/copybutton.go following the same pattern as toc.go / lineanchor.go:

  • copyButtonCSS: positions the button absolutely in the top-right of pre, with hover/focus reveal and per-theme colors
  • copyButtonJS: injects buttons on DOMContentLoaded; uses textContent (not innerText) to avoid extra newlines introduced by chroma's display: flex on span.line

Test plan

  • Code blocks show a copy button on hover
  • Clicking copies the exact code (no extra newlines) to the clipboard
  • "Copied!" feedback appears for ~2 seconds then resets
  • Works in all three themes (GitHub, Simple, Dark)
  • Button is hidden when printing

Closes #53

🤖 Generated with Claude Code

patakuti and others added 3 commits June 18, 2026 20:02
Add a copy button to each code block that appears on hover at the top-right
corner. Clicking copies the code to the clipboard and briefly shows "Copied!".
Styled for all themes (GitHub, Simple, Dark) and hidden during printing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Strip trailing newlines from code block text before writing to clipboard,
as chroma appends a trailing newline to rendered code blocks.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
chroma applies display:flex to span.line, causing innerText to insert
extra newlines between each line. Switch to textContent which reads
raw text node values without CSS layout side-effects.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@patakuti
patakuti merged commit 7437e22 into main Jun 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add copy button to code blocks

1 participant