question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<pre> line breaks get deleted when calling prism.highlightElement(code)

See original GitHub issue

I set up my HTML using the following snippet:

<div>
 <pre><code class=" language-js" contenteditable="true"></code></pre>
</div>

When I call prism.highlightElement(code); from the following TypeScript snippet:

import * as prism from "prismjs";

const code: HTMLElement = document.getElementsByTagName("code")[0];

prism.highlightElement(code);

… all line breaks in the <code> element get deleted when calling prism.highlightElement(code).

The result is that all code is getting displayed in a single line then:

before

multiple lines

after

one liner

I believe this is not supposed to happen?


Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
SetTrendcommented, Feb 27, 2019

The hook appears to be incorrect. The correct hook name is: before-highlight

Prism.hooks.add('before-highlight', function (env)
{
  env.code = env.element.innerText;
});
4reactions
RunDevelopmentcommented, Feb 26, 2019

innerText has a few other properties which make it less useful for Prism. It would also change the current behavior of Prism, so for some people out there, this might be a breaking change.

Also, you can use hooks to make Prism use innerText.

Prism.hooks.add('before-sanity-check', function (env) {
    env.code = env.element.innerText;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

PrismJS no line breaks - javascript - Stack Overflow
Not sure if anyone has come across this. I'm using PrismJS syntax highlighter to highlight code. Application is ...
Read more >
Trouble formating <pre> using Prism in Gatsby - Prismic People
gatsby-remark-prismjs Adds syntax highlighting to code blocks in markdown files using ... <pre> line breaks get deleted when calling prism.
Read more >
Prism
Adds special CSS classes for each keyword for fine-grained highlighting. Remove initial line feed. Removes the initial line feed in code blocks. Inline...
Read more >
Prism - GitHub Pages
Dead simple Include prism.css and prism.js, use proper HTML5 code tags ... highlightElement: function(element, async, callback) { // Find language var ...
Read more >
[Solved]-PrismJS no line breaks-Reactjs - appsloveworld
css file to using white-space: pre-wrap like this: Prism.plugins.NormalizeWhitespace.setDefaults({ 'remove-trailing': true, 'remove-indent': true, 'left-trim ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found