Skip to content

Commit

Permalink
support prettier range ignore via jinja comments (fixes #34)
Browse files Browse the repository at this point in the history
  • Loading branch information
anentropic authored and davidodenwald committed Aug 25, 2024
1 parent 5314c0b commit b6b45fc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ Using range ignores is the best way to tell prettier to igone part of files. Mos
<!-- prettier-ignore-end -->
```

Or using Jinja comments:
```jinja
{# prettier-ignore-start #}
<script>
window.someData = {{ data | safe }}
</script>
{# prettier-ignore-end #}
{# prettier-ignore-start #}
<style>
:root { --accent-color: {{ theme_accent_color }} }
</style>
{# prettier-ignore-end #}
```

## Options

This Plugin provides additional options:
Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
const NOT_FOUND = -1;

const regex =
/(?<node>{{(?<startDelimiterEx>[-+]?)\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*(?<endDelimiterEx>[-+]?)}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>\w+)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/;
/(?<node>{{(?<startDelimiterEx>[-+]?)\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*(?<endDelimiterEx>[-+]?)}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>\w+)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<ignoreBlock>(?:<!-- prettier-ignore-start -->|{# prettier-ignore-start #})[\s\S]*(?:<!-- prettier-ignore-end -->|{# prettier-ignore-end #}))|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>))/;

export const parse: Parser<Node>["parse"] = (text) => {
const statementStack: Statement[] = [];
Expand Down
34 changes: 34 additions & 0 deletions test/cases/ignore_jinja/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<html>
<script>
{
{
js;
}
}
</script>

<style>
/* {{ css }} */
</style>

{# prettier-ignore-start #}
<ul>
{%for item in seq%}
<li>
{{item}}
</li>
{%endfor%}
</ul>
{# prettier-ignore-end #}

<!-- prettier-ignore -->
<div class="{{class}}" >hello world</div >
<div class="{{ class }}">hello world</div>

{% if foo %}
<p>
<!-- prettier-ignore -->
{{item}}
</p>
{% endif %}
</html>
30 changes: 30 additions & 0 deletions test/cases/ignore_jinja/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<script>
{{js}}
</script>

<style>
/* {{ css }} */
</style>

{# prettier-ignore-start #}
<ul>
{%for item in seq%}
<li>
{{item}}
</li>
{%endfor%}
</ul>
{# prettier-ignore-end #}

<!-- prettier-ignore -->
<div class="{{class}}" >hello world</div >
<div class="{{class}}" >hello world</div >

{%if foo%}
<p>
<!-- prettier-ignore -->
{{item}}
</p>
{%endif%}
</html>

0 comments on commit b6b45fc

Please sign in to comment.