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.

Highlight.js strips whitespace in pre tag

See original GitHub issue

Describe 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:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
joshgoebelcommented, Aug 6, 2022

It’s the APIs you are using. Use textContent instead of innerText.

0reactions
joshgoebelcommented, Sep 14, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

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