g-plane / markup_fmt

Configurable HTML, Vue, Svelte, Astro, Angular, Jinja, Twig, Nunjucks and Vento formatter with dprint integration.
https://dprint.dev/plugins/markup_fmt/
MIT License
91 stars 4 forks source link

associate other file extensions #1

Closed disrupted closed 10 months ago

disrupted commented 10 months ago

What a cool new formatter! I've been trying it out with dprint and it works great, especially the ability to format embedded CSS and JS tags is brilliant. I was hoping to use this to format my Jinja2 HTML templates (also known as htmldjango filetype in Neovim). Here's an example:

index.jinja2

<!doctype html>
<html>
  <head>
    <title>Hello World</title>
    <style>
      body {
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>Hello {{ user.name }}</h1>
    {% for item in items %}
      <a>{{ item }}<a>
    {% endfor %}
  </body>
</html>

When I run dprint fmt "**/*.jinja2" --diff | delta dprint says No files found to format with the specified plugins. That makes sense, since markup_fmt expects files with the .html extension. As expected, it works with this trick cat index.jinja2 | dprint fmt --stdin html.

I was hoping to add associations for custom file extensions. I tried it with the following minimal config dprint.jsonc

{
    "$schema": "https://dprint.dev/schemas/v0.json",
    "incremental": true,
    "typescript": {},
    "malva": {},
    "markup": {
        "associations": [
            "**/*.html",
            "**/*.jinja2"
        ]
    },
    "includes": [
        "**/*.{html,jinja2}"
    ],
    "plugins": [
        "https://plugins.dprint.dev/typescript-0.88.3.wasm",
        "https://plugins.dprint.dev/g-plane/malva-v0.1.2.wasm",
        "https://plugins.dprint.dev/g-plane/markup_fmt-v0.1.1.wasm"
    ]
}

but I am getting the error

Error formatting index.jinja2. Message: unknown file extension of file: index.jinja2
g-plane commented 10 months ago

Adding associations is simple, but I'm not sure whether the code with template syntaxes (such as {% for ... %} and {% if ... %}) can be formatted well or not. Can you rename Jinja2 files to .html temporarily then let us check the format result?

disrupted commented 10 months ago

markup_fmt already does an excellent job. If I rename the extension to html or run cat index.jinja2 | dprint fmt --stdin html the output is perfectly formatted.

g-plane commented 10 months ago

Sounds good.

disrupted commented 10 months ago

thank you so much!