1.41.1 • Published 2 months ago

blade-formatter v1.41.1

Weekly downloads
257
License
MIT
Repository
github
Last release
2 months ago

npm version GitHub Workflow Status npm NPM codecov

blade-formatter

An opinionated blade template formatter for Laravel that respects readability

blade-formatter

This project aims to provide a formatter for blade templates, as there is no official blade template formatter.

Try the online demo

Features

  • Automatically Indents markup inside directives

    blade-formatter-indent

  • Automatically add spacing to blade templating markers

    blade-formatter-spacing

  • PHP 8 support (null safe operator, named arguments) 🐘

    blade-formatter-php8

  • PSR-2 support (format inside directives)

    blade-formatter-format-in-directive

  • Automatic Tailwind CSS Class Sorting. see Options

  • Custom Directive support

Example

Input

@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
<section id="content">
<div class="container mod-users-pd-h">
    <div class="pf-user-header">
    <div></div>
    <p>@lang('users.index')</p>
    </div>
        <div class="pf-users-branch">
            <ul class="pf-users-branch__list">
                @foreach($users as $user)
        <li>
            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
            {{ link_to_route("frontend.users.user.show",$users["name"],$users['_id']) }}
        </li>
        @endforeach
      </ul>
      <div class="pf-users-branch__btn">
      @can('create', App\Models\User::class)
            {!! link_to_route("frontend.users.user.create",__('users.create'),[1,2,3],['class' => 'btn']) !!}
            @endcan
        </div>
  </div>
    </div>
</section>
@endsection
@section('footer')
@stop

Output

@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
    <section id="content">
        <div class="container mod-users-pd-h">
            <div class="pf-user-header">
                <div></div>
                <p>@lang('users.index')</p>
            </div>
            <div class="pf-users-branch">
                <ul class="pf-users-branch__list">
                    @foreach ($users as $user)
                        <li>
                            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
                            {{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }}
                        </li>
                    @endforeach
                </ul>
                <div class="pf-users-branch__btn">
                    @can('create', App\Models\User::class)
                        {!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!}
                    @endcan
                </div>
            </div>
        </div>
    </section>
@endsection
@section('footer')
@stop

Installation

$ npm install --save-dev blade-formatter
$ node_modules/.bin/blade-formatter -h

yarn

$ yarn add --dev blade-formatter

global

$ npm install -g blade-formatter
$ yarn global add blade-formatter

docker

$ docker run -it -v $(pwd):/app -w /app shufo/blade-formatter resources/**/*.blade.php

Usage

  • Basic
# This outputs formatted result to stdout
$ blade-formatter resources/**/*.blade.php
$ blade-formatter resources/layouts/app.blade.php
  • Check if template is formatted or not (makes no change)
$ blade-formatter app/** -d -c
Check formatting...
app/index.blade.php

Above file(s) are formattable. Forgot to run formatter? Use --write option to overwrite.
$ echo $?
1
  • Format files and overwrite
$ blade-formatter --write resources/**/*.blade.php
  • Show diffs
$ blade-formatter -c -d resources/**/*.blade.php

Options

  Options:
      --version                       Show version number  [boolean]
  -c, --check-formatted               Only checks files are formatted or not  [boolean] [default: false]
  -w, --write                         Write to file  [boolean] [default: false]
  -d, --diff                          Show diffs  [boolean] [default: false]
  -e, --end-with-newline              End output with newline  [boolean] [default: true]
      --end-of-line                   End of line character(s). [string] [choices: "LF", "CRLF"]
  -i, --indent-size                   Indentation size  [default: 4]
      --wrap-line-length, --wrap      The length of line wrap size  [default: 120]
      --wrap-attributes, --wrap-atts  The way to wrap attributes.
                                      [auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned]  [string] [default: "auto"]
  -M, --wrap-attributes-min-attrs     Minimum number of html tag attributes for force wrap attribute options. Wrap the first attribute only if 'force-expand-multiline' is specified in wrap attributes  [default: "2"]
  -I, --indent-inner-html             Indent <head> and <body> sections in html.  [boolean] [default: false]
      --sort-tailwindcss-classes      Sort tailwindcss classes  [boolean] [default: false]
      --tailwindcss-config-path       Specify path of tailwind config  [string] [default: null]
      --sort-html-attributes          Sort HTML attributes.  [string] [choices: "none", "alphabetical", "code-guide", "idiomatic", "vuejs", "custom"] [default: none]
      --custom-html-attributes-order  Comma separated custom HTML attributes order. To enable this you must specify sort html attributes option as `custom`. You can use regex for attribute names. [string] [default: null]
      --no-single-quote               Use double quotes instead of single quotes for php expression.  [boolean] [default: false]
  -E, --extra-liners                  Comma separated list of tags that should have an extra newline before them.  [string] [default: "head,body,/html"]
      --no-multiple-empty-lines       Merge multiple blank lines into a single blank line  [boolean] [default: false]
      --no-php-syntax-check           Disable PHP syntax checking  [boolean] [default: false]
      --no-trailing-comma-php         If set to true, no trailing commas are printed for php expression.  [boolean] [default: false]
  -p, --progress                      Print progress  [boolean] [default: false]
      --stdin                         Format code provided on <STDIN>  [boolean] [default: false]
      --config                        Use this configuration, overriding .bladeformatterrc config options if present  [string] [default: null]
      --ignore-path                   Specify path of ignore file  [string] [default: null]
  -h, --help                          Show help  [boolean]

