0.8.8 • Published 6 years ago

react-smackdown v0.8.8

Weekly downloads
31
License
MIT
Repository
-
Last release
6 years ago

React Smackdown

React Smackdown is the easiest way to render markdown as html in React. With a single component and string of markdown, you get server-side-compatible JSX, complete with syntax highlighting.

Features

  • ⚛️ React or Preact
  • 💥 Tiny
  • 🚀 Blazing fast.
  • ⚙️ Custom Markdown-to-React Renderers
  • 🥇 Server-side-rendering

Installation

# yarn
$ yarn add react-smackdown
# npm
$ npm install --save react-smackdown

<Markdown>

Use the <Markdown> component to render markdown and also handle syntax highlighting automatically

Props:

  • source: String (Required) - The markdown string you want to render
  • renderers: Object - An object of optional renderers ({tag: component}), used to render any tags encountered in the markdown.
  • markdownConfig: Object - An optional configuration object for the underlying showdown instance.
  • syntax: Object - Optional props that will be forwarded to the Code component for any syntax in the markdown
  • highlightInline: Boolean - Defaults to true. If true, will also highlight any inline code blocks.
import { Markdown } from 'smackdown'

// Import any non-default Prism languages you need:
import 'prismjs/components/prism-jsx.js'
import 'prismjs/components/prism-bash.js'
import 'prismjs/components/prism-go.js'

// Import any Prism theme. We have two of our own "smackdown" themes:
import 'smackdown/themes/smackdown-dark.css'
// or  'smackdown/themes/smackdown-light.css'

const someMarkdown = `
  # Hello!

  This is some **markdown** with some code!

  ```javascript
    const foo = 'bar'
  ```

  ```jsx
    const foo = <div>Hello!</div>
  ```

  ```bash
    $ npm install react-smackdown
  ```

  ```go
    package main
    import "fmt"
    func main() {
        fmt.Println("hello world")
    }
  ```
`

<Markdown
  source={someMarkdown}
/>

The above code would produce this markdown:

Hello!

This is some markdown with some code!

const foo = "bar";
const foo = <div>Hello!</div>;
$ npm install react-smackdown
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

React Renderers

If you want to use your own components to render markdown, it's easy! Just provide an object of tag-to-component values to the renderers prop.

Important Note: If you override the pre or code tag renderers, you will most likely break the automatic syntax highlighting!

import { Markdown } from "smackdown";

const renderers = {
  a: ({ href, ...rest }) => <Link to={href} {...rest} />
}

<Markdown
  source={someMarkdown}
  renderers={renderers}
/>;

<Code> Component

If you don't need to render any markdown, but still want syntax highlighting, you can use the <Code> component directly!

import { Code } from "smackdown";

const source = `
  const foo = 'bar'

  if (foo) {
    console.log('I has foo!')
  }
`

<Code
  source={source}
  language='javascript'
/>;

The above example would produce this code block:

const foo = "bar";

if (foo) {
  console.log("I has foo!");
}
1.0.0-beta.3

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

1.0.0-beta.0

6 years ago

0.8.8

6 years ago

0.8.7

6 years ago

0.8.6

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.6

6 years ago

0.6.5

6 years ago

0.6.4

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago