0

我正在使用 reactjs 来渲染帖子。在我的帖子中,我有一些标签<code></code>。所以我想展示code给大家容易看到。

我用 reactjs 渲染我的帖子。

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

我从这里安装了代码美化。

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

那么我该如何使用pretty-code呢?

这是我的内容渲染 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
    },
4

2 回答 2

1

文档非常清楚如何使用它,只需包含脚本并在标签中添加一个prettyprintpre

<pre class="prettyprint">

这是一个运行示例:

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" />

于 2018-12-22T16:48:28.853 回答
0

正如Sagiv bg 提到的,有一种方法可以按需运行它,例如在页面渲染上,如下面的代码片段所示。它还演示了如何动态注入 lib。

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>

要在 React 中渲染 HTML,请使用html-to-react. 有了parseWithInstructions()您,您可以过滤掉有潜在风险的候选人。

于 2019-09-16T13:16:26.987 回答