UNPKG

prisma-markdown

Version:

Prisma Markdown documents generator including ERD diagrams and comment descriptions

73 lines 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DescriptionWriter = void 0; const PrismaUtil_1 = require("../utils/PrismaUtil"); var DescriptionWriter; (function (DescriptionWriter) { DescriptionWriter.table = (model) => { var _a; const description = writeDescription(model); return [ `### \`${(_a = model.dbName) !== null && _a !== void 0 ? _a : model.name}\``, ...(description.length ? [description] : []), "", "**Properties**", ...model.fields.filter((f) => f.kind !== "object").map(writeField), ].join("\n"); }; const writeField = (field) => { var _a; const description = writeDescription(field); const lines = description.split("\n"); const head = ` - \`${(_a = field.dbName) !== null && _a !== void 0 ? _a : field.name}\``; if (lines.length === 0) return head; else if (lines.length === 1) return `${head}: ${lines[0]}`; return [head, ...lines.map((line) => ` > ${line}`)].join("\n"); }; const writeDescription = (target) => { var _a; const content = ((_a = target.documentation) !== null && _a !== void 0 ? _a : "") .split("\r\n") .join("\n") .split("\n"); let first = 0; let last = content.length - 1; const empty = (str) => str.trim() === "" || str.trim()[0] === "@"; while (first < content.length && empty(content[first])) ++first; while (last >= 0 && empty(content[last])) --last; const summary = PrismaUtil_1.PrismaUtil.tagValues("summary")(target); const body = content.slice(first, last + 1).map(replaceLinks); return summary.length ? body.length ? [summary[0], "", ...body].join("\n") : summary[0] : body.join("\n"); }; const replaceLinks = (content) => { const rejoined = []; let i = 0; while (true) { const first = content.indexOf("{@link ", i); if (first === -1) break; const last = content.indexOf("}", first + 7); if (last === -1) break; const part = content.slice(first + 7, last).trim(); const space = part.indexOf(" "); rejoined.push(content.slice(i, first)); if (space === -1) rejoined.push(`[${part}](#${part.split(".")[0]})`); else rejoined.push(`[${part.slice(space + 1)}](#${part.slice(0, space).split(".")[0]})`); i = last + 1; } rejoined.push(content.slice(i)); return rejoined.join(""); }; })(DescriptionWriter || (exports.DescriptionWriter = DescriptionWriter = {})); //# sourceMappingURL=DescriptionWriter.js.map