@shufo/prettier-plugin-blade

repository·main·Indexed 19 days ago

https://github.com/shufo/prettier-plugin-blade

A Prettier plugin for formatting Laravel Blade templates. It supports the Blade language via the .blade.php extension and provides features such as indentation for directives, PHP 8 syntax support, Tailwind CSS class sorting, and configurable HTML attribute wrapping and sorting.

Tokens
1.3K
Snippets
5
Records
10
Agent score
14%

What's inside @shufo/prettier-plugin-blade

  1. Configure Prettier Blade plugin options

    main

    The @shufo/prettier-plugin-blade plugin supports standard Prettier options as well as specific Blade-related configuration keys. You can configure these in your .prettierrc or equivalent Prettier configuration file.

    Standard Prettier Options

    These options define the basic formatting behavior:

    • tabWidth: Number of spaces per tab (default: 4).
    • printWidth: Maximum line width (default: 120).
    • singleQuote: Use single quotes for strings (default: true).

    Blade-Specific Options

    These options control how Blade templates and HTML/PHP elements are formatted.

  2. Reference: Other Blade formatting options

    main

    Additional configuration for Blade template formatting.

    OptionTypeDefaultDescription
    endWithNewLinebooleantrueWhether to end the output with a newline.
    noPhpSyntaxCheckbooleanfalseDisable PHP syntax checking.
    indentInnerHtmlbooleanfalseIndent <head> and <body> sections in HTML.
    extraLinersstring"head,body,/html"Comma-separated list of tags that should have an extra newline before them.
    componentPrefixstring"x-,livewire:"Comma-separated list of component prefixes used to preserve style in HTML attributes.
    trailingCommaPHPbooleantrueIf false, no trailing commas are printed for PHP expressions.
    phpVersionstring"8.4"The version of PHP to use for formatting.
  3. Reference: Blade HTML attribute sorting options

    main

    Control the order of HTML attributes using sortHtmlAttributes and customHtmlAttributesOrder.

    sortHtmlAttributes values:

    • none (default)
    • alphabetical
    • code-guide
    • idiomatic
    • vuejs
    • custom

    customHtmlAttributesOrder usage: When sortHtmlAttributes is set to custom, provide a comma-separated list of attribute names or regex patterns. Example: "id, aria-.+, class, src".

    {
      "sortHtmlAttributes": "custom",
      "customHtmlAttributesOrder": "id, aria-.+, class, src"
    }
  4. Reference: Blade attribute wrapping configuration

    main

    Detailed settings for controlling attribute wrapping behavior.

    OptionTypeDefaultDescription
    wrapAttributesstring"auto"The way to wrap attributes. Options: auto, force, force-aligned, force-expand-multiline, aligned-multiple, preserve, preserve-aligned.
    wrapAttributesMinAttrsint2Minimum number of HTML tag attributes required for force wrap options. If force-expand-multiline is used, the first attribute is wrapped.
  5. Reference: Blade Tailwind CSS sorting options

    main

    Automatically sort Tailwind CSS classes within your templates.

    OptionTypeDefaultDescription
    sortTailwindcssClassesbooleanfalseWhether to sort Tailwind CSS classes automatically. Respects tailwind.config.js.
    tailwindcssConfigPathstring""Path to a custom Tailwind CSS config. Only used if sortTailwindcssClasses is true.
  6. Supported languages in prettier-plugin-blade

    main

    The plugin supports the Blade language. It is identified by the .blade.php file extension and uses the blade parser. This metadata allows Prettier to automatically associate Blade files with this plugin.

    export const languages: SupportLanguage[] = [
    	{
    		name: "Blade",
    		parsers: ["blade"],
    		since: "1.0.0",
    		extensions: [".blade.php"],
    		tmScope: "source.blade.php",
    		aceMode: "text",
    		linguistLanguageId: 33,
    		vscodeLanguageIds: ["blade"],
    	},
    ];
  7. Blade printer configuration

    main

    The blade-format printer is responsible for taking the Blade AST and converting it back into formatted source code. It uses the print function to perform the actual code generation.

    export const printers: { [k: string]: Printer } = {
    	"blade-format": {
    		print,
    	},
    };
  8. Blade parser configuration

    main

    The blade parser is responsible for converting Blade source code into an Abstract Syntax Tree (AST) with the format blade-format. It implements locStart and locEnd to provide node location information based on the start and end properties of the AST nodes.

    export const parsers: { [k: string]: Parser } = {
    	blade: {
    		parse,
    		astFormat: "blade-format",
    		locStart(node: any) {
    			return node.start;
    		},
    		locEnd(node: any) {
    			return node.end;
    		},
    	},
    };