Examples:
  blade-formatter "resources/views/**/*.blade.php" --write  Format all files in views directory

Configuring blade-formatter

To configure project wide settings, put .bladeformatterrc.json or .bladeformatterrc in your repository root, blade-formatter will treat them as settings files.

e.g.

{
  "indentSize": 4,
  "wrapAttributes": "auto",
  "wrapLineLength": 120,
  "wrapAttributesMinAttrs": 2,
  "indentInnerHtml": true,
  "endWithNewLine": true,
  "endOfLine": "LF",
  "useTabs": false,
  "sortTailwindcssClasses": true,
  "sortHtmlAttributes": "none",
  "noMultipleEmptyLines": false,
  "noPhpSyntaxCheck": false,
  "noSingleQuote": false,
  "noTrailingCommaPhp": false,
  "extraLiners": []
}

blade-formatter will search up the directory structure until it reaches the root directory.

Ignore Files

To ignore a specific file, put a `.bladeignore' in the root of your repository and the blade formatter will treat it as an ignored file.

e.g.

resources/views/users/index.blade.php
resources/views/products/*
resources/views/books/**/*

Disabling format in file

To disable formatting in your file, you can use blade comments in the following format:

{{-- blade-formatter-disable --}}
    {{ $foo }}
    {{ $bar }}
{{-- blade-formatter-enable --}}

To disable formatiing on a specific line, you can use comment in the following format:

{{-- blade-formatter-disable-next-line --}}
    {{ $foo }}

To disable formatting for an entire file, put a {{-- blade-formatter-disable --}} comment at the beginning of the file:

{{-- blade-formatter-disable --}}

{{ $foo }}

API

You can also use the blade formatter via API.

const { BladeFormatter } = require('blade-formatter');

const input = `
<html>
  <body>
    <p>foo</p>
  </body>
</html>
`;

const options = {
  indentSize: 4,
  wrapAttributes: "auto",
  wrapLineLength: 120,
  endWithNewLine: true,
  useTabs: false,
  sortTailwindcssClasses: true,
};

new BladeFormatter(options).format(input).then((formatted) => {
  console.log(formatted);
});

ESModule

import { Formatter } from "blade-formatter";

const input = `
<html>
  <body>
    <p>foo</p>
  </body>
