Prism: Weird indentation issue

Created on 18 Mar 2015  Â·  10Comments  Â·  Source: PrismJS/prism

I've got syntax highlighting working, but all of my code gets indented. See here for what I mean: http://rwwagner90.github.io/ember-shepherd/

Has anyone seen this before and is there a fix?

Most helpful comment

The <code> element must not be indented inside the <pre> element.

i.e. You should do this:

<pre class="language-..."><code>your
code
here</code></pre>

instead of this:

<pre class="language-...">
    <code>your
code
here</code>
</pre>

All 10 comments

The <code> element must not be indented inside the <pre> element.

i.e. You should do this:

<pre class="language-..."><code>your
code
here</code></pre>

instead of this:

<pre class="language-...">
    <code>your
code
here</code>
</pre>

My IDE literally won't allow me to do this. I just put it all on one line, and it works, but that is not very good. It would be nice to ignore the indentation of the <code> tag and just look at the content inside. Is there a reason to not do that?

This really isn’t Prism’s problem, if I am not mistaken. Prism works by replacing the <code> block with highlighted content, and does not touch anything outside it. That includes newlines and white-spaces between the <pre> and <code> tags. So if your <code> block was indented, the highlighted content will be indented.

The best “solution” here might be some custom JavaScript to strip the extra content yourself, outside Prism. Or it can be done as a Prism plugin, I think.

Just made a quick hack of a plugin, available here. Should be straightforward to use.

@rwwagner90 @uranusjr There's already a well-tested script for that
https://github.com/leeoniya/preCode.js

@vkbansal I'm not sure preCode handles the spaces between the opening <pre> and the opening <code> by default, though.

Since this issue is not a problem with Prism, but the normal behaviour of <pre> tags, I'm closing it.

THANKS @vkbansal , you made my day !!

May not be a problem with Prism, but it's certainly a problem that Prism could solve, right? Seems like a pretty common use-case to expect code's whitespace to be formatted relative to it's wrapper div. I'd love for this feature to be reconsidered, even if easy external fixes are available.

@esr360 Hi! We built the Normalize Whitespace plugin to fix this common issue, indeed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brian-kephart picture brian-kephart  Â·  6Comments

benjaminBrownlee picture benjaminBrownlee  Â·  4Comments

markerikson picture markerikson  Â·  3Comments

huweihuang picture huweihuang  Â·  6Comments

myst729 picture myst729  Â·  6Comments