|
1 | | -# prettier-plugin-tailwindcss-anywhere |
| 1 | +# prettier-plugin-tailwindcss-anywhere |
| 2 | + |
| 3 | +[](https://github.com/ttskch/prettier-plugin-tailwindcss-anywhere/actions/workflows/ci.yaml?query=branch:main) |
| 4 | +[](https://codecov.io/gh/ttskch/prettier-plugin-tailwindcss-anywhere) |
| 5 | +[](https://www.npmjs.com/package/@ttskch/prettier-plugin-tailwindcss-anywhere) |
| 6 | +[](https://www.npmjs.com/package/@ttskch/prettier-plugin-tailwindcss-anywhere) |
| 7 | + |
| 8 | +A Prettier plugin for sorting TailwindCSS classes **in any HTML-like language, like Twig etc**⚡ |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```shell |
| 13 | +npm install -D prettier \ |
| 14 | + prettier-plugin-tailwindcss \ |
| 15 | + @ttskch/prettier-plugin-tailwindcss-anywhere |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +This plugin is intended to be used in conjunction with [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss). |
| 21 | + |
| 22 | +For example, by setting `.prettierrc` as follows, you can sort classes even in files of template engines such as [Twig](https://twig.symfony.com/) that cannot be processed by prettier-plugin-tailwindcss. |
| 23 | + |
| 24 | +```json5 |
| 25 | +// .prettierrc |
| 26 | +{ |
| 27 | + "plugins": ["prettier-plugin-tailwindcss", "@ttskch/prettier-plugin-tailwindcss-anywhere"], |
| 28 | + "overrides": [ |
| 29 | + { |
| 30 | + "files": "*.html.twig", |
| 31 | + "options": { |
| 32 | + "parser": "anywhere", |
| 33 | + } |
| 34 | + } |
| 35 | + ] |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Before |
| 40 | + |
| 41 | +```twig |
| 42 | +{% extends 'base.html.twig %} |
| 43 | + {% block content %} |
| 44 | + <div class="space-y-4 flex-col flex"> |
| 45 | + <div class="p-4 rounded-lg bg-blue-500 text-white rounded-lg p-4"> |
| 46 | + Hello, {{ name }}! |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + {% endblock %} |
| 50 | +{% endblock %} |
| 51 | +``` |
| 52 | + |
| 53 | +### After |
| 54 | + |
| 55 | +```diff |
| 56 | + {% extends 'base.html.twig %} |
| 57 | + {% block content %} |
| 58 | +- <div class="space-y-4 flex-col flex"> |
| 59 | ++ <div class="flex flex-col space-y-4"> |
| 60 | +- <div class="p-4 rounded-lg bg-blue-500 text-white rounded-lg p-4"> |
| 61 | ++ <div class="rounded-lg bg-blue-500 p-4 text-white"> |
| 62 | + Hello, {{ name }}! |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + {% endblock %} |
| 66 | + {% endblock %} |
| 67 | +``` |
| 68 | + |
| 69 | +## Options |
| 70 | + |
| 71 | +### `regex` |
| 72 | + |
| 73 | +The `regex` option allows you to specify the part that corresponds to the value of the class attribute using a regular expression. |
| 74 | + |
| 75 | +For example, if you have the following text: |
| 76 | + |
| 77 | +```twig |
| 78 | +<div class="space-y-4 flex-col flex {% if foo %}is-foo{% endif %}"> |
| 79 | +``` |
| 80 | + |
| 81 | +In this case, you can specify the following `regex` to sort only the part before `{% if foo %}`. |
| 82 | + |
| 83 | +```json5 |
| 84 | +{ |
| 85 | + "options": { |
| 86 | + "parser": "anywhere", |
| 87 | + "regex": "class=\"([^{}\"]*)(?:\"| {)", // <-- here |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +```diff |
| 93 | +- <div class="space-y-4 flex-col flex {% if foo %}is-foo{% endif %}"> |
| 94 | ++ <div class="flex flex-col space-y-4 {% if foo %}is-foo{% endif %}"> |
| 95 | +``` |
| 96 | + |
| 97 | +> [!NOTE] |
| 98 | +> Note that you need to surround the part you want to see as the class attribute value with `()` so that the plugin can backreference it. |
| 99 | +
|
| 100 | +## Getting involved |
| 101 | + |
| 102 | +```shell |
| 103 | +pnpm install |
| 104 | + |
| 105 | +# Develop... |
| 106 | + |
| 107 | +pnpm check |
| 108 | +pnpm test |
| 109 | +pnpm build |
| 110 | +``` |
| 111 | + |
| 112 | +Thanks! 🎉 |
0 commit comments