</html>
`;

const options = {
    indentSize: 2,
};

new Formatter(options).formatContent(input).then((formatted) => {
    console.log(formatted);
});

Extensions

Troubleshoot

  • If you encounter the following error during installation
$ npm install -g blade-formatter
~~
current user ("nobody") does not have permission to access the dev dir
~~

Try setting the global user as root

$ npm -g config set user root
  • If you encounter the following message like below when sorting TailwindCss class sorting enabled
  message: 'module is not defined in ES module scope\n' +
// or
export default {
^^^^^^
SyntaxError: Unexpected token 'export'

then you should check your nodejs module type is matched with tailwindcss.config.js.

ESM

package.json

"type": "module"

tailwind.config.js

export default {
  ~~~
}

CommonJS

tailwind.config.js

module.exports = {
  ~~~
}

TODO

  • custom directives
  • @for directive support
  • ignore formatting in blade comment
  • automatically add new line after directive

Development

$ yarn install
$ yarn run watch # watch changes

You can use local docker image for development. It might help if the host OS is not an amd64 architecture.

$ make build
$ make run example.php

Testing

$ yarn install
$ yarn run test

You can use local docker image for testing. It might help if the host OS is not an amd64 architecture.

$ make build
$ make test
$ make debug # attach

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

LICENSE

MIT

1.41.1

2 months ago

1.41.0

2 months ago

1.40.0

3 months ago

1.40.1

3 months ago

1.33.1

10 months ago

1.34.0

10 months ago

1.34.1

10 months ago

1.35.1

9 months ago

1.35.0

9 months ago

1.36.0

9 months ago

1.37.0

9 months ago

1.37.1

8 months ago

1.38.2

7 months ago

1.38.3

7 months ago

1.38.0

8 months ago

1.38.1

8 months ago

1.38.6

6 months ago

1.38.4

7 months ago

1.38.5

7 months ago

1.33.0

10 months ago

1.32.13

11 months ago

1.32.12

1 year ago

1.32.11

1 year ago

1.32.10

1 year ago

1.32.8

1 year ago

1.32.9

1 year ago

1.32.6

1 year ago

1.32.7

1 year ago

1.32.1

1 year ago

1.32.4

1 year ago

1.32.5

1 year ago

1.32.2

1 year ago

1.32.3

1 year ago

1.32.0

1 year ago

1.30.2

2 years ago

1.30.3

2 years ago

1.30.0

2 years ago

1.30.1

2 years ago

1.31.1

1 year ago

1.31.0

2 years ago

1.29.0

2 years ago

1.29.1

2 years ago

1.29.4

2 years ago

1.29.5

2 years ago

1.29.2

2 years ago

1.29.3

2 years ago

1.29.8

2 years ago

1.29.6

2 years ago

1.29.7

2 years ago

1.28.0

2 years ago

1.26.7

2 years ago

1.26.8

2 years ago

1.26.9

2 years ago

1.26.15

2 years ago

1.26.14

2 years ago

1.26.17

2 years ago

1.26.16

2 years ago

1.26.11

2 years ago

1.26.10

2 years ago

1.26.13

2 years ago

1.26.12

2 years ago

1.27.2

2 years ago

1.27.3

2 years ago

1.27.0

2 years ago

1.27.1

2 years ago

1.27.6

2 years ago

1.27.7

2 years ago

1.27.4

2 years ago

1.27.5

2 years ago

1.25.0

2 years ago

1.25.1

2 years ago

1.25.4

2 years ago

1.25.2

2 years ago

1.25.3

2 years ago

1.26.0

2 years ago

1.26.3

2 years ago

1.26.4

2 years ago

1.26.1

2 years ago

1.26.2

2 years ago

1.26.5

2 years ago

1.26.6

2 years ago

1.24.0

2 years ago

1.23.2

2 years ago

1.23.3

2 years ago

1.23.1

2 years ago

1.23.6

2 years ago

1.23.7

2 years ago

1.23.4

2 years ago

1.23.5

2 years ago

1.23.8

2 years ago

1.21.0

2 years ago

1.21.1

2 years ago

1.21.4

2 years ago

1.21.5

2 years ago

1.21.2

2 years ago

1.21.3

2 years ago

1.22.0

2 years ago

1.22.3

2 years ago

1.22.1

2 years ago

1.22.2

2 years ago

1.23.0

2 years ago

1.20.5

2 years ago

1.20.1

2 years ago

1.20.2

2 years ago

1.20.0

2 years ago

1.20.3

2 years ago

1.20.4

2 years ago

1.18.1

2 years ago

1.18.0

2 years ago

1.16.2

2 years ago

1.14.4

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.18.5

2 years ago

1.18.4

2 years ago

1.18.3

2 years ago

1.18.2

2 years ago

1.18.6

2 years ago

1.15.0

2 years ago

1.19.0

2 years ago

1.17.2

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.15.2

2 years ago

1.15.1

2 years ago

1.17.5

2 years ago

1.17.4

2 years ago

1.17.3

2 years ago

1.14.1

2 years ago

1.14.3

2 years ago

1.14.2

2 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.12.5

3 years ago

1.12.4

3 years ago

1.12.3

3 years ago

1.12.2

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.11.13

3 years ago

1.11.12

3 years ago

1.11.11

3 years ago

1.11.10

3 years ago

1.11.9

3 years ago

1.11.8

3 years ago

1.11.7

3 years ago

1.11.6

3 years ago

1.11.5

3 years ago

1.11.4

3 years ago

1.11.3

3 years ago

1.11.2

3 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.7

3 years ago

1.10.6

3 years ago

1.10.5

3 years ago

1.10.4

3 years ago

1.10.3

3 years ago

1.10.2

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.9

3 years ago

1.9.8

3 years ago

1.9.7

3 years ago

1.9.6

3 years ago

1.9.5

3 years ago

1.9.4

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.1

3 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.6

4 years ago

1.2.0

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago