Comparing version 1.4.0 to 1.5.0
@@ -22,5 +22,5 @@ /** | ||
export { ReferenceLink } from "./ReferenceLink"; | ||
export { Table } from "./Table"; | ||
export { Table, TableAlignment } from "./Table"; | ||
export { Task } from "./Task"; | ||
export { Text } from "./Text"; | ||
export { UnorderedList } from "./UnorderedList"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UnorderedList = exports.Text = exports.Task = exports.Table = exports.ReferenceLink = exports.ReferenceImage = exports.Reference = exports.Strikethrough = exports.OrderedList = exports.Link = exports.LineBreak = exports.Italics = exports.InlineCode = exports.Image = exports.HorizontalRule = exports.Heading = exports.Fragment = exports.Emphasis = exports.CodeBlock = exports.Bold = exports.BlockQuote = void 0; | ||
exports.UnorderedList = exports.Text = exports.Task = exports.TableAlignment = exports.Table = exports.ReferenceLink = exports.ReferenceImage = exports.Reference = exports.Strikethrough = exports.OrderedList = exports.Link = exports.LineBreak = exports.Italics = exports.InlineCode = exports.Image = exports.HorizontalRule = exports.Heading = exports.Fragment = exports.Emphasis = exports.CodeBlock = exports.Bold = exports.BlockQuote = void 0; | ||
/** | ||
@@ -44,2 +44,3 @@ * @packageDocumentation | ||
Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return Table_1.Table; } }); | ||
Object.defineProperty(exports, "TableAlignment", { enumerable: true, get: function () { return Table_1.TableAlignment; } }); | ||
var Task_1 = require("./Task"); | ||
@@ -46,0 +47,0 @@ Object.defineProperty(exports, "Task", { enumerable: true, get: function () { return Task_1.Task; } }); |
import { Component, MarkdownChildren } from ".."; | ||
export declare enum TableAlignment { | ||
LEFT = "left", | ||
CENTER = "center", | ||
RIGHT = "right" | ||
} | ||
/** @internal */ | ||
interface TableHeader { | ||
title: MarkdownChildren; | ||
alignment?: TableAlignment; | ||
} | ||
/** @internal */ | ||
interface Props<Headers extends string> { | ||
body: Record<Headers, MarkdownChildren>[]; | ||
headers: Record<Headers, MarkdownChildren>; | ||
headers: Record<Headers, MarkdownChildren | TableHeader>; | ||
} | ||
@@ -16,7 +26,8 @@ /** | ||
* foo: "Foo header", | ||
* bar: "Bar header", | ||
* bar: { title: "Bar header", alignment: TableAlignment.CENTER }, | ||
* baz: { title: "Baz header" }, | ||
* }; | ||
* const body = [ | ||
* { foo: "Foo body 1", bar: "Bar body 1" }, | ||
* { foo: "Foo body 2", bar: "Bar body 2" }, | ||
* { foo: "Foo body 1", bar: "Bar body 1", baz: "Baz body 1" }, | ||
* { foo: "Foo body 2", bar: "Bar body 2", baz: "Baz body 2" }, | ||
* ]; | ||
@@ -26,10 +37,10 @@ * render(<Table headers={headers} body={body} />) | ||
* ` | ||
* | Foo header | Bar header | | ||
* | ---------- | ---------- | | ||
* | Foo body 1 | Bar body 1 | | ||
* | Foo body 2 | Bar body 2 | | ||
* | Foo header | Bar header | Baz header | | ||
* | ---------- | :--------: | ---------- | | ||
* | Foo body 1 | Bar body 1 | Baz body 1 | | ||
* | Foo body 2 | Bar body 2 | Baz body 2 | | ||
* ` | ||
*/ | ||
export declare function Table<Headers extends string>({ | ||
/** An array of data objects with the same keys as the headers to be displayed as rows */ | ||
/** An array of data objects with the same keys as the headers to be displayed as rows. */ | ||
body, | ||
@@ -36,0 +47,0 @@ /** |
@@ -33,7 +33,18 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Table = void 0; | ||
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) { | ||
@@ -46,5 +57,16 @@ return (__1.default(__1.Fragment, null, Object.values(entries).map((entry, index) => (__1.default(__1.Fragment, { key: index }, | ||
/** @internal */ | ||
function renderSeparator(columnWidths) { | ||
return columnWidths | ||
.map((width) => ` ${"-".repeat(width)} |`) | ||
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(""); | ||
@@ -69,7 +91,8 @@ } | ||
* foo: "Foo header", | ||
* bar: "Bar header", | ||
* bar: { title: "Bar header", alignment: TableAlignment.CENTER }, | ||
* baz: { title: "Baz header" }, | ||
* }; | ||
* const body = [ | ||
* { foo: "Foo body 1", bar: "Bar body 1" }, | ||
* { foo: "Foo body 2", bar: "Bar body 2" }, | ||
* { foo: "Foo body 1", bar: "Bar body 1", baz: "Baz body 1" }, | ||
* { foo: "Foo body 2", bar: "Bar body 2", baz: "Baz body 2" }, | ||
* ]; | ||
@@ -79,10 +102,10 @@ * render(<Table headers={headers} body={body} />) | ||
* ` | ||
* | Foo header | Bar header | | ||
* | ---------- | ---------- | | ||
* | Foo body 1 | Bar body 1 | | ||
* | Foo body 2 | Bar body 2 | | ||
* | 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 */ | ||
/** An array of data objects with the same keys as the headers to be displayed as rows. */ | ||
body, | ||
@@ -95,14 +118,23 @@ /** | ||
headers, }) { | ||
const columnWidths = Object.values(headers).map((header) => { | ||
if (typeof header !== "string") { | ||
return 5; | ||
const columns = Object.values(headers).map((header) => { | ||
let width = typeof header !== "string" ? 5 : header.length; | ||
let alignment; | ||
if (isTableHeader(header)) { | ||
({ alignment } = header); | ||
if (typeof header.title === "string") { | ||
width = header.title.length; | ||
} | ||
} | ||
return header.length; | ||
return { width, alignment }; | ||
}); | ||
const normalizedColumns = Object.keys(headers).reduce((normalizedHeaderAccumulator, key) => { | ||
const header = headers[key]; | ||
return Object.assign(Object.assign({}, normalizedHeaderAccumulator), { [key]: isTableHeader(header) ? header.title : header }); | ||
}, {}); | ||
const sortedBody = body.map(sortKeysInOrderOf(Object.keys(headers))); | ||
return (__1.default(__1.Fragment, null, | ||
"\n|", | ||
renderRow(headers), | ||
renderRow(normalizedColumns), | ||
"\n|", | ||
renderSeparator(columnWidths), | ||
renderSeparator(columns), | ||
"\n|", | ||
@@ -109,0 +141,0 @@ intersperse_1.intersperse(sortedBody.map((row) => renderRow(row)), "\n|"), |
{ | ||
"name": "jsx-md", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Generate markdown files with a React-like syntax.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68815
1514