/** * Collection of tiny utilities that convert input values into strings suitable for Markdown formatting. * * @packageDocumentation */ /** * DataCell represents a single cell in the data rows of a Markdown table. * * @public */ export declare type DataCell = string; /** * DataRow represents a row of data cells in a Markdown table. * * @public */ export declare type DataRow = DataCell[]; /** * DelimiterCell represents a single cell in the delimiter row of a Markdown table. * * @public */ export declare interface DelimiterCell { /** * The alignment of the cell content. */ alignment?: 'left' | 'center' | 'right'; } /** * DelimiterRow represents a row of delimiter cells in a Markdown table. * * @public */ export declare type DelimiterRow = DelimiterCell[]; /** * Markdown Table Utilities * * Provides functions to create and validate Markdown tables. */ /** * HeaderCell represents a single cell in the header row of a Markdown table. * * @public */ export declare type HeaderCell = string; /** * HeaderRow represents a row of header cells in a Markdown table. * * @public */ export declare type HeaderRow = HeaderCell[]; /** * MarkdownTable represents a complete Markdown table structure. * * @public */ export declare interface MarkdownTable { /** * The header row of the table. */ header: HeaderRow; /** * The delimiter row of the table, defining cell alignments. */ delimiter: DelimiterRow; /** * The data rows of the table, containing the actual content. */ data: DataRow[]; } /** * Parses a TSV (Tab-Separated Values) string into a MarkdownTable structure. * * @param tsv - The TSV string to parse. * @returns A MarkdownTable object containing the header, delimiter, and data rows. * * @remarks * This function splits the input TSV string into lines, processes the header, * delimiter, and data rows, and returns a structured MarkdownTable object. * It does not trim the input string, allowing for leading or trailing whitespace * in the TSV data. * Empty lines are filtered out, and the header is expected to be the first line * of the TSV. The delimiter row is created with empty objects for each header cell. * The data rows are parsed from the remaining lines. * * @example * ```typescript * const tsv = 'Name\tAge\nAlice\t30\nBob\t25'; * const table = parseTsv(tsv); * console.log(table.header); // ['Name', 'Age'] * console.log(table.data); // [['Alice', '30'], ['Bob', '25']] * console.log(table.delimiter); // [{}, {}] * ``` * * @beta */ export declare function parseTsv(tsv: string): MarkdownTable; /** * Converts a string to a Markdown header. * * The output format is {@link https://spec.commonmark.org/0.31.2/#atx-headings | ATX headings}. * * @param value - The text to convert into a header. * @param level - The level of the header (1-6). If less than 1, it is treated as 1. If greater than 6, it is treated as 6. * @returns A string formatted as a Markdown header. * * @remarks * This function generates a Markdown header by repeating the `#` character according to the specified level, followed by a space and the provided text. * The level must be between 1 and 6, where 1 is the highest level (largest header) and 6 is the lowest level (smallest header). * If the level is less than 1, it is treated as 1. If the level is greater than 6, it is treated as 6. * * @example * ```typescript * import { toHeader } from '@msn088/x2md'; * const header = toHeader('Header Level 1'); // "# Header Level 1\n" * const header0 = toHeader('Header Level 0', 0); // "# Header Level 0\n" * const header1 = toHeader('Header Level 1', 1); // "# Header Level 1\n" * const header2 = toHeader('Header Level 2', 2); // "## Header Level 2\n" * const header6 = toHeader('Header Level 6', 6); // "###### Header Level 6\n" * const header7 = toHeader('Header Level 7', 7); // "###### Header Level 7\n" * ``` * * @public */ export declare function toHeader(value: string, level?: number): string; /** * Converts an array of strings into a Markdown list. * * @param items - An array of strings, where each string represents a list item. * @param indentLevel - The indentation level for the entire list. Defaults to 0. If negative, it will be treated as 0 by the underlying `toListItem` calls. * @returns A string formatted as a Markdown list. * * @remarks * This function iterates through the provided array of strings and converts each string into a list item using the `toListItem` function. * Each list item is placed on a new line. The `toListItem` function ensures that negative `indentLevel` values are treated as 0. * The `indentLevel` parameter applies the same indentation to all items in the list. * * @example * ```typescript * import { toList } from '@msn088/x2md'; * const listItems = ['Item 1', 'Item 2', 'Item 3']; * const markdownList = toList(listItems); * // Output: * // - Item 1 * // - Item 2 * // - Item 3 * * const indentedList = toList(listItems, 1); * // Output: * // - Item 1 * // - Item 2 * // - Item 3 * ``` * * @public */ export declare function toList(items: string[], indentLevel?: number): string; /** * Converts a string to a list item with a specified indentation level. * * @param value - The text content of the list item. * @param indentLevel - The indentation level of the list item. Defaults to 0. If negative, it is treated as 0. * @returns A string formatted as a Markdown list item. * * @remarks * This function ensures that the `indentLevel` is non-negative; negative values are treated as 0. * It adds a hyphen (`-`) and a space before the provided text to create a list item. * The `indentLevel` parameter controls the number of spaces before the hyphen, with each level adding 4 spaces. * * @example * ```typescript * import { toListItem } from '@msn088/x2md'; * const item1 = toListItem('First item'); // "- First item\n" * const item2 = toListItem('Second item', 1); // " - Second item\n" * const item3 = toListItem('Third item', 2); // " - Third item\n" * ``` * * @public */ export declare function toListItem(value: string, indentLevel?: number): string; /** * Convert a MarkdownTable to a string representation. * * @param table - The MarkdownTable to convert. * @param customDelimiter - Optional custom delimiter row. * @returns String representation of the MarkdownTable. * @throws Error if the table structure is invalid. * * @remarks * This function takes a `MarkdownTable` object and converts it into a string formatted as a Markdown table. * It validates the table structure before conversion, ensuring that the header, delimiter, and data rows are correctly formatted. * If the table structure is invalid, it throws an error with a message indicating the issue. * * The `customDelimiter` parameter allows you to specify a custom delimiter row for the table. * If provided, it overrides the default delimiter row in the table. * * @example * ```typescript * import { toTable } from '@msn088/x2md'; * const table = { * header: ['Name', 'Age'], * delimiter: [ * { alignment: 'left' }, * { alignment: 'right' }, * ], * data: [ * ['Alice', '30'], * ['Bob', '25'], * ], * }; * const markdownTable = toTable(table); * console.log(markdownTable); * // Output: * | Name | Age | * | :--- | ---: | * | Alice | 30 | * | Bob | 25 | * ``` * * @public */ export declare function toTable(table: MarkdownTable, customDelimiter?: DelimiterRow): string; export { }