Scully - Using Copy To Clipboard Plugin To Add Copy Button In Code Snippets

When you build a technical blog that has code snippets, you might also need a feature that provides the facility to copy that code snippet in a clipboard. you might have seen in many technical blogs and documentation where it has a button or icon to copy the snippet.
 
Scully Code Snippets with Copy To Clipboard Plugin
 
Scully copy-to-clipboard plugin provides out of the box feature, We just need to use it in our application to add copy to the clipboard facility for code snippets. In this article, we will see how to use it. 
 
Checkout the previous Scully series articles, 
We will continue with the previous portfolio example, If you want to code along, download or clone this github repository.
 
So let's get started.
 

Install Scully To Clipboard Plugin


Execute the following command to install a plugin.
  1. npm install -D @scullyio/scully-plugin-copy-to-clipboard  

Install Clipboard JS


This plugin requires the clipboard.js. Download it from here and add the clipboard.min.js in the assets folder of the angular application.

if you don't want to add clipboard.min.js in the assets folder, We can also provide a clipboard.js CDN path in the plugin config, that we will see in a later section.
 

Configure Scully Config 


Once the copy-to-clipboard plugin is installed, we need to configure it in ScullyConfig to use it for markdown file code snippets.

Copy-To-Clipboard plugin is a render plugin that means at the time of Scully build - after it transforms markdown file to HTML file, it will customize the rendered HTML to add copy button and its script to add copy to clipboard feature.

Import the copyToClipboard plugin in scully.[projectName].config.ts file. and add it in defaultPostRenderers property of ScullyConfig. 
  1. import { ScullyConfig, setPluginConfig } from '@scullyio/scully';  
  2. import { copyToClipboard } from '@scullyio/scully-plugin-copy-to-clipboard';  
  3.   
  4. setPluginConfig('md', { enableSyntaxHighlighting : true});  
  5.   
  6. export const config: ScullyConfig = {  
  7.   projectRoot: "./src",  
  8.   projectName: "portfolio",  
  9.   outDir: './dist/static',  
  10.   defaultPostRenderers : [ copyToClipboard ],  
  11.   routes: {  
  12.     '/blog/:slug': {  
  13.       type: 'contentFolder',  
  14.       slug: {  
  15.         folder: "./blog"  
  16.       }  
  17.     },  
  18.   }  
  19. };  
defaultPostRenderers will check for code snippets in each route and markdown files and add a copy button there. If you want to use it only for the blog/markdown files, you can provide it in its routes config,
  1. import { ScullyConfig, setPluginConfig } from '@scullyio/scully';  
  2. import { copyToClipboard } from '@scullyio/scully-plugin-copy-to-clipboard';  
  3.   
  4. setPluginConfig('md', { enableSyntaxHighlighting : true});  
  5.   
  6. export const config: ScullyConfig = {  
  7.   projectRoot: "./src",  
  8.   projectName: "portfolio",  
  9.   outDir: './dist/static',  
  10.   routes: {  
  11.     '/blog/:slug': {  
  12.       type: 'contentFolder',  
  13.       postRenderers: [ copyToClipboard ],  
  14.       slug: {  
  15.         folder: "./blog"  
  16.       }  
  17.     },  
  18.   }  
  19. };  
This is all you need to do to use a copy-to-clipboard plugin in your application.
 
Output
 
Now to test the plugin by rebuilding the application with the following command
  1. ng build --prod && npm run scully && npm run scully:serve  

Isn't it simple to implement? Write your thoughts in the comment section.

Now let's see the additional features of this plugin.
 

Additional Features


Copy to Clipboard plugin provides additional plugin config option using which, you can set
  • Custom button text for before copy and after copy, you can also use emojis.
  • Attach a custom class to apply custom CSS to that button.
  • Load clipboard.js from a different path, for example, loading it from CDN.
So let's see the example,  We will do the following customization
  1. Change default button text
  2. Customize button style
  3. Load Clipboard.js from CDN
Set Plugin Config
  1. import { ScullyConfig, setPluginConfig } from '@scullyio/scully';  
  2. import { copyToClipboard, CopyToClipboardPluginConfig } from '@scullyio/scully-plugin-copy-to-clipboard';  
  3.   
  4. setPluginConfig('md', { enableSyntaxHighlighting : true});  
  5.   
  6. setPluginConfig<CopyToClipboardPluginConfig>(copyToClipboard, {  
  7.   copyBtnInitialText : 'Copy ðŸ“„',  
  8.   copyBtnOnClickText : 'Copied! âœ…',  
  9.   customBtnClass: 'copyToClipboardBtn',  
  10.   clipboardJSPath : 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js',  
  11. })  
  12.   
  13. export const config: ScullyConfig = {  
  14.   projectRoot: "./src",  
  15.   projectName: "portfolio",  
  16.   outDir: './dist/static',  
  17.   routes: {  
  18.     '/blog/:slug': {  
  19.       type: 'contentFolder',  
  20.       postRenderers: [ copyToClipboard ],  
  21.       slug: {  
  22.         folder: "./blog"  
  23.       }  
  24.     },  
  25.   }  
  26. };  
Add custom copy button styles in styles.css
  1. // styles.css  
  2.   
  3. .copyToClipboardBtn {  
  4.   border-radius: 50px !important;  
  5.   border-color: purple !important;  
  6.   background: linear-gradient(45deg, var(--purple), var(--pink)) !important;  
  7.   color: white !important;  
  8. }  
Now rebuild the app with the following command and test it.
  1. ng build --prod && npm run scully && npm run scully:serve 
Scully Copy To Clipboard Plugin With Custom Configuration
 
As you can see, it has custom styles and text, you can also verify the clipboard.js download location in the developer console.
 

Summary

 
In this article, we have seen how to use the copy to clipboard plugin to add the copy code snippet feature. We have also seen its additional feature to customize the copy button.
 
I hope you like this article, provide your valuable feedback and suggestions in the comment section🙂.

If you feel this is useful to others please like and share.