Comparing version 1.5.0 to 2.0.0
@@ -80,2 +80,8 @@ "use strict"; | ||
} | ||
/** @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] } })), {}); | ||
} | ||
/** | ||
@@ -115,17 +121,8 @@ * Creates a markdown table based on a headers object and an array of rows. | ||
headers, }) { | ||
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 { 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 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))); | ||
@@ -132,0 +129,0 @@ return (__1.default(__1.Fragment, null, |
/** @internal */ | ||
export declare const MdFragmentType: "mdFragment"; | ||
/** @internal */ | ||
interface FunctionElement<Props = unknown> { | ||
export interface MdFunctionElement<Props = unknown> { | ||
type: Component<Props>; | ||
@@ -10,3 +10,3 @@ props: Props; | ||
/** @internal */ | ||
interface MdFragmentElement { | ||
export interface MdFragmentElement { | ||
type: typeof MdFragmentType; | ||
@@ -17,3 +17,3 @@ props: PropsWithChildren; | ||
/** Internal representation of markdown before rendering. */ | ||
export declare type MarkdownElement<Props = unknown> = FunctionElement<Props> | MdFragmentElement; | ||
export declare type MarkdownElement<Props = unknown> = MdFunctionElement<Props> | MdFragmentElement; | ||
/** Primitive text types that get converted into text. */ | ||
@@ -33,2 +33,1 @@ export declare type MarkdownText = string | number; | ||
export declare type Component<ComponentProps extends unknown = unknown> = (props: ComponentProps) => MarkdownElement | null; | ||
export {}; |
import { MarkdownElement } from "./model"; | ||
/** Renders a markdown element into text. */ | ||
export declare function render(element: MarkdownElement): string; | ||
export declare function render(element: MarkdownElement): Promise<string>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.render = void 0; | ||
const model_1 = require("./model"); | ||
const renderFunctions_1 = require("./renderFunctions"); | ||
/** @internal */ | ||
function renderNode(element) { | ||
if (element === null || element === undefined || element === false) { | ||
return ""; | ||
const results = renderFunctions_1.synchronousRenderFunctions | ||
.map((renderFunction) => renderFunction(element, renderNode)) | ||
.filter((result) => result !== null); | ||
if (results.length === 0) { | ||
throw new Error("Invalid element"); | ||
} | ||
if (typeof element === "string") { | ||
return element; | ||
} | ||
if (typeof element === "number") { | ||
return element.toString(); | ||
} | ||
if (Array.isArray(element)) { | ||
return element.map((el) => renderNode(el)).join(""); | ||
} | ||
if (typeof element.type === "function") { | ||
return renderNode(element.type(element.props)); | ||
} | ||
if (element.type === model_1.MdFragmentType) { | ||
return renderNode(element.props.children); | ||
} | ||
throw new Error("Invalid element"); | ||
return results[0]; | ||
} | ||
/** Renders a markdown element into text. */ | ||
function render(element) { | ||
async function render(element) { | ||
return renderNode(element); | ||
@@ -30,0 +18,0 @@ } |
{ | ||
"name": "jsx-md", | ||
"version": "1.5.0", | ||
"version": "2.0.0", | ||
"description": "Generate markdown files with a React-like syntax.", | ||
@@ -45,2 +45,3 @@ "main": "lib/index.js", | ||
"jest": "^26.4.2", | ||
"jest-extended": "^0.11.5", | ||
"lint-staged": "^10.2.13", | ||
@@ -47,0 +48,0 @@ "pkg-ok": "^2.3.1", |
@@ -33,6 +33,7 @@ # jsx-md | ||
// Currently, the rendering function is not always necessary. | ||
// This might change in the future, though, so do not rely on any | ||
// intermediate results from creating elements. | ||
writeFileSync("README.md", render(<Readme />)); | ||
async function writeReadme() { | ||
writeFileSync("README.md", await render(<Readme />)); | ||
} | ||
void writeReadme(); | ||
``` | ||
@@ -39,0 +40,0 @@ |
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
77319
116
1635
43
25