Skip to content

feat: add copy-to-clipboard button to code blocks (resolves #361)#367

Merged
sumn2u merged 1 commit intosumn2u:mainfrom
mhughes2012:feature/copy-code-button
Feb 26, 2026
Merged

feat: add copy-to-clipboard button to code blocks (resolves #361)#367
sumn2u merged 1 commit intosumn2u:mainfrom
mhughes2012:feature/copy-code-button

Conversation

@mhughes2012
Copy link
Contributor

Overview

This PR implements a "Copy to Clipboard" functionality for all code blocks throughout the book. It uses a modern, language-agnostic icon approach to ensure the UI remains intuitive across all translated versions of the book without requiring additional i18n strings.

Key Changes

  • Assets: Added website/copy-code.js and website/copy-code.css containing the logic and styling for the copy buttons.
  • Layout Overrides: Modified _layouts/layout.html to inject these assets using basePath to ensure correct relative pathing on both the homepage and nested language chapters.
  • Multi-language Support: To ensure the custom layouts are recognized by HonKit's multi-language build process, the _layouts folder has been included within each language directory (e.g., /en/, /fr/).
  • Cross-Platform Compatibility: I opted to copy the layout files into the language subdirectories rather than using symlinks. This ensures that the build remains stable for contributors on Windows, where symlinks often fail or require elevated permissions.
  • Build Configuration: Updated .bookignore to ensure .js assets are no longer excluded from the final output.

Technical Implementation Details

  • Icons: Used embedded SVGs (overlapping squares) for the copy action and a checkmark for the success state. This removes dependencies on external icon fonts.
  • Color Matching: The button background and hover states are tuned to match the existing #f7f7f7 code block background.
  • Event Handling: The script hooks into GitBook/HonKit page.change events to ensure buttons are re-initialized when navigating between chapters without a full page reload.

Testing Performed

  • Verified no errors on root homepage (localhost:4000).
  • Verified on language sub-pages (e.g. localhost:4000/en/, http://localhost:4000/es/, http://localhost:4000/fr/).
  • Tested "Copied!" success state and clipboard verification.
  • Confirmed responsive behavior on mobile views.

@vercel
Copy link

vercel bot commented Feb 26, 2026

@mhughes2012 is attempting to deploy a commit to the Suman Kunwar's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Feb 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
learn-javascript Ready Ready Preview, Comment Feb 26, 2026 0:04am

Copy link
Owner

@sumn2u sumn2u left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for adding this feature; readers will really benefit from it.

@sumn2u
Copy link
Owner

sumn2u commented Feb 26, 2026

Resolves #367

@sumn2u sumn2u merged commit 2aa91d6 into sumn2u:main Feb 26, 2026
3 checks 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.

2 participants