UNPKG

@felisdiligens/md-table-tools

Version:

MultiMarkdown table tools

181 lines (180 loc) 6.83 kB
/** indicates how text is aligned in a column */ export declare enum TextAlignment { left = "left", center = "center", right = "right", default = "start" } /** indicates how a cell is merged with a neighboring cell */ export declare enum TableCellMerge { above = 0, left = 1, none = 2 } /** indicates the placement of the table caption */ export declare enum TableCaptionPosition { top = "top", bottom = "bottom" } export declare class IndexOutOfBoundsError extends Error { constructor(msg: string); } export declare class TableCaption { text: string; label: string; position: TableCaptionPosition; constructor(text?: string, label?: string, position?: TableCaptionPosition); getLabel(): string; } export 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; } export 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[]; } export 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[]; } export 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; }