prettier-plugin-astro-attributes
Version:
A Prettier plugin that automatically sorts HTML attributes alphabetically in .astro files for consistent code formatting.
28 lines • 1.11 kB
JavaScript
import { parsers as astroParsers, languages, printers, } from 'prettier-plugin-astro';
const sortAttributes = (attributes) => [...attributes]?.sort((a, b) => a?.name?.toLowerCase().localeCompare(b?.name?.toLowerCase()));
const traverseAndSort = (node) => {
if (node?.type === 'element' && node?.attributes) {
node.attributes = sortAttributes(node.attributes);
}
if ('children' in node && Array.isArray(node?.children)) {
node.children.forEach(child => traverseAndSort(child));
}
};
if (!astroParsers.astro) {
console.warn('prettier-plugin-astro-attributes: The official prettier-plugin-astro parser is not detected. This plugin requires prettier-plugin-astro to function correctly.');
}
const baseParser = astroParsers.astro;
const extendedParser = {
...baseParser,
parse(text, options) {
const ast = baseParser.parse(text, options);
traverseAndSort(ast);
return ast;
},
};
export const parsers = {
astro: extendedParser,
};
const plugin = { languages, parsers, printers };
export default plugin;
//# sourceMappingURL=index.js.map