Skip to content

Commit

Permalink
Merge pull request #840 from shufo/feature/add-option-indent-inner-html
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo authored Jul 25, 2023
2 parents 805e1fb + fff5cbb commit b4ce9a0
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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]
Expand Down Expand Up @@ -211,6 +212,7 @@ e.g.
"wrapAttributes": "auto",
"wrapLineLength": 120,
"wrapAttributesMinAttrs": 2,
"indentInnerHtml": true,
"endWithNewLine": true,
"endOfLine": "LF",
"useTabs": false,
Expand Down
26 changes: 26 additions & 0 deletions __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>

<head>
@section('header')
<title>
foo
</title>
@endsection
</head>

<body>
<button className="prettier-class" id="prettier-id" onClick={this.handleClick}>
Click Here
</button>
</body>

</html>
14 changes: 14 additions & 0 deletions __tests__/fixtures/argumentTest/indentInnerHtml/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
@section('header')
<title>
foo
</title>
@endsection
</head>
<body>
<button className="prettier-class" id="prettier-id" onClick={this.handleClick}>
Click Here
</button>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"indentInnerHtml": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>

<head>
@section('header')
<title>
foo
</title>
@endsection
</head>

<body>
<button className="prettier-class" id="prettier-id" onClick={this.handleClick}>
Click Here
</button>
</body>

</html>
14 changes: 14 additions & 0 deletions __tests__/fixtures/runtimeConfig/indentInnerHtml/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
@section('header')
<title>
foo
</title>
@endsection
</head>
<body>
<button className="prettier-class" id="prettier-id" onClick={this.handleClick}>
Click Here
</button>
</body>
</html>
37 changes: 37 additions & 0 deletions __tests__/fixtures/snapshots/indent_inner_html.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
------------------------------------options----------------------------------------
{
"indentInnerHtml": true
}
------------------------------------content----------------------------------------
<html>
<head>
@section('header')
<title>
foo
</title>
@endsection
</head>
<body>
<button className="prettier-class" id="prettier-id" onClick={this.handleClick}>
Click Here
</button>
</body>
</html>
------------------------------------expected----------------------------------------
<html>

<head>
@section('header')
<title>
foo
</title>
@endsection
</head>

<body>
<button className="prettier-class" id="prettier-id" onClick={this.handleClick}>
Click Here
</button>
</body>

</html>
6 changes: 6 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <head> and <body> sections in html.',
default: false,
})
.option('sort-tailwindcss-classes', {
alias: 'sort-classes',
type: 'boolean',
Expand Down
3 changes: 3 additions & 0 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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'],
};
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type FormatterOption = {
wrapLineLength?: number;
wrapAttributes?: WrapAttributes;
wrapAttributesMinAttrs?: number;
indentInnerHtml?: boolean;
endWithNewline?: boolean;
endOfLine?: EndOfLine;
useTabs?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/runtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface RuntimeConfig {
wrapLineLength?: number;
wrapAttributes?: WrapAttributes;
wrapAttributesMinAttrs?: number;
indentInnerHtml?: boolean;
endWithNewline?: boolean;
endOfLine?: EndOfLine;
useTabs?: boolean;
Expand Down Expand Up @@ -78,6 +79,7 @@ export async function readRuntimeConfig(filePath: string | null): Promise<Runtim
nullable: true,
},
wrapAttributesMinAttrs: { type: 'integer', nullable: true, default: 2 },
indentInnerHtml: { type: 'boolean', nullable: true },
endWithNewline: { type: 'boolean', nullable: true },
endOfLine: { type: 'string', enum: ['LF', 'CRLF'], nullable: true },
useTabs: { type: 'boolean', nullable: true },
Expand Down

0 comments on commit b4ce9a0

Please sign in to comment.