wcoder / highlightjs-line-numbers.js

Line numbering plugin for Highlight.js
https://wcoder.github.io/highlightjs-line-numbers.js/
MIT License
544 stars 126 forks source link

Line indents not displaying in code blocks #90

Closed PhalanxHead closed 2 years ago

PhalanxHead commented 2 years ago

Description When rendering a standard HTML page, any indentation in the code block is removed, and the entire code segment is displayed with the same column index.

In Essence, I expect:

fn main() {
    println!("Hello, World!");
    if(true) {
        println!("It is me!");
    }
}

But I'm getting

fn main() {
println!("Hello, World!");
if(true) {
println!("It is me!");
}
}

To Reproduce Copy the sample at the end into a raw html file and open it in your browser.

Expected behavior The code block should preserve formatting, including indentation.

Desktop:

Additional context Presumably, by putting <code> segments into a HTML table, it is removing whitespace. I've edited my code blocks to replace any double spaces with &emsp;, but this is not optimal for copying code back out.

Sample indicating bugged page

<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/default.min.css">
    <style>
        /* for block of numbers */

        .hljs-ln-numbers {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            text-align: center;
            color: #ccc;
            border-right: 1px solid #CCC;
            vertical-align: top;
            padding-right: 5px;
            /* your custom style here */
        }
        /* for block of code */

        .hljs-ln-code {
            padding-left: 10px;
        }
    </style>
</head>

<body>
    <main>
        <h1>This is a sample document</h1>
        <p>This will display the alignment issues</p>
        <pre><code>
            fn main() {
                println!("Hello, World!");
                if(true) {
                    println!("It is me!");
                }
            }
        </code></pre>
    </main>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/languages/rust.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script>
<script>
    hljs.highlightAll();
    hljs.initLineNumbersOnLoad();
</script>
wcoder commented 2 years ago

Thanks for the report!

Try to use full described HTML page with <!DOCTYPE html> & <html>.

The example above works in any browser: https://jsbin.com/quhubay/edit?html,output

PhalanxHead commented 2 years ago

Oh wow I feel like such a noob 🤣 Can you tell I'm usually a backend developer? :p