Giter VIP home page Giter VIP logo

react-code-blocks's Introduction

All Contributors

Welcome to react-code-blocks ๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹

Version Downloads Documentation License: MIT Twitter: rajinwonderland

React components for rendering code snippets with syntax highlighting

BREAKING CHANGES: VERSION >= 0.0.8-alpha now requires a nodejs version >= 12

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

Netlify Status

Check out react-code-blocks

Install

Install react-code-blocks via yarn or npm

yarn add react-code-blocks
npm i react-code-blocks

Usage

Updated usage instructions can be found here

import { CopyBlock } from "react-code-blocks";
function MyCodeComponent(props) {
  return (
    <CopyBlock
      text={props.code}
      language={props.language}
      showLineNumbers={props.showLineNumbers}
      startingLineNumber={props.startingLineNumber}
      wrapLines
    />
  );
}

Components

CodeBlock

A simple code block component

import { CodeBlock, dracula } from "react-code-blocks";

function MyCoolCodeBlock({ code, language, showLineNumbers, startingLineNumber }) {
  return (
    <CodeBlock
      text={code}
      language={language}
      showLineNumbers={showLineNumbers}
      startingLineNumber={startingLineNumber}
      theme={dracula}
    />
  );
}

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
startingLineNumber number 1 if showLineNumbers is enabled the line numbering will start from here.
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.

import { CopyBlock, dracula } from "react-code-blocks";

function MyCoolCodeBlock({ code, language, showLineNumbers, startingLineNumber }) {
  <CopyBlock
    text={code}
    language={language}
    showLineNumbers={showLineNumbers}
    startingLineNumber={startingLineNumber}
    theme={dracula}
    codeBlock
  />;
}

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

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 with prismjs or highlightjs ast using inline styles by @conorhastings.
    • It's actually used in this project as well! ๐Ÿ˜„
  • react-highlight.js: A lightweight React wrapper around the Highlight.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! ๐Ÿ˜„

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

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!

react-code-blocks's People

Contributors

allcontributors[bot] avatar rajinwonderland avatar azrosen92 avatar masterodin avatar hamadtheironside avatar bofeiw avatar cannonlock avatar daceynolan avatar okeeffed avatar kevineaton603 avatar lukasbals avatar rohit-ravikoti avatar florpor avatar tahahichri avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.