@rawwee/prettier-plugin-twig-melody
Version:
Prettier Plugin for Twig/Melody (Enhanced Fork)
43 lines (37 loc) • 1.28 kB
JavaScript
const prettier = require("prettier");
const { concat } = prettier.doc.builders;
const {
createTextGroups,
stripTwigCommentChars,
normalizeTwigComment,
countNewlines
} = require("../util");
const p = (node, path, print, options) => {
const originalText = node.value.value || "";
let commentText = stripTwigCommentChars(originalText);
const trimLeft = originalText.length >= 3 ? originalText[2] === "-" : false;
const trimRight =
originalText.length >= 3 ? originalText.slice(-3, -2) === "-" : false;
// Restore Vue expression placeholders in comment content
const replacements = options.vueAlpineReplacements || new Map();
for (const [placeholder, originalContent] of replacements) {
if (
placeholder.startsWith("vue-expression-") &&
commentText.includes(placeholder)
) {
commentText = commentText.replace(placeholder, originalContent);
}
}
const numNewlines = countNewlines(commentText);
if (numNewlines === 0) {
return normalizeTwigComment(commentText, trimLeft, trimRight);
}
return concat([
trimLeft ? "{#-" : "{#",
commentText,
trimRight ? "-#}" : "#}"
]);
};
module.exports = {
printTwigComment: p
};