Skip to content

Importing <pre> with trailing newline produces spurious <br><br> in code block #917

@lylo

Description

@lylo

Version: 0.9.0.beta

Description

When Lexxy imports HTML containing a code block (<pre>) whose text content ends with a trailing newline, the editor displays a spurious empty line at the end of the code block, represented internally as <br><br>.

Steps to reproduce

Load the following HTML into a Lexxy editor:

<pre data-language="plain">Last updated: {{ updated_at }}
</pre>

Note the trailing \n before </pre> - this is standard output from Redcarpet and other markdown parsers.

Expected behaviour

The code block renders as a single line with no trailing empty line:

Last updated: {{ updated_at }}

Actual behaviour

The code block renders with a spurious empty line at the end:

  Last updated: {{ updated_at }}
  

Root cause (according to Claude 😅)

When Lexical imports the <pre> element, it splits the text content on \n to build CodeLineNodes. content\n splits into ["content", ""] - two lines, the second being empty - which gets serialised as an extra <br><br>. A trailing newline inside a <pre> is semantically insignificant (analogous to how the HTML spec strips a leading newline in <pre>), so the importer should strip it.

Suggested fix

When importing a <pre> element, trim a single trailing \n from the text content before splitting into code lines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions