Gatsby plugin to add copy button to Remark code block
npm install gatsby-remark-prismjs-copy-button
Add copy button to your prismjs' code blocks.
https://thundermiracle.com/blog/en/2022-03-13-gatsby-add-copy-button-to-code-block/
``shell`
npm install --save gatsby-transformer-remark gatsby-remark-prismjs gatsby-remark-prismjs-copy-button
1. Add it to gatsby-config.js:
※ NOTE: You _MUST_ add gatsby-remark-prismjs-copy-button before gatsby-remark-prismjs, as gatsby-remark-prismjs will transform Code MarkdownASTs to HTML. So gatsby-remark-prismjs-copy-button will unable to find the Code MarkdownASTs to add copy button.
`javascriptgatsby-transformer-remark
module.exports = {
plugins: [
{
resolve: ,gatsby-remark-prismjs-copy-button
options: {
plugins: [
,gatsby-remark-prismjs
,`
],
},
},
],
};
2. In your template.js.
`javascript
export const query = graphql
query ($slug: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
html
}
}
;
export default function Template({ data }) {
return (
3. Overwrite the style of the copy button.
You can overwrite the style of the copy button by adding the following class to your own
style.css.
`css
.gatsby-remark-prismjs-copy-button-container {
left: 0;
padding-right: 8px;
}
``If you have unanswered questions, would like help with enhancing or debugging the plugin, it is nice to include instructions for people who want to contribute to your plugin.
This project is licensed under the terms of the MIT license.