prettier-plugin-asciidoc
Version:
Format AsciiDoc files with prettier 📖
187 lines (186 loc) • 7.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.languages = exports.options = exports.printers = exports.parsers = exports.PLUGIN_KEY = void 0;
const prettier_1 = require("prettier");
const parse_1 = require("./parser/parse");
exports.PLUGIN_KEY = "asciidoc";
function getAdocOptions(options) {
return options;
}
const parser = {
astFormat: exports.PLUGIN_KEY,
parse: (text, _, options) => (0, parse_1.parse)(text, getAdocOptions(options).adocVerbose),
locStart: (node) => node.location.start,
locEnd: (node) => node.location.end,
};
const printer = {
print: (path, options, print, args) => {
const { adocLiteralParagraphSpaces, adocAlwaysQuoteAttributeValues, adocIdFormat, adocOptionFormat, adocRoleFormat, } = options;
const node = path.getValue();
switch (node === null || node === void 0 ? void 0 : node.type) {
case undefined:
return "";
case "adoc":
const children = prettier_1.doc.utils.removeLines(path.map(print, "children"));
const parts = prettier_1.doc.utils.getDocParts(prettier_1.doc.utils.normalizeDoc(children));
return prettier_1.doc.utils.normalizeDoc([
parts.slice(1),
prettier_1.doc.builders.hardline,
]);
case "headline":
return [
prettier_1.doc.builders.hardline,
"=".repeat(node.level + 1),
" ",
printText(node.text),
];
case "text":
return printText(node);
case "paragraph":
return path
.map(print, "lines")
.map((line) => [
prettier_1.doc.builders.hardline,
node.isLiteral ? " ".repeat(adocLiteralParagraphSpaces) : "",
line,
]);
case "emptyLine":
return prettier_1.doc.builders.hardline;
case "attributeBlock":
return [prettier_1.doc.builders.hardline, path.call(print, "list")];
case "attributeList":
const ids = adocIdFormat === "shorthand"
? node.attributes.filter((attr) => attr.key === "id")
: [];
const roles = adocRoleFormat === "shorthand"
? node.attributes.filter((attr) => attr.key === "role")
: [];
const options = adocOptionFormat === "shorthand"
? node.attributes.filter((attr) => attr.key === "option")
: [];
const others = node.attributes
.filter((attr) => !ids.includes(attr))
.filter((attr) => !roles.includes(attr))
.filter((attr) => !options.includes(attr));
const printedShorthands = [
ids.map((id) => printAttributeShorthand(id, "#")),
roles.map((role) => printAttributeShorthand(role, ".")),
options.map((option) => printAttributeShorthand(option, "%")),
].flat();
const printedFormals = others.map((attr) => printAttributeFormal(attr, adocAlwaysQuoteAttributeValues));
return [
"[",
prettier_1.doc.builders.join(",", printedFormals),
printedShorthands,
"]",
];
case "attributeNode":
const prefix = node.key && `${node.key}=`;
return [prefix !== null && prefix !== void 0 ? prefix : "", path.call(print, "value")];
case "attributeValueNode":
const printedValues = node.values.join(" ");
const wrapChar = adocAlwaysQuoteAttributeValues || attributeQuotesNeeded(node.values)
? '"'
: "";
return `${wrapChar}${printedValues}${wrapChar}`;
default:
console.error(`Encountered unknown node of type "${node.type}".`);
return "";
}
},
};
function printAttributeFormal(node, alwaysQuote) {
const prefix = node.key && `${node.key}=`;
const printedValues = node.value.values.join(" ");
const wrapChar = alwaysQuote || attributeQuotesNeeded(node.value.values) ? '"' : "";
return [prefix !== null && prefix !== void 0 ? prefix : "", `${wrapChar}${printedValues}${wrapChar}`];
}
function printAttributeShorthand(node, shorthandSymbol) {
return node.value.values
.map((value) => `${shorthandSymbol}${value}`)
.join("");
}
function printText(node) {
return node.text.join(" ");
}
function attributeQuotesNeeded(values) {
return values.length > 1 || values.some((value) => value.match(/[,\[\]=]/));
}
exports.parsers = { [exports.PLUGIN_KEY]: parser };
exports.printers = { [exports.PLUGIN_KEY]: printer };
exports.options = {
adocLiteralParagraphSpaces: {
category: "AsciiDoc",
description: "How many spaces should be used to indent a literal paragraph?",
type: "int",
default: 2,
since: "0.0.0",
},
adocVerbose: {
category: "AsciiDoc",
description: "Log details about lexing, parsing and printing.",
type: "boolean",
default: false,
since: "0.0.0",
},
adocAlwaysQuoteAttributeValues: {
category: "AsciiDoc",
description: 'If set to true will always quote attributes in assignments. E.g. [key="value"] vs [key=value]',
type: "boolean",
default: false,
since: "0.0.0",
},
adocIdFormat: {
category: "AsciiDoc",
description: "Whether ids should be formatted in shorthand [#myid] or in formal [id=myid] syntax.",
type: "choice",
choices: [
{ since: "0.0.0", value: "shorthand", description: "[#myid]" },
{
since: "0.0.0",
value: "formal",
description: "[id=myid]",
},
],
default: "shorthand",
since: "0.0.0",
},
adocOptionFormat: {
category: "AsciiDoc",
description: `Whether options should be formatted in shorthand [%option1%option2] or in formal [option=option1 option2"] syntax.`,
type: "choice",
choices: [
{ since: "0.0.0", value: "shorthand", description: `[%option1%option2]` },
{
since: "0.0.0",
value: "formal",
description: `[option="option1 option2"]`,
},
],
default: "shorthand",
since: "0.0.0",
},
adocRoleFormat: {
category: "AsciiDoc",
description: `Whether roles should be formatted in shorthand [.role1.role2] or in formal [role="role1 role2"] syntax.`,
type: "choice",
choices: [
{ since: "0.0.0", value: "shorthand", description: `[%role1%role2]` },
{
since: "0.0.0",
value: "formal",
description: `[role="role1 role2"]`,
},
],
default: "shorthand",
since: "0.0.0",
},
};
exports.languages = [
{
name: "AsciiDoc",
parsers: [exports.PLUGIN_KEY],
extensions: [".adoc"],
vscodeLanguageIds: ["adoc", "asciidoc"],
},
];