Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism HTML highlighter

Tags:

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.

like image 327
kajacx Avatar asked Jan 28 '13 09:01

kajacx


People also ask

How do you use Prism syntax highlighter?

Go to the Prism project homepage (https://prismjs.com/), then to the download page as before, and generate a script file selecting only the coding languages you are interested in; don't reselect any plugins. Then download the JavaScript file. Remember: do not download the CSS file, only the prism. js file.

What is PrismJS used for?

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.

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.

How do you use PrismJS in react?

babelrc file, create one in the root folder of your source code and add the following JSON to the . babelrc file. Step 3: Create a simple react component code. js and import PrismJS into your react component and configure the component to read the language props from App.


2 Answers

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> 
like image 153
Nathan Jones Avatar answered Oct 04 '22 00:10

Nathan Jones


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.

like image 37
Adam Avatar answered Oct 03 '22 22:10

Adam