简体   繁体   中英

Use code-prettify with rendered content reactjs

I am using reactjs to render post. In my post, I have some tag <code></code> . So I want to display code for everyone easy to see.

I render my post like this with reactjs.

<div className="post-body" dangerouslySetInnerHTML={{
                    __html: this.state.post.content.rendered
                  }} />

I installed code-prettify from this.

https://www.npmjs.com/package/code-prettify

So how can I use pretty-code ?

Here is my content render json:

"content": {
        "rendered": "<p>In blogger\/blogspot you can you the conditional tags to make custom for posts, pages, archive page, label and search page, even a specific post.<\/p>\n<h2>I.List of conditional tags you can use in your Blogger theme.<\/h2>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>For Index page (Homepage, label pages and archive pages)\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.pageType == \"index\"'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For posts<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.pageType == \"index\"'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For pages<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.pageType == \"static_page\"'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For archive pages<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.pageType == \"archive\"'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For homepage<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.url == data:blog.homepageUrl'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For specific url<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.url == \"PUT_URL_HERE\"'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For search page<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.searchLabel'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<li>\n<div class=\"toolbar\">For error page<\/div>\n<div class=\"toolbar\"><code class=\"prettyprint\">&lt;b:if cond='data:blog.pageType == \"error_page\"'&gt;<br \/>\n&lt;\/b:if&gt;<\/code><\/div>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2>II. How to use?<\/h2>\n<p>You can use like this:<br \/>\nExample: In home page, I want to make a different style with other pages\/posts. I will use css inside conditional tags.<br \/>\n<code class=\"prettyprint\"><br \/>\n&lt;b:if cond='data:blog.url == data:blog.homepageUrl'&gt;<br \/>\n&lt;style&gt;<br \/>\nbody {font-size:20px; background:#f1f1f1}<br \/>\n&lt;\/style&gt;<br \/>\n&lt;script&gt;<br \/>\n\/\/&lt;![CDATA[<br \/>\n$('body').addClass('homepage')<br \/>\n\/\/]]&gt;<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/b:if&gt;<br \/>\n<\/code><\/p>\n<p>You also can use else if not homepage.<\/p>\n<p><code class=\"prettyprint\"><br \/>\n&lt;b:if cond='data:blog.url == data:blog.homepageUrl'&gt;<br \/>\n&lt;style&gt;<br \/>\nbody {font-size:20px; background:#f1f1f1}<br \/>\n&lt;\/style&gt;<br \/>\n&lt;script&gt;<br \/>\n\/\/&lt;![CDATA[<br \/>\n$('body').addClass('homepage')<br \/>\n\/\/]]&gt;<br \/>\n&lt;\/script&gt;<\/code><\/p>\n<p>&lt;b:else\/&gt;<br \/>\n&lt;!&#8211; Code for other pages here &#8211;&gt;<br \/>\n&lt;\/b:if&gt;<\/p>\n",
        "protected": false
    },

The docs are quite clear on how to use it, just include the script and add a prettyprint class to the pre tag

<pre class="prettyprint">

Here is a running example:

 const codeFromServer = `<code> function add(n1,n2){ console.log(x + y); } const x = 8; const y = 10; add(x,y); </code> `; class App extends React.Component{ render(){ return( <pre className="prettyprint" dangerouslySetInnerHTML={{ __html: codeFromServer }}></pre> ); } } ReactDOM.render(<App/>, document.getElementById('root')); 
 <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root" /> 

As Sagiv bg mentioned there is a way to run it on demand, eg on page render like in the code snippet below. Also it is demonstrated how you can inject lib dynamically.

 class CodePrettyComponent extends Component { static getHeadOrBody() { return document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]; } static codePrettify() { // Check if PR object already exists if (typeof PR !== 'undefined') { return; } // Inject if not CodePrettyComponent.getHeadOrBody().appendChild( Object.assign( document.createElement('script'), { type: 'text/javascript', async: true, src: 'https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js' } ) ); } componentDidMount() { // Injecting lib into document CodePrettyComponent.codePrettify(); // Waiting for lib to be loaded and invoke setTimeout(() => { PR.prettyPrint(); }, 1500); } render() { return ( <pre class = "prettyprint" > <code class = "language-groovy" > <!-- your code here --> </code> </pre> ); }; }; 
 <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> 

For rendering HTML in React use html-to-react . With parseWithInstructions() you can filter out potentially risky candidates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM