param text export function markdownHighlight ( text ) { return `\`${ text }\`` ; } param level param title export function markdownHeader ( level , title ) { return `${ "#" . repeat ( level ) } ${ title }` ; } export interface MarkdownTableOptions { removeEmptyColumns : boolean ; minCellWidth : number ; maxCellWidth : number ; cellPadding : number ; } const DEFAULT_MARKDOWN_TABLE_OPTIONS = { removeEmptyColumns : true , minCellWidth : 3 , maxCellWidth : 50 , cellPadding : 1 } ; param rows param options export function markdownTable ( rows , options = { } ) { const MIN_CELL_WIDTH = options . minCellWidth || DEFAULT_MARKDOWN_TABLE_OPTIONS . minCellWidth ; const MAX_CELL_WIDTH = options . maxCellWidth || DEFAULT_MARKDOWN_TABLE_OPTIONS . maxCellWidth ; const CELL_PADDING = options . cellPadding || DEFAULT_MARKDOWN_TABLE_OPTIONS . cellPadding ; let columnCount = Math . max ( ... rows . map ( r => r . length ) ) ; if ( options . removeEmptyColumns ) { const emptyColumns = Array ( columnCount ) . fill ( false ) . map ( ( b , i ) => i !== 0 && rows . slice ( 1 ) . find ( r => r [ i ] != null && r [ i ] . length > 0 ) == null ) ; if ( emptyColumns . includes ( true ) ) { rows = rows . map ( row => row . filter ( ( column , i ) => ! emptyColumns [ i ] ) ) ; columnCount = Math . max ( ... rows . map ( r => r . length ) ) ; } } rows = rows . map ( r => r . map ( markdownEscapeTableCell ) ) ; const columnWidths = Array ( columnCount ) . fill ( 0 ) . map ( ( c , i ) => Math . min ( MAX_CELL_WIDTH , Math . max ( MIN_CELL_WIDTH , ... rows . map ( r => ( r [ i ] || "" ) . length ) ) + CELL_PADDING * 2 ) ) ; return `
|${ rows [ 0 ] . map ( ( r , i ) => fillWidth ( r , columnWidths [ i ] , CELL_PADDING ) ) . join ( "|" ) }|
|${ columnWidths . map ( c => "-" . repeat ( c ) ) . join ( "|" ) }|
${ rows . slice ( 1 ) . map ( r => `|${ r . map ( ( r , i ) => fillWidth ( r , columnWidths [ i ] , CELL_PADDING ) ) . join ( "|" ) }|` ) . join ( "\n" ) }
` ; } param text function markdownEscapeTableCell ( text ) { return text . replace ( /\n/g , "<br />" ) . replace ( /\|/g , "\\|" ) ; } param text param width param paddingStart function fillWidth ( text , width , paddingStart ) { return " " . repeat ( paddingStart ) + text + " " . repeat ( Math . max ( 1 , width - text . length - paddingStart ) ) ; }