Question

I'm using Prism and its working well for CSS:

<pre><code class="language-css">p { color: red }</code></pre>

but i can't get it working for html:

<pre><code class="language-html"><p class="red">red text</p></code></pre>

I have 2 problems:

  1. < and > are represented as tags, not as text, but i could replace it by &lt; and &gt;

  2. More important, even replaced as shown in problem 1, the highliter will not highlight any code and everything is just black. Despite that it is working for CSS, whole code looks like this:

<!DOCTYPE html>
<html>
    <head>
        <link href="prism.css" rel="stylesheet" />
    </head>
    <body>
        <script src="prism.js"></script>
        <pre><code class="language-css">p { color: red }</code></pre>
    </body>
</html>

Thanks for any help.

Was it helpful?

Solution

Use <code class="language-markup"> to style html code.

Also, you only need to escape the beginning of the tags with &lt;, don't worry about the &gt; characters. The easiest way is to paste your html code into the pre tag, then perform a find and replace for all < characters.

This should work:

<!DOCTYPE html>
<html>
    <head>
        <link href="prism.css" rel="stylesheet" />
    </head>
    <body>
        <script src="prism.js"></script>
        <pre><code class="language-markup">
            &lt;p class="red">red text &lt;/p>
        </code></pre>
    </body>
</html>

OTHER TIPS

To solve issue 1):

You can use the unexcaped-markup plugin

This is how it works:

<script type="text/plain" class="language-markup">
   <p>Example</p>
</script>

To ignore first and last returns I would recommend using the normalize whitespace plugin.

To solve issue 2):

There exists no languages-html see http://prismjs.com/index.html#languages-list. HTML is a HyperTextMarkupLanguage so its included in language-markup. Thats what you have to use.

I had the same problem with the HTML. I didn't want to replace the <, > with &lt &gt so what I did was put the code inside textarea-elements inside a hidden div and once the page was loaded copied the contents of all textareas to code-elements. This way I was able to keep the code as it is and render it withou issues.

The obvious downside of course is that without JS there is no content but then again the highlighter wouldn't work either.

I want to add that another very simple possibility is to use the archaic <xmp>-tag like so:

<pre><code class="language-html">
<xmp>
  <p>this markup is now rendered as expected although your IDE might be upset about you using that old tag</p>
</xmp>
</code></pre>

The best solution is to save the html you want highlighted into a seperate file. You'll need to include the file highlight plugin into your js.

The syntax highlighting will be worked out from the extension

<pre data-src="assets/partials/picture.html"></pre>

I actually ran into same issue where i placed prism js and prism css on html page but couldn't get the highlighting. Not sure if it will be the issue with others but for me it was some global css that was set as color black for all "code","pre" tags. After i removed that color from there prism highlighting started showing. If it still doesn't work than you can follow this article Code syntax higlighter using Prism js. I couldn't see much difference but i was having issue when i placed prism js below the code i wanted to highlight. When i moved that to top it started working. If anyone have issue they can try this.

I think Adam's answer should be the best solution in this case. I just elaborate more on the use for the unexcaped-markup plugin to make the HTML highlighter in the simplest way.

Since <script /> will only work on single line of code, therefore you may want to insert simple HTML-comment to escape all of your codes:

<pre class="language-markup"><code><!--
  <p>Example</p>
  <p>Example</p>
  <p>Example</p>
--></code></pre>

Everything inside the comment would be printed prettily. It's worth a try if you need to print you HTML markup by using Prism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top