Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism.js html examples in React

So I'm switching an internal style guide for a site we're working on from regular html to use reactjs. I've got example code and I'm using highlighting with prism.js. The highlighting seems to work fine, but line breaks don't. Even putting in br tags after every line has no effect. Anyone have any thoughts on this? Just some example code:

var Example = React.createClass({
    render: function() {
        return (
          <div class="highlight">
              <pre>
                 <code class="language-markup">
                     &lt;label class=&quot;select&quot;&gt;
                     &lt;select class=&quot;selector&quot;&gt;
                            &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
                            &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
                            &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
                            &lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;
                            &lt;option value=&quot;5&quot;&gt;5&lt;/option&gt;
                         &lt;/select&gt;
                     &lt;/label&gt;
                 </code>
             </pre>
         </div>
    );
  }      
});

React.render(<Example />, document.getElementById('example'));

When it renders it looks like this.

<label class="select"><select class="selector"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select></label>

But I expect it to look like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/0.0.1/prism.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/0.0.1/prism.min.css" rel="stylesheet"/>
<div class="highlight">
                  <pre>
                     <code class="language-markup">
                         &lt;label class=&quot;select&quot;&gt;
                         &lt;select class=&quot;selector&quot;&gt;
                                &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
                                &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
                                &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
                                &lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;
                                &lt;option value=&quot;5&quot;&gt;5&lt;/option&gt;
                             &lt;/select&gt;
                         &lt;/label&gt;
                     </code>
                 </pre>
             </div>

Anyone know of a way to preserve the line breaks?

like image 295
SnareHanger Avatar asked Apr 17 '15 17:04

SnareHanger


People also ask

How do you use Prism JS in react?

How to get Prism working with create-react-app — super quick. import React from "react"; import Prism from "prismjs"; Step 3: Select your preferred theme type from the prism website — ( I like Okadia) and hit that rather large DOWNLOAD CSS button at the foot of the page.

What is the use of PrismJS?

Syntax Highlighting with PrismJS Prism is a lightweight, extensible syntax highlighter that can be used when working with code blocks in markdown files in blog posts.

How do you highlight codes in react?

You need to pass a render prop to the Highlight component as in the example below. The above code block illustrates how you can highlight a simple code block with the prism-react-renderer package. The Highlight component passes several arguments to the render prop.

What is Prism in coding?

pdf [411KB], which is a compilation of past slides.) PRISM is a general programming language intended for symbolic-statistical modeling. It is a new and unprecedented programming language with learning ability for statistical parameters embedded in programs.


2 Answers

You can just code like this:

var Example = React.createClass({
    render: function() {
        return (
          <div class="highlight">
              <pre>
                 <code class="language-markup">
                 {`
                <label class="select">
                <select class="selector">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                 </select>
                </label>
                 `}
                 </code>
             </pre>
         </div>
    );
  }      
});

Demo is here.

The only shortage is the indent.

like image 97
John Xiao Avatar answered Nov 03 '22 05:11

John Xiao


First: Use ReactDOMServer.renderToStaticMarkup to render component to string

var inner = ReactDOMServer.renderToStaticMarkup(
    <label className="select">
        ...
    </label>
);

After Use JS Beautifier to prettify your string

inner = html_beautify(inner);

Finnaly Insert it to your code as string

<code className="language-markup">
   {inner}
</code>

Url: http://jsfiddle.net/ohwz5ry2/2/

like image 33
Umidbek Karimov Avatar answered Nov 03 '22 05:11

Umidbek Karimov