diff --git a/.changeset/six-lobsters-jog.md b/.changeset/six-lobsters-jog.md new file mode 100644 index 0000000..88e52db --- /dev/null +++ b/.changeset/six-lobsters-jog.md @@ -0,0 +1,5 @@ +--- +"prettier-plugin-astro": minor +--- + +Do not delete line breaks and indentation of lines in class attribute diff --git a/src/printer/utils.ts b/src/printer/utils.ts index 2881148..2e42f85 100644 --- a/src/printer/utils.ts +++ b/src/printer/utils.ts @@ -198,7 +198,12 @@ export function trimTextNodeRight(node: TextNode): void { } export function printClassNames(value: string) { - return value.trim().split(/\s+/).join(' '); + const lines = value.trim().split(/[\r\n]+/); + const formattedLines = lines.map((line) => { + const spaces = line.match(/^\s+/); + return (spaces ? spaces[0] : '') + line.trim().split(/\s+/).join(' '); + }); + return formattedLines.join('\n'); } /** dedent string & return tabSize (the last part is what we need) */ diff --git a/test/fixtures/basic/html-class-attribute-with-line-breaks/input.astro b/test/fixtures/basic/html-class-attribute-with-line-breaks/input.astro new file mode 100644 index 0000000..803d363 --- /dev/null +++ b/test/fixtures/basic/html-class-attribute-with-line-breaks/input.astro @@ -0,0 +1,3 @@ +
diff --git a/test/fixtures/basic/html-class-attribute-with-line-breaks/output.astro b/test/fixtures/basic/html-class-attribute-with-line-breaks/output.astro new file mode 100644 index 0000000..20daa1d --- /dev/null +++ b/test/fixtures/basic/html-class-attribute-with-line-breaks/output.astro @@ -0,0 +1,3 @@ + diff --git a/test/tests/basic.test.ts b/test/tests/basic.test.ts index 5bc5c30..6661adc 100644 --- a/test/tests/basic.test.ts +++ b/test/tests/basic.test.ts @@ -17,6 +17,12 @@ test('Can format HTML custom elements', files, 'basic/html-custom-elements'); test('Can properly format the class attribute', files, 'basic/html-class-attribute'); +test( + 'Can properly format the class attribute with line breaks', + files, + 'basic/html-class-attribute-with-line-breaks' +); + test('Can format long self-closing tags with multiple attributes', files, 'basic/self-closing'); test('Can properly format inline tags and respect whitespace', files, 'basic/inline-whitespace');