clean ∏
clean up markdown table

Script key: clean up markdown table

Posted by @cpaconnelly

Install

Details

Label clean ∏
Voice Over clean up markdown table
Script
// for fixed-width fonts, clean up a selected markdown table by making all columns the same width and adding padding where necessary to get alignment indicated in separator row



var uglyTable = getSelectedText();

// parse ugly markdown table into multi-dimensional array

var rows = uglyTable.split("\n"); // break it into rows
var cells = []; // create array to hold cells

for (i = 0; i < rows.length; i++) { //loop through rows, breaking them into cells
    rows[i] = rows[i].replace(/\|(.+)\|/g, '$1'); //strip out the outside pipes
    cells[i] = rows[i].split("|");
}


// this result in each cell being able to be accessed by cells[row][column]

var columnCount = cells[0].length; // get column count

// trim white space from each cell

for (i = 0; i < columnCount; i++) {
	for (j = 0; j < rows.length; j++) {
		cells[j][i] = cells[j][i].trim();
	}
}

var columnWidth = []; // set array to hold column widths

// get maximum width for each column and store it in an array

for (i = 0; i < columnCount; i++) {
	columnWidth[i] = 0;
	for (j = 0; j < rows.length; j++) {
		if (cells[j][i].length > columnWidth[i]) {
			columnWidth[i] = cells[j][i].length;
			if (columnWidth[i] > 50) {
				columnWidth[i] = 50

			}
		}
	}
}

// get alignment for each column and store it in an array

var columnAlignment = []
for (i = 0; i < columnCount; i++) {
    if (cells[1][i].endsWith(':')) {
        if (cells[1][i].startsWith(':')) {
            columnAlignment[i] = 'center';
        } else {
            columnAlignment[i] = 'right';
        }    
    } else {
        columnAlignment[i] = 'left';
    }
}


// add whitespace (evaluating whether should be added before or after content) to row


for (i = 0; i < columnCount; i++) {
	for (j = 0; j < rows.length; j++) {
		while (cells[j][i].length < columnWidth[i]) {
			if (columnAlignment[i] == 'left') {
		       cells[j][i] = cells[j][i] + ' ';
			}
			if (columnAlignment[i] == 'right') {
			    cells[j][i] = ' ' + cells[j][i];
			}
			if (columnAlignment[i] == 'center') {
			    cells[j][i] = cells[j][i] + ' ';
			    if (cells[j][i].length < columnWidth[i]) {
			        cells[j][i] = ' ' + cells[j][i];
			    }
			}
		}
	}
}

// fix separator row by removing spaces and adding dashes

for (i = 0; i < columnCount; i++) {
    if (columnAlignment[i] == 'center') {
        cells[1][i] = '-';
        while (cells[1][i].length + 2 < columnWidth[i]) {
            cells[1][i] = cells[1][i] + '-';
        }
        cells[1][i] = ':' + cells[1][i] + ':';
    } else {
        cells[1][i] = cells[1][i].replace(/ /g,'-');
    }
}



// recompile table

for (i = 0; i < rows.length; i++) {
    rows[i] = '| ' + cells[i].join(' | ') + ' |'; // include padding and external pipes
}

neatTable = rows.join('\n');

setSelectedText(neatTable);

Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus