Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slimy-plums-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-astro": patch
---

Format custom elements & components as block elements with htmlWhitespaceSensitivity: ignore
13 changes: 11 additions & 2 deletions src/printer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ export function isInlineElement(path: AstPath, opts: ParserOptions, node: anyNod
}

export function isBlockElement(node: anyNode, opts: ParserOptions): boolean {
if (!node) {
return false;
}

// All tags (element, custom-element, component, fragment) are considered
// block elements when htmlWhitespaceSensitivity is set to "ignore".
if (opts.htmlWhitespaceSensitivity === 'ignore') {
return true;
}

return (
node &&
node.type === 'element' &&
opts.htmlWhitespaceSensitivity !== 'strict' &&
(opts.htmlWhitespaceSensitivity === 'ignore' || blockElements.includes(node.name as TagName))
blockElements.includes(node.name as TagName)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
import Link from "@/components/link.astro";
---

<ul>
<li><a href="https://github.com/withastro/prettier-plugin-astro">Prettier Plugin for Astro (using HTML element)</a></li>
<li><Link href="https://github.com/withastro/prettier-plugin-astro">Prettier Plugin for Astro (using component)</Link></li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bracketSameLine": true,
"htmlWhitespaceSensitivity": "ignore"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import Link from "@/components/link.astro";
---

<ul>
<li>
<a href="https://github.com/withastro/prettier-plugin-astro">
Prettier Plugin for Astro (using HTML element)
</a>
</li>
<li>
<Link href="https://github.com/withastro/prettier-plugin-astro">
Prettier Plugin for Astro (using component)
</Link>
</li>
</ul>
5 changes: 5 additions & 0 deletions test/tests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ test(
files,
'options/option-html-whitespace-sensitivity-ignore',
);
test(
'Can format components with prettier "htmlWhitespaceSensitivity: ignore" option',
files,
'options/option-html-whitespace-sensitivity-ignore-component',
);

// https://prettier.io/docs/en/options.html#single-attribute-per-line
test(
Expand Down