source: node_modules/react-syntax-highlighter/README.md@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 11.8 KB
RevLine 
[d24f17c]1## React Syntax Highlighter
2
3[![Actions Status](https://github.com/react-syntax-highlighter/react-syntax-highlighter/workflows/Node%20CI/badge.svg)](https://github.com/conorhastings/react-syntax-highlighter/actions)
4[![npm](https://img.shields.io/npm/dm/react-syntax-highlighter.svg?style=flat-square)](https://www.npmjs.com/package/react-syntax-highlighter)
5
6<!-- [![codecov](https://codecov.io/gh/conorhastings/react-syntax-highlighter/branch/master/graph/badge.svg)](https://codecov.io/gh/conorhastings/react-syntax-highlighter) -->
7
8Syntax highlighting component for `React` using the seriously super amazing <a href="https://github.com/wooorm/lowlight">lowlight</a> and <a href="https://github.com/wooorm/refractor">refractor</a> by <a href="https://github.com/wooorm">wooorm</a>
9
10Check out a small demo <a href="https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/">here</a> and see the component in action highlighting the generated test code <a href="https://conorhastings.github.io/redux-test-recorder/demo/">here</a>.
11
12For React Native you can use <a href='https://github.com/conorhastings/react-native-syntax-highlighter'>react-native-syntax-highlighter</a>
13
14### Install
15
16`npm install react-syntax-highlighter --save`
17
18### Why This One?
19
20There are other syntax highlighters for `React` out there so why use this one? The biggest reason is that all the others rely on triggering calls in `componentDidMount` and `componentDidUpdate` to highlight the code block and then insert it in the render function using `dangerouslySetInnerHTML` or just manually altering the DOM with native javascript. This utilizes a syntax tree to dynamically build the virtual dom which allows for updating only the changing DOM instead of completely overwriting it on any change, and because of this it is also using more idiomatic `React` and allows the use of pure function components brought into `React` as of `0.14`.
21
22### Javascript Styles!
23
24One of the biggest pain points for me trying to find a syntax highlighter for my own projects was the need to put a stylesheet tag on my page. I wanted to provide out of the box code styling with my modules without requiring awkward inclusion of another libs stylesheets. The styles in this module are all javascript based, and all styles supported by `highlight.js` have been ported!
25
26I do realize that javascript styles are not for everyone, so you can optionally choose to use css based styles with classNames added to elements by setting the prop `useInlineStyles` to `false` (it defaults to `true`).
27
28### Use
29
30#### props
31
32- `language` - the language to highlight code in. Available options [here for hljs](./AVAILABLE_LANGUAGES_HLJS.MD) and [here for prism](./AVAILABLE_LANGUAGES_PRISM.MD). (pass text to just render plain monospaced text)
33- `style` - style object required from styles/hljs or styles/prism directory depending on whether or not you are importing from `react-syntax-highlighter` or `react-syntax-highlighter/prism` directory [here for hljs](./AVAILABLE_STYLES_HLJS.MD). and [here for prism](./AVAILABLE_STYLES_PRISM.MD). `import { style } from 'react-syntax-highlighter/dist/esm/styles/{hljs|prism}'` . Will use default if style is not included.
34- `children` - the code to highlight.
35- `customStyle` - prop that will be combined with the top level style on the pre tag, styles here will overwrite earlier styles.
36- `codeTagProps` - props that will be spread into the `<code>` tag that is the direct parent of the highlighted code elements. Useful for styling/assigning classNames.
37- `useInlineStyles` - if this prop is passed in as false, react syntax highlighter will not add style objects to elements, and will instead append classNames. You can then style the code block by using one of the CSS files provided by highlight.js.
38- `showLineNumbers` - if this is enabled line numbers will be shown next to the code block.
39- `showInlineLineNumbers` - if this is enabled in conjunction with `showLineNumbers`, line numbers will be rendered into each line, which allows line numbers to display properly when using renderers such as <a href="https://github.com/conorhastings/react-syntax-highlighter-virtualized-renderer">react-syntax-highlighter-virtualized-renderer</a>. (This prop will have no effect if `showLineNumbers` is `false`.)
40- `startingLineNumber` - if `showLineNumbers` is enabled the line numbering will start from here.
41- `lineNumberContainerStyle` - the line numbers container default to appearing to the left with 10px of right padding. You can use this to override those styles.
42- `lineNumberStyle` - inline style to be passed to the span wrapping each number. Can be either an object or a function that receives current line number as argument and returns style object.
43- `wrapLines` - a boolean value that determines whether or not each line of code should be wrapped in a parent element. defaults to false, when false one can not take action on an element on the line level. You can see an example of what this enables <a href="https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/diff.html">here</a>
44- `wrapLongLines` - boolean to specify whether to style the `<code>` block with `white-space: pre-wrap` or `white-space: pre`. [Demo](https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/)
45- `lineProps` - props to be passed to the span wrapping each line if wrapLines is true. Can be either an object or a function that receives current line number as argument and returns props object.
46- `renderer` - an optional custom renderer for rendering lines of code. See <a href="https://github.com/conorhastings/react-syntax-highlighter-virtualized-renderer">here</a> for an example.
47- `PreTag` - the element or custom react component to use in place of the default pre tag, the outermost tag of the component (useful for custom renderer not targeting DOM).
48- `CodeTag` - the element or custom react component to use in place of the default code tag, the second tag of the component tree (useful for custom renderer not targeting DOM).
49- `spread props` pass arbitrary props to pre tag wrapping code.
50
51```js
52import SyntaxHighlighter from 'react-syntax-highlighter';
53import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
54const Component = () => {
55 const codeString = '(num) => num + 1';
56 return (
57 <SyntaxHighlighter language="javascript" style={docco}>
58 {codeString}
59 </SyntaxHighlighter>
60 );
61};
62```
63
64### Prism
65
66Using <a href="https://github.com/wooorm/refractor">refractor</a> we can use an ast built on languages from Prism.js instead of highlight.js. This is beneficial especially when highlighting jsx, a problem long unsolved by this module. The semantics of use are basically the same although a light mode is not yet supported (though is coming in the future). You can see a demo(with jsx) using Prism(refractor) <a href="https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/prism.html">here</a>.
67
68```js
69import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
70import { dark } from 'react-syntax-highlighter/dist/esm/styles/prism';
71const Component = () => {
72 const codeString = '(num) => num + 1';
73 return (
74 <SyntaxHighlighter language="javascript" style={dark}>
75 {codeString}
76 </SyntaxHighlighter>
77 );
78};
79```
80
81### Light Build
82
83React Syntax Highlighter used in the way described above can have a fairly large footprint. For those that desire more control over what exactly they need, there is an option to import a light build. If you choose to use this you will need to specifically import desired languages and register them using the registerLanguage export from the light build. There is also no default style provided.
84
85```js
86import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
87import js from 'react-syntax-highlighter/dist/esm/languages/hljs/javascript';
88import docco from 'react-syntax-highlighter/dist/esm/styles/hljs/docco';
89
90SyntaxHighlighter.registerLanguage('javascript', js);
91```
92
93You can require `PrismLight` from `react-syntax-highlighter` to use the prism light build instead of the standard light build.
94
95```jsx
96import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
97import jsx from 'react-syntax-highlighter/dist/esm/languages/prism/jsx';
98import prism from 'react-syntax-highlighter/dist/esm/styles/prism/prism';
99
100SyntaxHighlighter.registerLanguage('jsx', jsx);
101```
102
103### Async Build
104
105For optimal bundle size for rendering ASAP, there's a async version of prism light & light.
106This versions requires you to use a bundler that supports the dynamic import syntax, like webpack.
107This will defer loading of refractor (17kb gzipped) & the languages, while code splits are loaded the code will show with line numbers
108but without highlighting.
109
110Prism version:
111
112```js
113import { PrismAsyncLight as SyntaxHighlighter } from 'react-syntax-highlighter';
114```
115
116Highlight version
117
118```js
119import { LightAsync as SyntaxHighlighter } from 'react-syntax-highlighter';
120```
121
122#### Supported languages
123
124Access via the `supportedLanguages` static field.
125
126```js
127SyntaxHighlighter.supportedLanguages;
128```
129
130### Built with React Syntax Highlighter
131
132- [mdx-deck](https://github.com/jxnblk/mdx-deck) - MDX-based presentation decks
133- [codecrumbs](https://github.com/Bogdan-Lyashenko/codecrumbs) - Learn, design or document codebase by putting breadcrumbs in source code. Live updates, multi-language support, and easy sharing.
134- [Spectacle Editor](https://github.com/FormidableLabs/spectacle-editor) - An Electron based app for creating, editing, saving, and publishing Spectacle presentations. With integrated Plotly support.
135- [Superset](https://github.com/airbnb/superset) - Superset is a data exploration platform designed to be visual, intuitive, and interactive.
136- [Daydream](https://github.com/segmentio/daydream) - A chrome extension to record your actions into a [nightmare](https://github.com/segmentio/nightmare) script
137- [CodeDoc](https://github.com/B1naryStudio/CodeDoc) - Electron based application build with React for creating project documentations
138- [React Component Demo](https://github.com/conorhastings/react-component-demo) - A React Component To make live editable demos of other React Components.
139- [Redux Test Recorder](https://github.com/conorhastings/redux-test-recorder) - a redux middleware to automatically generate tests for reducers through ui interaction. Syntax highlighter used by react plugin.
140- [GitPoint](https://github.com/gitpoint/git-point) - GitHub for iOS. Built with React Native. (built using react-native-syntax-highlighter)
141- [Yoga Layout Playground](https://yogalayout.com/playground) - generate code for yoga layout in multiple languages
142- [Kibana](https://github.com/elastic/kibana) - browser-based analytics and search dashboard for Elasticsearch.
143- [Golangci Web](https://github.com/golangci/golangci-web)
144- [Storybook Official Addons](https://github.com/storybooks/storybook)
145- [Microsoft Fast DNA](https://github.com/Microsoft/fast-dna/)
146- [Alibaba Ice](https://github.com/alibaba/ice)
147- [Uber BaseUI Docs](https://github.com/uber-web/baseui)
148- [React Select Docs](https://github.com/JedWatson/react-select)
149- [Auto-layout](https://github.com/0123cf/auto-layout) - use flex layout
150- [npmview](https://github.com/pd4d10/npmview) - A web application to view npm package files
151- [Static Forms](https://www.staticforms.xyz) - Free HTML forms for your static websites.
152- [React DemoTab](https://github.com/mkosir/react-demo-tab) - A React component to easily create demos of other components
153- [codeprinter](https://github.com/jaredpetersen/codeprinter) - Print out code easily
154- [Neumorphism](https://www.neumorphism.io) - CSS code generator for Soft UI/Neumorphism shadows
155- [grape-ui](https://www.grapeui.com) - Component library using styled-system and other open source components.
156- [✅ Good Arduino Code](https://goodarduinocode.com) - A curated library of Arduino Coding examples
157
158If your project uses react-syntax-highlighter please send a pr to add!
159
160### License
161
162MIT
Note: See TracBrowser for help on using the repository browser.