Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Copy Button #911

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

Open
essesoul wants to merge 5 commits into luizdepra:main
base: main
Choose a base branch
Loading
from essesoul:essesoul-dev
Open

Add Copy Button #911

essesoul wants to merge 5 commits into luizdepra:main from essesoul:essesoul-dev

Conversation

@essesoul
Copy link

@essesoul essesoul commented May 5, 2024

Prerequisites

Put an x into the box(es) that apply:

  • This pull request fixes a bug.
  • This pull request adds a feature.
  • This pull request introduces breaking change.

Description

Click on the button to copy the content of the code block

One-click copying of the content of a block of code is achieved by creating the files static/css/clipboard.css and static/js/clipboard.js

Users can now quickly copy the contents of a block by clicking the button in the upper right corner of the block.

Checklist

Put an x into the box(es) that apply:

General

  • Describe what changes are being made
  • Explain why and how the changes were necessary and implemented respectively
  • Reference issue with #<ISSUE_NO> if applicable

Resources

  • If you have changed any SCSS code, run make release to regenerate all CSS files

Contributors

  • Add yourself to CONTRIBUTORS.md if you aren't on it already

jankatins reacted with thumbs up emoji
Copy link
Owner

Nice, thank you.
But I will need to make some modifications like move the CSS code to SCSS, and overriding the code template to add the button instead of using JS to include it.

Copy link

@septs septs left a comment

Choose a reason for hiding this comment

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

i'm not a reviewer, but i think this is not elegant

  1. i think it would be better to use js.Build preprocessing
{{-
$defines := dict
 "css_url" (resources.Get "js/clipboard.css").Permalink
-}}
{{- with resources.Get "js/clipboard.ts" (dict "defines" $defines) | js.Build | minify | fingerprint -}}
<script src="{{ .Permalink }}"></script>
{{- end -}}
  1. use layouts/_default/_markup/render-codeblock.html hook, set page variable
{{- .Page.Store.Set "hasCodeblock" true -}}

see https://gohugo.io/render-hooks/code-blocks/

  1. use javascript dynamic loading stylesheet
declare const css_url: string
const stylesheet = document.createElement("link")
stylesheet.rel = "stylesheet"
stylesheet.href = css_url
document.head.appendChild(stylesheet)

Copy link

jemus42 commented Mar 8, 2025

I was just about to research how to add a code copy button when I found this --- is this still being considered?

Copy link
Contributor

pduchnovsky commented May 24, 2025
edited
Loading

clipboard.writeText(codeBlock.innerText)
must be
clipboard.writeText(codeBlock.textContent) instead

innerText can add extra line breaks or modify spacing due to CSS/rendering.
textContent preserves the exact formatting in the DOM — better for code blocks.

Anyway I already added this to my web repo directly and I like it, awesome.

essesoul reacted with heart emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers

@septs septs septs left review comments

@Yunglow22 Yunglow22 Yunglow22 approved these changes

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /