0.0.4 • Published 4 months ago
snippy-copy v0.0.4
SnippyCopy
SnippyCopy
is a lightweight JavaScript library that allows you to display code snippets with syntax highlighting and a
customizable copy-to-clipboard button. It supports different themes, customizable button styles, and error messages.
Perfect for blogs, tutorials, and technical documentation pages!
Installation
Install SnippyCopy
via npm:
npm install snippy-copy
Usage
Here is an example of how to use SnippyCopy
with all available options:
import SnippyCopy from 'snippy-copy';
const code = `const greet = () => { console.log('Hello, World!'); };`;
new SnippyCopy('#snippet-container', code, 'javascript', {
noCopy: false,
highlight: true,
theme: 'dark',
copyButtonText: 'Copy',
copyButtonStyle: {backgroundColor: '#27ae60', color: 'white', fontSize: '16px'},
errorMessage: 'Unable to copy the code',
caption: 'JavaScript Code Example',
});
Add the corresponding HTML element:
<div id="snippet-container"></div>
Options
Main Parameters
Option | Type | Description |
---|---|---|
selector | String | The selector of the container element where the snippet will be displayed. |
code | String | The code snippet to display. |
language | String | The language of the code snippet (e.g., 'javascript', 'python', 'java'). |
options | Object | Configuration options for customization. |
options
Object
Option | Type | Default Value | Description |
---|---|---|---|
noCopy | Boolean | false | Disables the copy button. |
highlight | Boolean | true | Enables syntax highlighting. |
theme | String | 'light' | Sets the theme for the code snippet ('light' or 'dark' ). |
copyButtonText | String | '📋' | Text displayed on the copy button. |
copyButtonStyle | Object | {} | Custom styles for the copy button. |
errorMessage | String | 'Unable to copy' | Custom error message when copying fails. |
caption | String | '' | Text displayed as a caption above the code snippet. |
showLineNumbers | Boolean | false | Displays line numbers in the code snippet when set to true . |
highlightLineNumbers | Array | [] | List of line numbers to highlight (e.g., [2, 4, 6] ). This option works when showLineNumbers is enabled |
highlightLineColor | String | '' | Color used to highlight the specified line numbers in highlightLineNumbers . |
copySuccessMessage | String | 'Code copied successfully! 🎉' | Message shown after a successful copy. |
fontSize | String | '14px' | Font size of the code. |
lineHeight | String | '1.6' | Line height of the code. |
downloadable | Boolean | false | Line height of the code. |
themeSwitcher | Boolean | false | Line height of the code. |
disableCopyAnimation | Boolean | false | Line height of the code. |
✨ Example Usage
new SnippyCopy('#snippet-container', `const sum = (a, b) => a + b;`, 'javascript', {
noCopy: false,
highlight: true,
theme: 'dark',
copyButtonText: 'Copy',
copyButtonStyle: {backgroundColor: '#3498db', color: 'white', fontSize: '14px'},
errorMessage: 'Unable to copy the code',
caption: 'Simple Sum Function',
showLineNumbers: true,
highlightLineNumbers: [1],
highlightLineColor: '#ff6347'
});
Add the corresponding HTML container:
<div id="snippet-container"></div>
🤝 Contributing
Thank you for your interest in SnippyCopy
! 🚀
🛠 Setting up the project locally
- Fork this repository and clone it locally:
git clone https://github.com/your-username/snippy-copy.git
- Navigate to the project folder:
cd snippy-copy
- Install dependencies:
npm install
- Start the development environment:
npm run dev
🚀 How to contribute
- Create a branch for your feature or bug fix:
git checkout -b feature/add-new-functionality
- Make your changes and test them.
- Commit your changes with a meaningful message:
git commit -m "✨ Added new functionality X"
- Push your branch:
git push origin feature/add-new-functionality
- Open a Pull Request (PR) on GitHub. 🎉
✅ Code style & best practices
- Follow naming conventions (
camelCase
for variables,kebab-case
for file names). - Ensure all tests pass before submitting a PR:
npm test
🛠 Reporting Issues
If you find a bug or have an idea for improvement, please open an issue on GitHub! 🐛✨
📜 License
MIT License