Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag autoTrim plugin #35

Merged
merged 14 commits into from
Feb 29, 2024
Merged

Tag autoTrim plugin #35

merged 14 commits into from
Feb 29, 2024

Conversation

wrapperup
Copy link
Contributor

@wrapperup wrapperup commented Feb 24, 2024

This adds a new plugin (unused by default) called autoTrim, which automatically trims away logic tags, or tags that don't render anything.

By default, the tags that are trimmed by default are:
set, if, else, for, function (and async), export, import, comments and JavaScript.

The plugin also takes options:

env.use(autoTrim({
  tags: [...]
});

If enabled, any of the listed tags will be trimmed away, as if it didn't exist in the markup. It respects newlines, so it only snips out the tag, while preserving your markup.

For example,

{{ if true }}
  Hello, {{ name }}!
  {{ set variable = 10 }}
{{ /if }}

would output

  Hello, Name!

instead of


  Hello, Name!
  

@oscarotero
Copy link
Collaborator

Thanks!
Now that we have the token preprocessors, I was thinking of moving the space trimming to a preprocessor. So probably I need to do this change before implementing this.

Also, I found some issues with this implementation:

  • If doesn't work with tags without spaces at the beginning or end. For example {{ varname }} works but {{varname}} doesn't.
  • It should work only for some tags, not all. For example, conditional tags like for or if should be trimmed automatically but printing variables should't. What I would like to do is, with the following code:
<ul>
  {{ for n of 3 }}
  <li>
    Number: {{ n }}
  </li>
  {{ /for }}
</ul>

Generate the following input (removing the spaces generated by the for and/for tags, but not {{ n }}).

<ul>
  <li>
    Number: 1
  </li>
  <li>
    Number: 2
  </li>
  <li>
    Number: 3
  </li>
</ul>

Your implementation removes all spaces, even the spaces after the colon Number:1 instead of Number: 1.

<ul><li>
    Number:1</li><li>
    Number:2</li><li>
    Number:3</li></ul>

@wrapperup
Copy link
Contributor Author

wrapperup commented Feb 25, 2024

It should work only for some tags, not all. For example, conditional tags like for or if should be trimmed automatically but printing variables should't. What I would like to do is, with the following code:

I had the same thought, but it would require a change to tag plugins I think. The tokenizer needs to be aware of tags that are purely evaluated (if/else, for, set, etc), vs tags that output stuff (include, layout, value interpolation). So plugins that add tags would need to register their tags.

@oscarotero
Copy link
Collaborator

Hi. I saw that you update this PR to implement the autotrim option in the trim token preprocessor.
I think it should be a different preprocessor. The trim I've added just re-implement the current behavior under a preprocessor and it's enabled by default.

The autotrim option should be a different preprocessor, that the user can use but not enabled by default (at least for now). This is an example of how I imagine it:

const tmpl = vento();

tmpl.use(autotrim({
  tags: ["if", "/if", "for", "/for", "import", ...etc] // Default values
));
  • The plugin only trims some tags (not all).
  • No need to use + character to disable it.

The trim is different as with the - character, because it should trim only until a linebreak is found. For example:

<p>
  {{ if salute }}
  Hello
  {{ /if }}
</p>

The idea is to output this:

<p>
  Hello
</p>

So the trim should be:

  • before the tag: remove the whitespaces until a linebreak is found (but keep the linebreak)
  • after the tag: remove the whitespaces until a linebreak is found (and remove the linebreak)

@wrapperup wrapperup changed the title Add whitespace options and + trim marker Tag autoTrim plugin Feb 27, 2024
@wrapperup wrapperup requested a review from oscarotero February 29, 2024 05:01
@oscarotero oscarotero merged commit a913ced into ventojs:main Feb 29, 2024
1 check failed
@oscarotero
Copy link
Collaborator

Fantastic work. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants