Highlight.js strips whitespace in pre tag
See original GitHub issueDescribe the issue/behavior that seems buggy
When calling hljs.highlightElement, it strips all whitespace.
Sample Code or Instructions to Reproduce
fun `editor$transformer`() { // called on page load
document.querySelector<HTMLDivElement>(".md-editor")?.let { editor ->
val input = editor.querySelector<HTMLTextAreaElement>(".md-editor-input")!!
val code = editor.querySelector<HTMLElement>(".md-editor-code")!!
input.addEventListener<InputEvent>("input") {
var text = input.value
if (text.isNotEmpty() && text.last() == '\n') {
text += " "
}
println(text)
code.innerText = text
hljs.highlightElement(code) // Removing this keeps whitespace as intended
Unit
}
}
}
<div class="row md-editor">
<div class="col-7">
<div class="content md-editor-out">
</div>
</div>
<div class="col-5">
<div class="content md-editor-wrapper">
<textarea class="md-editor-input" spellcheck="false"></textarea>
<pre class="md-editor-highlight" aria-hidden="true"><code class="md-editor-code language-markdown"></code></pre>
</div>
</div>
</div>
Expected behavior Highlight.js correctly keeps spaces.
Additional context Disabling highlighting preserves spaces.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Ignoring whitespace in code and pre tags - Stack Overflow
The whole point of a pre tag is to preserve the spacing that's displayed, including anything at the beginning of a line. This...
Read more >How to use highlight.js
We strongly recommend <pre><code> wrapping for code blocks. It's quite semantic and "just works" out of the box with zero fiddling. It is...
Read more >One blank line in the code block when using highlight.js #191
Whitespace inside <pre> elements is always respected by the browser, and output as-is. A newline is treated the same as any other whitespace...
Read more >Normalize Whitespace Prism plugins
By default the plugin trims all leading and trailing whitespace of every code block. It also removes extra indents and trailing whitespace on...
Read more >white-space - CSS: Cascading Style Sheets - MDN Web Docs
The white-space CSS property sets how white space inside an element is handled. ... pre-line, Preserve, Collapse, Wrap, Remove, Hang.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s the APIs you are using. Use textContent instead of innerText.
Closing until a test JSFiddle case can be provided. Right now this sounds like expected behavior that can be resolved by using clean text (no HTML) inside the block and using the
textContent
API exclusively.If you truly need BR, consider the use of a plugin or manually replacing the
<br>
with real line breaks BEFORE highlighting. Ref: https://github.com/highlightjs/highlight.js/issues/2559