Installations
npm install @milessystems/react-code-blocks
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=16
Node Version
21.1.0
NPM Version
10.2.0
Releases
Unable to fetch releases
Download Statistics
Total Downloads
821
Last Day
1
Last Week
13
Last Month
29
Last Year
821
Bundle Size
1.60 MB
Minified
525.11 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.11
Package Id
@milessystems/react-code-blocks@1.0.11
Unpacked Size
860.73 kB
Size
161.62 kB
File Count
146
NPM Version
10.2.0
Node Version
21.1.0
Publised On
06 Mar 2024
Total Downloads
Cumulative downloads
Total Downloads
821
Last day
0%
1
Compared to previous day
Last week
0%
13
Compared to previous week
Last month
11.5%
29
Compared to previous month
Last year
0%
821
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
32
Welcome to react-code-blocks
👋👋👋
React components for rendering code snippets with syntax highlighting
BREAKING CHANGES:
VERSION >= 0.0.8-alpha
now requires a nodejs version >= 12
- Background
- Install
- Usage
- Components
- Supported Themes
- Supported Languages
- Alternatives
- Todos
- Show your support
- Author
- Maintainer
- Contributors ✨
Background
Initially, this started as a small project looking to modify Atlaskit's Code Block component to support more languages (i.e graphql
, reasonml
, etc). It then rapidly evolved into a never-ending rabbithole which ended up with support for themes (i.e railscast, darcula, monokai, etc), customizable styles, and copy functionality too!
I don't plan on keeping this updated unless people are looking to actually use it. So don't be shy! Feel free to post an issue or a pr.
✨ Demo
Install
Install react-code-blocks
via yarn
or npm
1yarn add react-code-blocks
1npm i react-code-blocks
Usage
Updated usage instructions can be found here
1import { CopyBlock } from 'react-code-blocks';
1function MyCodeComponent(props) { 2 return ( 3 <CopyBlock 4 text={props.code} 5 language={props.language} 6 showLineNumbers={props.showLineNumbers} 7 wrapLines 8 /> 9 ); 10}
Components
CodeBlock
A simple code block component
1import { CodeBlock, dracula } from 'react-code-blocks'; 2 3function MyCoolCodeBlock({ code, language, showLineNumbers }) { 4 return ( 5 <CodeBlock 6 text={code} 7 language={language} 8 showLineNumbers={showLineNumbers} 9 theme={dracula} 10 /> 11 ); 12}
Props
Note that
CodeBlock
&CopyBlock
share exactly the same props
name | type | default | description |
---|---|---|---|
text | string | required | The code to be formatted |
language | string | "text" | The language in which the code is written. See here for a list of supported languages |
showLineNumbers | boolean | true | Indicates whether or not to show line numbers |
theme | object | dracula | A theme object for the code block. See here for a list of supported themes |
highlight | string | "" | Lines to highlight! For multiple lines, use a comma i.e highlight="1,6,7" . For a range of lines, use a - i.e highlight="1-5" for highlighting lines 1-5. |
more to come...
CopyBlock
A code block component with a little copy button for copying a snippet.
1import { CopyBlock, dracula } from 'react-code-blocks'; 2 3function MyCoolCodeBlock({ code, language, showLineNumbers }) { 4 <CopyBlock 5 text={code} 6 language={language} 7 showLineNumbers={showLineNumbers} 8 theme={dracula} 9 codeBlock 10 />; 11}
Props
Same as the CodeBlock
's component with the exception of one!
name | type | default | description |
---|---|---|---|
codeBlock | boolean | false | Indicates whether to render the CopyBlock as an inline Code component or a CodeBlock component |
onCopy | function | - | The onCopy function is called if the copy icon is clicked. This enables you to add a custom message that the code block is copied |
Supported Themes
For a list of supported themes, check out the list here
Supported Languages
For a list of supported languages, check out the list here
Alternatives
If you're looking for some more maintained solutions, I'd suggest the following:
react-syntax-highlighter
: syntax highlighting component for react withprismjs
orhighlightjs
ast
using inline styles by @conorhastings.- It's actually used in this project as well! :smile:
react-highlight.js
: A lightweight React wrapper around theHighlight.js
syntax highlighting library by @bvaughn.react-live
: A flexible playground for live editing React components by @FormidableLabs.@atlaskit/code
: Renders inline code snippets and code blocks- Original inspiration for this project kit for this project
carbon-components-react
: Check out their<CodeSnippet>
component which supports multi-line, single-line, and inline snippets along with added copying functionality.
Feel free to add any other alternative packages here! :smile:
Todos
- Add a better readme
- Highlighting line feature
- Supported Themes documentation
- Supported Languages documentation
- Add a License
- Docs for usage with GatsbyJS
- Docs for usage with
MDX
- Contributor guide for development
- Contributor guide for adding themes
- Contributor guide for adding languages
- Document props for
Code
component - Better demo
- Storybook with Docs
- Move to typescript
- A component with a terminal around it. because why not!? 🧐
Show your support
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator
Author
- Website: AreaHub
- Twitter: @rajinwonderland
- Github: @rajinwonderland
Maintainer
react-code-blocks is currently maintained by Thomas
- Website: Bend
- Bluesky: @thomasmost.bsky.social
- Github: @thomasmost
Contributors ✨
Thanks goes to these wonderful people (emoji key):
Taha HICHRI 💻 | florpor 🐛 | Abdollah Keshtkar 💡 📖 💻 | Lukas Bals 💻 | Bobby Wang 💻 | Matthew Peveler 📖 | Dacey Nolan 💡 📖 💻 |
Kevin Eaton 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
No security vulnerabilities found.