posthtml-include icon indicating copy to clipboard operation
posthtml-include copied to clipboard

Trim line breaks

Open andreyvolokitin opened this issue 7 years ago • 12 comments

It makes sense to remove line breaks from start and end of the included tree (especially considering that line break at the end is often inserted automatically by code editor)

[
  '\n',
  {tag: 'a'},
  '\n'
]

or

[
  '\n some text \n'
]

andreyvolokitin avatar Apr 07 '18 09:04 andreyvolokitin

there should be no ambiguity in parsing

Scrum avatar Apr 09 '18 06:04 Scrum

What do you mean?

andreyvolokitin avatar Apr 09 '18 06:04 andreyvolokitin

Currently, I'm trying to do this in posthtml-extend and it looks pretty easy. I will make a PR there and then we'll see. The main point is that the module/include should not dictate vertical whitespace outside its own boundaries, because it is simply a module, and such formatting should be done within the main file which includes this module, i.e. this code:

<h2>Head</h2>
<include src="module.html"></include>
<h2>Head</h2>

should end up like this:

<h2>Head</h2>
module contents
<h2>Head</h2>

and not like this:

<h2>Head</h2>

module contents

<h2>Head</h2>

andreyvolokitin avatar Apr 09 '18 07:04 andreyvolokitin

if it's part of posthtml-include then you are right

Scrum avatar Apr 09 '18 07:04 Scrum

Check this out: https://github.com/posthtml/posthtml-extend/pull/14

andreyvolokitin avatar May 01 '18 21:05 andreyvolokitin

There's a potential smell to this request. See normalize-html-whitespace.

stevenvachon avatar May 01 '18 21:05 stevenvachon

There is mainly vertical whitespace / linebreaks problem (specifically with posthtml-include, which is different to posthtml-extend). The leading/trailing line breaks for the first/last text node should be removed somehow (and not replaced by a single space as normalize-html-whitespace suggests). Because the contents of include.html as such: Image of Yaktocat ...shouldn't dictate its own vertical formatting. Also trailing line break is often added automatically/unintentionally

andreyvolokitin avatar May 01 '18 22:05 andreyvolokitin

I think it’s suitable for this case posthtml-beautify

Scrum avatar Oct 10 '19 09:10 Scrum

Then it might be impossible to keep the original project indentation intact (see https://github.com/posthtml/posthtml-beautify/issues/220 and https://github.com/posthtml/posthtml-beautify/pull/222) if we will try to fix this later with another tool like posthtml-beautify, because we will not know if these line breaks should be removed (if they are from posthtml-include) or not (if they are set by developer)

andreyvolokitin avatar Oct 10 '19 09:10 andreyvolokitin

@andreyvolokitin I’ll try to figure it out, so far I don’t really understand how it should work

Scrum avatar Oct 10 '19 12:10 Scrum

I think I've done something similar for posthtml-extend: https://github.com/posthtml/posthtml-extend/pull/14/files, though I'm not sure if it can be applied to posthtml-include. Generally I think that it should remove line breaks from the start and the end of the tree (so the leading line break for the first node, and the trailing line break for the last node, or both if the tree consists of a single node)

andreyvolokitin avatar Oct 10 '19 12:10 andreyvolokitin

Any chance to get this fixed in posthtml-include? I agree with Andrey that the included snippet shouldn't contain extra newlines beyond its bounds. Text editors do tend to add a single trailing newline to files.

adrian5 avatar Jul 11 '23 13:07 adrian5