node-html-to-text icon indicating copy to clipboard operation
node-html-to-text copied to clipboard

Cannot remove newlines between tags even with options

Open TrofinSorin opened this issue 1 year ago • 3 comments

Minimal HTML example

<h1>1</h1>
<h1>2</h1>
<h1>3</h1>

Options

{
        wordwrap: false,
        preserveNewlines: false,
        selectors: [
          { selector: 'p', options: { leadingLineBreaks: 0, trailingLineBreaks: 0 } },
          { selector: 'h1', options: { leadingLineBreaks: 0, trailingLineBreaks: 0 } },
          { selector: 'h2', options: { leadingLineBreaks: 0, trailingLineBreaks: 0 } },
          { selector: 'h3', options: { leadingLineBreaks: 0, trailingLineBreaks: 0 } },
        ]
      }

Observed output Before the selector options there were 2 newlines. But with those options it's reduced to one newline.

1 (newline here) 2 (newline here) 3

Expected output

I want this output without newlines

1
2
3

The same as inside wysiwig, without newlines but H1

Version information

  • html-to-text: 9.0.5
  • node: 18.19.0

TrofinSorin avatar Jan 24 '24 13:01 TrofinSorin

These options will work:

{
  selectors: [
    { selector: 'h1', options: { leadingLineBreaks: 1, trailingLineBreaks: 1 } },
  ]
}

What happens with zero: The condition 0 || 2 equates to the default value of 2.

Notice that this counts line breaks, not empty lines. Zero line breaks would mean "continue on the same line" which doesn't make sense for block level tags.

I agree that this logic might be somewhat less intuitive though. It should probably be clamped to the closest valid value and sent a warning when misused...

KillyMXI avatar Jan 24 '24 13:01 KillyMXI