-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
add copy button to code blocks #389
Comments
as an update: this is what I am using right now: I am injecting the copy button as a plugin: function addCopyButtons(md: MarkdownIt) {
const defaultFenceRenderer =
md.renderer.rules.fence ||
((tokens, idx, options, env, self) => {
return self.renderToken(tokens, idx, options);
});
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
// Original rendered HTML of the code block
const originalRenderedHtml = defaultFenceRenderer(
tokens,
idx,
options,
env,
self
);
// Custom HTML for the button
const customHtml = `
<div class="code-block-with-overlay">
${originalRenderedHtml}
<button class="copy-button q-btn q-btn-item non-selectable transparent q-btn--flat q-btn--rectangle
q-btn--actionable q-focusable q-hoverable q-btn--dense copy-button print-hide">
<span class="q-focus-helper"></span>
<span class="q-btn__content text-center col items-center q-anchor--skip justify-center row">
<i class="q-icon notranslate material-icons" aria-hidden="true" role="img">content_copy</i>
</span>
</button>
</div>
`;
return customHtml;
};
}
function handleMarkdownClick(event: MouseEvent) {
const target = (event.target as HTMLElement).closest('.copy-button');
if (target) {
// Find the closest .code-block-with-overlay and then find the <code> element inside it
const codeBlockContainer = target.closest('.code-block-with-overlay');
if (codeBlockContainer) {
const codeElement = codeBlockContainer.querySelector('code');
if (codeElement) {
const codeText = codeElement.textContent || ''; // Get the text content of the <code> element
copyToClipboard(codeText);
}
}
}
} and then add it like this:
will create a copy button like this: you can test it out here in my app: taskyon.space :) |
would be great though, to have this "built-in" :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is your feature request related to a problem? Please describe.
I would like to have an optional copy button for code blocks.
Describe the solution you'd like
When a code block e.g. like this:
appears, there should be a copy button next to the code block
Describe alternatives you've considered
I tried showCopy, but it only works for the entire document.
Additional context
it works in github :). see:
The text was updated successfully, but these errors were encountered: