Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added csv, tsv or markdown table to "pretty markdown table" Script #203

Merged
merged 3 commits into from
Mar 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Scripts/convertToMarkdownTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
{
"api":1,
"name":"Convert to pretty markdown table",
"description":"Converts csv, tsv or markdown table into pretty markdown table format.",
"author":"xshoji",
"icon":"term",
"tags":"csv,tsv,md,markdown"
}
**/
function main(input) {
input.text = convertToPrettyMarkdownTableFormat(input.text);
}

function convertToPrettyMarkdownTableFormat(input) {
const list = input.trim().replace(/^(\r?\n)+$/g, "\n").split("\n").map(v => v.replace(/^\||\|$/g, ""));
const delimiter = [`|`, `\t`, `","`, `,`].find(v => list[0].split(v).length > 1);
if (delimiter === `|`) {
// If input text is markdown table format, removes header separator.
list.splice(1, 1);
}
const tableElements = list.map(record => record.split(delimiter).map(v => v.trim()));
const calcBytes = (character) => {
let length = 0;
for (let i = 0; i < character.length; i++) {
const c = character.charCodeAt(i);
// Multibyte handling
(c >= 0x0 && c < 0x81) || (c === 0xf8f0) || (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4) ? length += 1 : length += 2;
}
return length;
};
const columnMaxLengthList = tableElements[0].map((v, i) => i).reduce((map, columnIndex) => {
let maxLength = 0;
tableElements.forEach(record => maxLength < calcBytes(record[columnIndex]) ? maxLength = calcBytes(record[columnIndex]) : null);
if (maxLength === 1) {
// Avoids markdown header line becomes only ":" ( ":-" is correct. ).
maxLength = 2;
}
map[columnIndex] = maxLength;
return map;
}, {})
const formattedTableElements = tableElements.map(record => record.map((value, columnIndex) => value + "".padEnd(columnMaxLengthList[columnIndex] - calcBytes(value), " ")));
const headerValues = formattedTableElements.shift();
const tableLine = headerValues.map(v => "".padStart(calcBytes(v), "-").replace(/^./, ":"));
formattedTableElements.unshift(tableLine);
formattedTableElements.unshift(headerValues);
return formattedTableElements.map(record => "| " + record.join(" | ") + " |").join("\n");
}