-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Prettier support for .astro
files
#106
Conversation
9c64f7a
to
d3db440
Compare
<h1> | ||
Hello world!</h1> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
”Why would these files be so poorly formatted— Oh. Oh. I see. Carry on!”
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure how to test this, but I see tests added. This looks great for a first cut; I don’t have any feedback.
|
Did you mean to remove the social banner in this branch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was playing around with this locally, and there seem to be some edge cases with interpolations where running Prettier breaks the syntax.
E.g.
Input:
---
---
<p>
{"hello world! this is a really long string. I sure hope it survives the formatter"}
</p>
Output:
---
---
<p>
{"hello world! this is a really long string. I sure hope it survives the
formatter"}
</p>
No, those commits were on |
Oh no! Good catch. I'm really hoping we don's have to implement much custom printing logic, but it may be the only way around stuff like this. I'll have to check how the official JSX formatting handles expressions! |
54bde73
to
2efe3d1
Compare
"node": "~14.0.0", | ||
"npm": ">=6.14.0 <7.0.0" | ||
"node": ">=14.0.0", | ||
"npm": ">=6.14.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad you made this change. Was thinking the same
0d4f99d
to
817d844
Compare
.astro
files.astro
files
Sorry for digging up an old conversation, but this might be related. I'm experiencing an issue where I will write the following in a ---
import { Button } from 'somewhere'
---
<Button>Hello world</Button> I write the above, but when I save / run <button>Hello world</button> Notice now the Any pointers? |
Changes
Closes #103. Adds
prettier-plugin-astro
to format.astro
files.We'll probably want to revisit this prior to launch, as this is not the most elegant solution in the world. But it works with minimal development effort!
This implementation attempts to leverage built-in prettier behavior as much as possible. It does this by doing a
parse
of the.astro
file, then converting any expressions inside of HTML to a<script>
to allow Prettier to do it's thing. Then we walk the generated HTML doc to make a few modifications to the output (like replacing the<script>
tags with{
and}
anddedent
ing the content.)Todo
Node
start
andend
within documentAstroContent
into HTML fragments and JSX expressionsTesting
Tests have been added to
tests/astro-prettier.test.js
. You can run just these tests usingnpm test -- prettier
.Docs