gatsby-remark-code-buttons
Add buttons to markdown code snippets.
This plugin doesn't support MDX. Example of MDX copy button.
Install
npm install gatsby-remark-code-buttons --save-dev
How to use
in your gatsby-config.js
Options
Custom styling
Now that we've injected the custom button, we need to style it!
To apply custom styles import stylesheet in your app's root gatsby-browser.js.
Usage in Markdown
In your Markdown content
...```
With toasterText config enabled this plugin will inject a custom toaster node:
Don't show button
if (navigator.clipboard) {
console.log('Clipboard API available');
}
// copy text TO the clipboard await navigator.clipboard.writeText('This text is now in the clipboard');
// get text FROM the clipboard let text = await navigator.clipboard.readText();
copy text from #mysection
paste
const image = await fetch('myimage.png'), blob = await image.blob();
await navigator.clipboard.write([ new ClipboardItem({ [blob.type]: blob }) ]);
const clipboardItems = await navigator.clipboard.read();
for (const clipboardItem of clipboardItems) {
for (const type of clipboardItem.types) {
}
}
copy imagepaste image
body.addEventListener('cut', cutCopyHandler); body.addEventListener('copy', cutCopyHandler);
// cut or copy event handler function cutCopyHandler(e) {
const selection = document.getSelection();
// send uppercase text to clipboard e.clipboardData.setData( 'text/plain', selection.toString().toUpperCase() );
if (e.type === 'cut') selection.deleteFromDocument();
// stop default cut/copy e.preventDefault();
}
document.getElementById('field1').addEventListener('paste', pasteEvent);
// paste event handler function pasteEvent(e) {
// add 'pasted:' to pasted text const paste = 'pasted:\n' + (e.clipboardData || window.clipboardData).getData('text');
e.target.value = paste;
// stop default paste e.preventDefault(); }
Last updated
Was this helpful?