diff --git a/README.md b/README.md index 9b21cc6b..2268dde7 100755 --- a/README.md +++ b/README.md @@ -182,6 +182,7 @@ $ blade-formatter -c -d resources/**/*.blade.php --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 and 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] @@ -211,6 +212,7 @@ e.g. "wrapAttributes": "auto", "wrapLineLength": 120, "wrapAttributesMinAttrs": 2, + "indentInnerHtml": true, "endWithNewLine": true, "endOfLine": "LF", "useTabs": false, diff --git a/__tests__/cli.test.ts b/__tests__/cli.test.ts index 5bd3966f..855b3946 100644 --- a/__tests__/cli.test.ts +++ b/__tests__/cli.test.ts @@ -779,4 +779,30 @@ describe('The blade formatter CLI', () => { expect(cmdResult).toEqual(formatted.toString('utf-8')); }); + + test.concurrent('runtime config test (indent inner html)', async () => { + const cmdResult = await cmd.execute(binPath, [ + path.resolve('__tests__', 'fixtures', 'runtimeConfig', 'indentInnerHtml', 'index.blade.php'), + ]); + + const formatted = fs.readFileSync( + path.resolve('__tests__', 'fixtures', 'runtimeConfig', 'indentInnerHtml', 'formatted.index.blade.php'), + ); + + expect(cmdResult).toEqual(formatted.toString('utf-8')); + }); + + test.concurrent('cli argument test (indent inner html)', async () => { + const cmdResult = await cmd.execute(binPath, [ + '--indent-inner-html', + 'true', + path.resolve('__tests__', 'fixtures', 'argumentTest', 'indentInnerHtml', 'index.blade.php'), + ]); + + const formatted = fs.readFileSync( + path.resolve('__tests__', 'fixtures', 'argumentTest', 'indentInnerHtml', 'formatted.index.blade.php'), + ); + + expect(cmdResult).toEqual(formatted.toString('utf-8')); + }); }); diff --git a/__tests__/fixtures/argumentTest/indentInnerHtml/formatted.index.blade.php b/__tests__/fixtures/argumentTest/indentInnerHtml/formatted.index.blade.php new file mode 100644 index 00000000..c561cfbc --- /dev/null +++ b/__tests__/fixtures/argumentTest/indentInnerHtml/formatted.index.blade.php @@ -0,0 +1,17 @@ + + + + @section('header') + + foo + + @endsection + + + + + + + diff --git a/__tests__/fixtures/argumentTest/indentInnerHtml/index.blade.php b/__tests__/fixtures/argumentTest/indentInnerHtml/index.blade.php new file mode 100644 index 00000000..5f3d5d43 --- /dev/null +++ b/__tests__/fixtures/argumentTest/indentInnerHtml/index.blade.php @@ -0,0 +1,14 @@ + + +@section('header') + +foo + +@endsection + + + + + diff --git a/__tests__/fixtures/runtimeConfig/indentInnerHtml/.bladeformatterrc.json b/__tests__/fixtures/runtimeConfig/indentInnerHtml/.bladeformatterrc.json new file mode 100644 index 00000000..626db659 --- /dev/null +++ b/__tests__/fixtures/runtimeConfig/indentInnerHtml/.bladeformatterrc.json @@ -0,0 +1,3 @@ +{ + "indentInnerHtml": true +} diff --git a/__tests__/fixtures/runtimeConfig/indentInnerHtml/formatted.index.blade.php b/__tests__/fixtures/runtimeConfig/indentInnerHtml/formatted.index.blade.php new file mode 100644 index 00000000..c561cfbc --- /dev/null +++ b/__tests__/fixtures/runtimeConfig/indentInnerHtml/formatted.index.blade.php @@ -0,0 +1,17 @@ + + + + @section('header') + + foo + + @endsection + + + + + + + diff --git a/__tests__/fixtures/runtimeConfig/indentInnerHtml/index.blade.php b/__tests__/fixtures/runtimeConfig/indentInnerHtml/index.blade.php new file mode 100644 index 00000000..5f3d5d43 --- /dev/null +++ b/__tests__/fixtures/runtimeConfig/indentInnerHtml/index.blade.php @@ -0,0 +1,14 @@ + + +@section('header') + +foo + +@endsection + + + + + diff --git a/__tests__/fixtures/snapshots/indent_inner_html.snapshot b/__tests__/fixtures/snapshots/indent_inner_html.snapshot new file mode 100644 index 00000000..2b169f16 --- /dev/null +++ b/__tests__/fixtures/snapshots/indent_inner_html.snapshot @@ -0,0 +1,37 @@ +------------------------------------options---------------------------------------- +{ + "indentInnerHtml": true +} +------------------------------------content---------------------------------------- + + +@section('header') + +foo + +@endsection + + + + + +------------------------------------expected---------------------------------------- + + + + @section('header') + + foo + + @endsection + + + + + + + diff --git a/src/cli.ts b/src/cli.ts index 0340d54d..0e2eb4c6 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -74,6 +74,12 @@ export default async function cli() { description: `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', }) + .option('indent-inner-html', { + alias: 'I', + type: 'boolean', + description: 'Indent and sections in html.', + default: false, + }) .option('sort-tailwindcss-classes', { alias: 'sort-classes', type: 'boolean', diff --git a/src/formatter.ts b/src/formatter.ts index 7710d347..2126073b 100644 --- a/src/formatter.ts +++ b/src/formatter.ts @@ -245,6 +245,7 @@ export default class Formatter { wrap_line_length: util.optional(this.options).wrapLineLength || 120, wrap_attributes: util.optional(this.options).wrapAttributes || 'auto', wrap_attributes_min_attrs: util.optional(this.options).wrapAttributesMinAttrs, + indent_inner_html: util.optional(this.options).indentInnerHtml || false, end_with_newline: util.optional(this.options).endWithNewline || true, max_preserve_newlines: util.optional(this.options).noMultipleEmptyLines ? 1 : undefined, css: { @@ -1934,6 +1935,7 @@ export default class Formatter { wrap_line_length: util.optional(this.options).wrapLineLength || 120, wrap_attributes: util.optional(this.options).wrapAttributes || 'auto', wrap_attributes_min_attrs: util.optional(this.options).wrapAttributesMinAttrs, + indent_inner_html: util.optional(this.options).indentInnerHtml || false, indent_with_tabs: useTabs, end_with_newline: false, templating: ['php'], @@ -2042,6 +2044,7 @@ export default class Formatter { wrap_line_length: util.optional(this.options).wrapLineLength || 120, wrap_attributes: util.optional(this.options).wrapAttributes || 'auto', wrap_attributes_min_attrs: util.optional(this.options).wrapAttributesMinAttrs, + indent_inner_html: util.optional(this.options).indentInnerHtml || false, end_with_newline: false, templating: ['php'], }; diff --git a/src/main.ts b/src/main.ts index 51b25b15..b11d3065 100644 --- a/src/main.ts +++ b/src/main.ts @@ -35,6 +35,7 @@ export type FormatterOption = { wrapLineLength?: number; wrapAttributes?: WrapAttributes; wrapAttributesMinAttrs?: number; + indentInnerHtml?: boolean; endWithNewline?: boolean; endOfLine?: EndOfLine; useTabs?: boolean; diff --git a/src/runtimeConfig.ts b/src/runtimeConfig.ts index 1592b29b..34cd747d 100644 --- a/src/runtimeConfig.ts +++ b/src/runtimeConfig.ts @@ -23,6 +23,7 @@ export interface RuntimeConfig { wrapLineLength?: number; wrapAttributes?: WrapAttributes; wrapAttributesMinAttrs?: number; + indentInnerHtml?: boolean; endWithNewline?: boolean; endOfLine?: EndOfLine; useTabs?: boolean; @@ -78,6 +79,7 @@ export async function readRuntimeConfig(filePath: string | null): Promise