UNPKG

jsx-md

Version:

Generate markdown files with a React-like syntax.

138 lines 5.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Table = exports.TableAlignment = void 0; /* @jsx MD */ const __1 = __importStar(require("..")); const intersperse_1 = require("../util/intersperse"); var TableAlignment; (function (TableAlignment) { TableAlignment["LEFT"] = "left"; TableAlignment["CENTER"] = "center"; TableAlignment["RIGHT"] = "right"; })(TableAlignment = exports.TableAlignment || (exports.TableAlignment = {})); /** @internal */ // eslint-disable-next-line @typescript-eslint/no-explicit-any function isTableHeader(value) { return value && value.title; } /** @internal */ function renderRow(entries) { return (__1.default(__1.Fragment, null, Object.values(entries).map((entry, index) => (__1.default(__1.Fragment, { key: index }, " ", entry, " |"))))); } /** @internal */ function renderSeparator(columns) { return columns .map((column) => { switch (column.alignment) { case TableAlignment.LEFT: return ` :${"-".repeat(Math.max(column.width - 1, 2))} |`; case TableAlignment.CENTER: return ` :${"-".repeat(Math.max(column.width - 2, 1))}: |`; case TableAlignment.RIGHT: return ` ${"-".repeat(Math.max(column.width - 1, 2))}: |`; default: return ` ${"-".repeat(Math.max(column.width, 3))} |`; } }) .join(""); } /** @internal */ function sortKeysInOrderOf(keys) { return (obj) => keys.reduce((bodyAccumulator, header) => { /* istanbul ignore next */ /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ const _a = bodyAccumulator, _b = header, _omitted = _a[_b], bodyWithoutHeader = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); return Object.assign(Object.assign({}, bodyWithoutHeader), { [header]: obj[header] }); }, obj); } /** @internal */ function normalizeHeaders(headers) { return Object.keys(headers).reduce((normalizedHeaderAccumulator, key) => (Object.assign(Object.assign({}, normalizedHeaderAccumulator), { [key]: isTableHeader(headers[key]) ? headers[key] : { title: headers[key] } })), {}); } /** * Creates a markdown table based on a headers object and an array of rows. * Columns are ordered in the order in which the header keys were created. * * @paramType A union of possible keys for headers and body * @example * ```js * const headers = { * foo: "Foo header", * bar: { title: "Bar header", alignment: TableAlignment.CENTER }, * baz: { title: "Baz header" }, * }; * const body = [ * { foo: "Foo body 1", bar: "Bar body 1", baz: "Baz body 1" }, * { foo: "Foo body 2", bar: "Bar body 2", baz: "Baz body 2" }, * ]; * render(<Table headers={headers} body={body} />) * === * ` * | Foo header | Bar header | Baz header | * | ---------- | :--------: | ---------- | * | Foo body 1 | Bar body 1 | Baz body 1 | * | Foo body 2 | Bar body 2 | Baz body 2 | * ` */ function Table({ /** An array of data objects with the same keys as the headers to be displayed as rows. */ body, /** * An object giving the headers for each column. The order in which the keys * of this object were created determines the order in which the columns * are rendered. */ headers, }) { const normalizedHeaders = normalizeHeaders(headers); const columns = Object.values(normalizedHeaders).map((header) => ({ width: typeof header.title === "string" ? header.title.length : 5, alignment: header.alignment, })); const normalizedColumns = Object.fromEntries(Object.entries(normalizedHeaders).map(([key, value]) => [key, value.title])); const sortedBody = body.map(sortKeysInOrderOf(Object.keys(headers))); return (__1.default(__1.Fragment, null, "\n|", renderRow(normalizedColumns), "\n|", renderSeparator(columns), "\n|", intersperse_1.intersperse(sortedBody.map((row) => renderRow(row)), "\n|"), "\n")); } exports.Table = Table; //# sourceMappingURL=Table.js.map