Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: ScottAgirs/Add inline code example #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 34 additions & 2 deletions README.md
Expand Up @@ -81,12 +81,14 @@ yarn add prism-react-renderer

<img src="./.readme/basic.png" width="237" alt="Screenshot showing highlighted code block" />

### Code block

```jsx
import React from "react";
import { render } from "react-dom";
import Highlight, { defaultProps } from "prism-react-renderer";

const exampleCode = `
const exampleCodeBlock = `
(function someDemo() {
var test = "Hello World!";
console.log(test);
Expand All @@ -96,7 +98,7 @@ return () => <App />;
`;

render((
<Highlight {...defaultProps} code={exampleCode} language="jsx">
<Highlight {...defaultProps} code={exampleCodeBlock} language="jsx">
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={style}>
{tokens.map((line, i) => (
Expand All @@ -123,6 +125,36 @@ It doesn't render anything itself, it just calls the render function and renders
["Use a render prop!"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)!
`<Highlight>{highlight => <pre>/* your JSX here! */</pre>}</Highlight>`

### Inline Code
> Note: backticks will break the inline code highlighting

```jsx
import React from "react";
import { render } from "react-dom";
import Highlight, { defaultProps } from "prism-react-renderer";

const exampleInlineCode = `const ohDeerVar = "Stringly"`;

render((
<Highlight {...defaultProps} theme={theme} code={exampleInlineCode} language="jsx">
{({ className, style, tokens, getLineProps, getTokenProps }) =>
tokens.map((line, i) => (
<code
{...getLineProps({ key: i, line })}
style={style}
className={className}
>
{line.map((token, key) => (
<span {...getTokenProps({ key, token })} />
))}
</code>
))
}
</Highlight>,
document.getElementById('root')
);
```

### Line Numbers

Add line numbers to your highlighted code blocks using the `index` parameter in your `line.map` function:
Expand Down