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.

Newlines are lost during formatting

See original GitHub issue

Describe the issue/behavior that seems buggy Preformatted code is lost after highlighting a block in 11.1. It seems that in version 10 br elements were inserted, and now they aren’t. Although I don’t know why that should matter in a pre block.

Sample Code or Instructions to Reproduce

I have a little library that uses highlight to embed source using web components. https://kag0.github.io/sauce/ You can see on the documentation page, it’s embedding snippets from an example html file in the repo https://kag0.github.io/sauce/example.html but they’re missing the line breaks.

To prove that they would normally have line breaks:

  1. clone the repo
  2. open example.html and change the script embed from https://kag0.github.io/sauce/sauce.js to the local sauce.js
  3. open sauce.js and comment out line 43 .then(_ => hljs.highlightBlock(code))
  4. open example.html in the browser, observe that line breaks and indentation are correct
  5. uncomment the line and observe that code is highlighted, but line breaks are gone

Expected behavior line breaks should be preserved

Additional context rel https://github.com/highlightjs/highlight.js/issues/2131 https://github.com/highlightjs/highlight.js/issues/3242

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
joshgoebelcommented, Jul 24, 2021

You may also want to look at textContent vs innerText… there are very different line handling behavior with the various APIs.

4reactions
yn-academiacommented, Apr 21, 2022

Just walked right into this one. I guess the existence of this issue is documentation in itself.

Regarding FAQ: I don’t know how often a question needs to arise in order to make it into FAQ, but it seems that multiple people have encountered this, or something related, in https://github.com/highlightjs/highlight.js/issues/1664

I agree that how to use the DOM API is outside the scope of using HLJS and its documentation.

However, people will continue opening Github issues for this in HLJS, because it’s not obvious right away that it’s DOM misuse. Having in the FAQ “before you create a github issue about missing highlights due to comments, or broken line breaks, or whitespace issues, or anything else, make sure you know the difference between innerText and textContent, and are using the latter” - that would at least prevent people from opening new Github issues for this project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do newline characters get lost when using command ...
Newlines get swapped out at some points because they are special characters. In order to keep them, you need to make sure they're...
Read more >
Line breaks are removed in posts made in plain text format
Method 1 - Disable the feature that removes extra line breaks · Open Outlook. · On the File tab, select Options. · In...
Read more >
Why are all newlines gone after PowerShell's Get-Content ...
In PowerShell 3.0 we now have a new dynamic parameter, Raw. When specified, Get-Content ignores newline characters and returns the entire contents of...
Read more >
Why are text file line breaks wrong, after the file is transferred ...
The most common formats are Unix and Windows format. A primary difference is that different character or sequence of characters is used to...
Read more >
How to Preserve Newlines, Line Breaks, and Whitespace in ...
Sometimes you want to display the content in the same format as typed in a textarea. For example, a text from a textarea...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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