Skip to content

Support for inline pre elements #16

Open

Description

@martincizek

collapse-whitespace behaves perfectly for block PRE elements, such as the default <PRE> tag.

But there are use cases where users need preformatted behavior on inline elements. For example, GitLab's CSS for the <code> tag is set as white-space: pre-wrap;. The browser behavior is then as follows, but collapse-whitespace is not consistent with that.

<div class="test"><code>four    spaces</code></div>
<div class="result"><code>four    spaces</code></div>
<div class="test">Space <code>no space</code> more space</div>
<div class="result">Space <code>no space</code> more space</div>
<div class="test">Space <code> leading code space</code> more space</div>
<div class="result">Space <code> leading code space</code> more space</div>
<div class="test">Space <code>trailing code space </code> more space</div>
<div class="result">Space <code>trailing code space </code> more space</div>
<div class="test">Space <code>trailing code space </code><b> more space in tag</b></div>
<div class="result">Space <code>trailing code space </code><b> more space in tag</b></div>
<div class="test">Space <code>trailing code space </code> <b> more space after code and tag</b></div>
<div class="result">Space <code>trailing code space </code> <b>more space after code and tag</b></div>
<div class="test"><b>Space before tag </b><code>my code</code></div>
<div class="result"><b>Space before tag </b><code>my code</code></div>
<div class="test"><b>Space before tag and code </b> <code>my code</code></div>
<div class="result"><b>Space before tag and code </b><code>my code</code></div>

Activity

  1. added 2 commits that reference this issue on Mar 24, 2020
    df0ab42
    aa847ab
  2. DmitriyAlergant commented on Dec 20, 2025

    @DmitriyAlergant

    Code copied to clipboard from highlightJs rendered blocks (example on their website: https://highlightjs.org/, explore raw clipboard html content by pasting to https://evercoder.github.io/clipboard-inspector/ ) is HTML sequence of only tags with heavy styling. There are no div's at all, and line breaks rely on white-space: pre; classes.

    Turndown conversion to markdown returns single-line, and does not have line breaks, let alone whitespace indentation. This is NOT currently controlled by the "preformattedCode" option which links to this issue from README.md. Therefore this issue remains unresolved.

    Use-Case: a Chat UI applications that wants to allow clipboard pasting rich content into the message box. Since we need to send a plain text or markdown to the LLMs, we are trying to use turndown for "best effort" content conversion to markdown at pasting time. So that the user could see how this content will look like to LLM, edit, etc. Primarily so that that chaptered, bullet-point, tabular content pasted from Gmail, Word, Google Docs, Notion, Slack etc preserved at least some structure.

    Impact: code copied&pasted from websites or web apps, and AI chatbots (and our own app) that happen to use highlightJs to format code, is losing both newlines and indentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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