@felisdiligens/md-table-tools
Version:
MultiMarkdown table tools
299 lines (289 loc) • 11 kB
TypeScript
import TurndownService from 'turndown';
/** indicates how text is aligned in a column */
declare enum TextAlignment {
left = "left",
center = "center",
right = "right",
default = "start"
}
/** indicates how a cell is merged with a neighboring cell */
declare enum TableCellMerge {
above = 0,
left = 1,
none = 2
}
/** indicates the placement of the table caption */
declare enum TableCaptionPosition {
top = "top",
bottom = "bottom"
}
declare class TableCaption {
text: string;
label: string;
position: TableCaptionPosition;
constructor(text?: string, label?: string, position?: TableCaptionPosition);
getLabel(): string;
}
declare class TableCell {
text: string;
table: Table;
row: TableRow;
column: TableColumn;
merged: TableCellMerge;
isHeader: boolean;
textAlign: TextAlignment;
constructor(table: Table, row: TableRow, column: TableColumn);
isHeaderCell(): boolean;
getTextAlignment(): TextAlignment;
setText(text: string): void;
getColspan(): number;
getRowspan(): number;
}
declare class TableRow {
index: number;
isHeader: boolean;
/** Only pertains to MultiMarkdown multiline feature. Ignored by other parsers/renderers. See Table.mergeMultilineRows() */
isMultiline: boolean;
startsNewSection: boolean;
cells: TableCell[];
constructor(index?: number, isHeader?: boolean,
/** Only pertains to MultiMarkdown multiline feature. Ignored by other parsers/renderers. See Table.mergeMultilineRows() */
isMultiline?: boolean, startsNewSection?: boolean);
updateCells(table: Table): void;
getCell(index: number): TableCell;
getCells(): TableCell[];
}
declare class TableColumn {
index: number;
textAlign: TextAlignment;
wrappable: boolean;
cells: TableCell[];
constructor(index?: number, textAlign?: TextAlignment, wrappable?: boolean);
updateCells(table: Table): void;
getCell(index: number): TableCell;
getCells(): TableCell[];
}
declare class Table {
private cells;
private rows;
private columns;
caption: TableCaption;
/** Text before the table */
beforeTable: string;
/** Text after the table */
afterTable: string;
constructor(rowNum?: number, colNum?: number);
/**
* Adds a TableRow to the table.
* @param index Insert row at index. -1 means it's appended.
* @param row (optional)
* @returns The added row.
*/
addRow(index?: number, row?: TableRow): TableRow;
/**
* Adds a TableColumn to the table.
* @param index Insert column at index. -1 means it's appended.
* @param col (optional)
* @returns The added column.
*/
addColumn(index?: number, col?: TableColumn): TableColumn;
/** Gets the row at index. Negative index counts back from the end. Returns undefined if out-of-bounds. */
getRow(index: number): TableRow;
/** Gets the index of the row. -1 if it hasn't been found. */
indexOfRow(row: TableRow): number;
/** Gets the column at index. Negative index counts back from the end. Returns undefined if out-of-bounds. */
getColumn(index: number): TableColumn;
/** Gets the index of the column. -1 if it hasn't been found. */
indexOfColumn(col: TableColumn): number;
/**
* Removes the given column. Also removes all cells within the column.
* @param col Either index or object reference.
*/
removeColumn(col: number | TableColumn): void;
/**
* Removes the given row. Also removes all cells within the row.
* @param row Either index or object reference.
*/
removeRow(row: number | TableRow): void;
/**
* Moves the given column to the new index.
* @param col Either index or object reference.
* @param newIndex The new index of the given column.
* @throws {IndexOutOfBoundsError} Can't move column outside of table.
*/
moveColumn(col: number | TableColumn, newIndex: number): void;
/**
* Moves the given row to the new index.
* @param row Either index or object reference.
* @param newIndex The new index of the given row.
* @throws {IndexOutOfBoundsError} Can't move row outside of table.
*/
moveRow(row: number | TableRow, newIndex: number): void;
/** Returns a list of all rows that are headers. */
getHeaderRows(): TableRow[];
/** Returns a list of all rows that aren't headers. */
getNormalRows(): TableRow[];
/** Retruns all rows in the table, from top to bottom, including header rows. */
getRows(): TableRow[];
/** Returns all columns in the table, from left to right. */
getColumns(): TableColumn[];
/** Returns all cells in the table. Isn't necessarily sorted! */
getCells(): TableCell[];
/**
* Returns all cells within the given row.
* See also: {@link TableRow.getCells()}
* @param row Either index or object reference.
*/
getCellsInRow(row: number | TableRow): TableCell[];
/**
* Returns all cells within the given column.
* See also: {@link TableColumn.getCells()}
* @param column Either index or object reference.
*/
getCellsInColumn(column: number | TableColumn): TableCell[];
/** Returns the cell at row and column. */
private getCellByObjs;
/**
* Returns the cell at row and column.
* If the cell doesn't already exist, it will be created.
* @param row Either index or object reference.
* @param column Either index or object reference.
* @returns The cell at row and column.
*/
getCell(row: number | TableRow, column: number | TableColumn): TableCell;
/**
* Adds the cell to the Table and the cell's respective TableRow and TableColumn.
* (Be careful not to add a cell with row/column that already exist. Otherwise, the added cell will be overshadowed and not be used.)
*/
addCell(cell: TableCell): void;
/** Returns the total amount of rows in the table, including the header rows. */
rowCount(): number;
/** Returns the total amount of columns in the table. */
columnCount(): number;
/**
* → Ensures that all table cells exist.
* → Updates indices and sorts the cells within rows and columns.
* → Tries to find invalid configurations and sanitize them.
*
* Call this method after altering the table.
*/
update(): Table;
/** Tries to find invalid configurations and sanitize them. */
private sanitize;
/**
* Merges multiline rows (from MultiMarkdown feature) into "normal" rows.
* This will destroy MultiMarkdown formatting! Only use when rendering into different formats.
*/
mergeMultilineRows(): Table;
}
interface TableParser {
parse(table: string): Table;
}
interface TableRenderer {
render(table: Table): string;
}
declare class CSVTableParser implements TableParser {
separator: string;
quote: string;
assumeFirstLineIsHeader: boolean;
constructor(separator?: string, quote?: string, assumeFirstLineIsHeader?: boolean);
parse(table: string): Table;
}
/** changes the output of CSVTableRenderer */
declare enum CSVTableRendererMode {
OmitSpecialCharacters = 0,
EscapeWithQuotes = 1,
AlwaysUseQuotes = 2
}
declare class CSVTableRenderer implements TableRenderer {
separator: string;
quote: string;
lineBreak: string;
mode: CSVTableRendererMode;
constructor(separator?: string, quote?: string, lineBreak?: string, mode?: CSVTableRendererMode);
render(table: Table): string;
}
declare class GitHubFlavoredMarkdownTableParser implements TableParser {
parse(table: string): Table;
}
declare class GitHubFlavoredMarkdownTableRenderer implements TableRenderer {
prettify: boolean;
renderOutsideTable: boolean;
constructor(prettify?: boolean, renderOutsideTable?: boolean);
render(table: Table): string;
private renderDelimiterRow;
private renderRow;
private renderCell;
private determineColumnWidth;
private determineColumnWidths;
}
/** changes the behavior of HTMLTableParser */
declare enum HTMLTableParserMode {
/** uses only text (`Cheerio.text()`) */
StripHTMLElements = 0,
/** uses the HTML code (`Cheerio.html()`) without any converting */
PreserveHTMLElements = 1,
/** uses the HTML code (`Cheerio.html()`) and converts to Markdown using Turndown if possible (default) */
ConvertHTMLElements = 2
}
declare class HTMLTableParser implements TableParser {
mode: HTMLTableParserMode;
turndownService: TurndownService;
constructor(mode?: HTMLTableParserMode, turndownService?: TurndownService);
parse(table: string): Table;
private parseSection;
private parseCell;
}
declare class HTMLTableRenderer implements TableRenderer {
prettify: boolean;
indent: string;
renderOutsideTable: boolean;
constructor(prettify?: boolean, indent?: string, renderOutsideTable?: boolean);
render(table: Table): string;
private renderRow;
private renderCell;
private indentString;
}
declare class MultiMarkdownTableParser implements TableParser {
parse(table: string): Table;
}
declare class MinifiedMultiMarkdownTableRenderer implements TableRenderer {
renderOutsideTable: boolean;
constructor(renderOutsideTable?: boolean);
render(table: Table): string;
private renderCaption;
private renderSeparator;
private renderRow;
}
declare class PrettyMultiMarkdownTableRenderer implements TableRenderer {
renderOutsideTable: boolean;
constructor(renderOutsideTable?: boolean);
render(table: Table): string;
private renderCaption;
private renderSeparator;
private renderRow;
private renderCell;
private determineColumnWidths;
}
declare class DokuWikiTableParser implements TableParser {
/** If true, converts DokuWiki syntax to Markdown syntax */
convertMarkup: boolean;
constructor(
/** If true, converts DokuWiki syntax to Markdown syntax */
convertMarkup?: boolean);
parse(table: string): Table;
}
declare class DokuWikiTableRenderer implements TableRenderer {
/** If true, converts Markdown syntax to DokuWiki syntax */
convertMarkup: boolean;
constructor(
/** If true, converts Markdown syntax to DokuWiki syntax */
convertMarkup?: boolean);
render(table: Table): string;
private renderCaption;
private renderRow;
private renderCell;
private renderText;
private determineColumnWidths;
}
export { CSVTableParser, CSVTableRenderer, CSVTableRendererMode, DokuWikiTableParser, DokuWikiTableRenderer, GitHubFlavoredMarkdownTableParser, GitHubFlavoredMarkdownTableRenderer, HTMLTableParser, HTMLTableParserMode, HTMLTableRenderer, MinifiedMultiMarkdownTableRenderer, MultiMarkdownTableParser, PrettyMultiMarkdownTableRenderer, Table, TableCaption, TableCaptionPosition, TableCell, TableCellMerge, TableColumn, TableParser, TableRenderer, TableRow, TextAlignment };