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

propsal: better support for comments in templ files #79

Closed
a-h opened this issue Apr 27, 2023 · 12 comments
Closed

propsal: better support for comments in templ files #79

a-h opened this issue Apr 27, 2023 · 12 comments
Labels
enhancement New feature or request good first issue Good for newcomers hacktoberfest Good issue for hacktoberfest.

Comments

@a-h
Copy link
Owner

a-h commented Apr 27, 2023

Currently, HTML comments and Go comments are not well supported. Would be good to have them as an option.

// Support this comment, don't include anything in the HTML.
/* Do not support this syntax for ... reasons */
<!-- Support this syntax and include it in the output -->
@gedw99
Copy link

gedw99 commented Jun 5, 2023

Yeah this would be nice

just means altering the parser to skip it ?

@a-h
Copy link
Owner Author

a-h commented Jun 9, 2023

@gedw99 - Yes, but not skip it, add it as a new node type, so that it's retained during reformatting.

I was thinking of basically the same sort of implementation as https://github.com/a-h/templ/blob/main/parser/v2/raw.go (script and style HTML elements) for the multi-line HTML comments, which just looks for the start and end tokens (i.e. if you see <!-- read until -->).

For the // style, there'd be a new node type of comment. The parser would match any line starts with that whitespace followed by //, and then skip until the end of the line.

@tantalic
Copy link
Contributor

@a-h I took a pass at implementing the first part of this proposal in #161 . I would be happy to take feedback on that PR. I would also be interested in helping with the // comment style following this as well.

PS - Thanks for the great project!

@aranw
Copy link
Contributor

aranw commented Sep 29, 2023

I noticed that the parser can't handle HTML in comments for example

<!-- <div> hello world </div> -->

will produce a error such as

parsing error: <div>: expected end tag not present or invalid tag contents: line 20, col 16

@a-h
Copy link
Owner Author

a-h commented Sep 29, 2023

Hi @aranw 👋🏻 nice to see you again, hope you're doing well!

Are you using the latest commit? The latest tagged version doesn't have these changes in it yet. I added a new test based on what you were seeing and couldn't reproduce the issue: https://github.com/a-h/templ/actions/runs/6354142654

@aranw
Copy link
Contributor

aranw commented Sep 29, 2023

@a-h 👋🏻 I'm using tagged version v0.2.334

I'll try make a reproducible test in a fork give me a few minutes

@a-h
Copy link
Owner Author

a-h commented Sep 29, 2023

I was planning to do a new release, quite a few improvements in since the last tagged release.

@aranw
Copy link
Contributor

aranw commented Sep 29, 2023

@a-h the following example here breaks when I run generate

Edit: I did it like this as I wasn't sure exactly where in the parser code a test for this would make sense. I've not really looked through the source code too much yet. Mostly just been experimenting with converting go templates to templ right now

@a-h
Copy link
Owner Author

a-h commented Sep 29, 2023

I'm not convinced you're running the latest (non-tagged) version of the templ binary, since I'm struggling to reproduce that on the latest version.

Screenshot 2023-09-29 at 17 33 17

The integration tests for HTML in comments are also passing:

efd3312

If you want to try the latest version, you can do go install github.com/a-h/templ/cmd/templ@efd33120d9e83e5b9c9c1b06272d5bfcce246436 and it will install that specific commit. Worth also checking which templ to make sure you're running the version you think you're running!

@aranw
Copy link
Contributor

aranw commented Sep 29, 2023

Ah yeah I'm not running the latest version that has definitely fixed the issue!

@a-h
Copy link
Owner Author

a-h commented Sep 30, 2023

For Go comments within the template section, I think this might be the place to start... https://github.com/a-h/templ/tree/issue_159_go_comments_before_package Was aiming to solve issue 159, and there's some crossover with this.

@tantalic - have you started on this one yet? Feel free to disregard my thoughts or work on top if you want to pick it up. Just let me know!

@a-h a-h added enhancement New feature or request good first issue Good for newcomers hacktoberfest Good issue for hacktoberfest. labels Sep 30, 2023
@tantalic
Copy link
Contributor

tantalic commented Oct 5, 2023

@a-h I have not started anything on go style comments, at least not yet. The next couple of weeks are pretty busy so I don't think I will be jumping in soon on this if anyone else wants to work on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers hacktoberfest Good issue for hacktoberfest.
Projects
None yet
Development

No branches or pull requests

4 participants