Skip to content

Instantly share code, notes, and snippets.

@djsipe
Created July 6, 2017 20:47
Show Gist options
  • Save djsipe/993403d9be7d208e71968db90c91baaf to your computer and use it in GitHub Desktop.
Save djsipe/993403d9be7d208e71968db90c91baaf to your computer and use it in GitHub Desktop.
This script can be used to export a data table result from DataGrip (or other JetBrains IDEAs) as a basic Markdown table. Helpful for documentation.
/*
Markdown Table Export Script for DataGrip
-----------------------------------------
This simple script is a modified version of the HTML script that allows you to
output a table of data as a Markdown table. The output isn't pretty, but it
should be rendered correctly.
@author Donald Sipe
*/
function eachWithIdx(iterable, f) { var i = iterable.iterator(); var idx = 0; while (i.hasNext()) f(i.next(), idx++); }
function mapEach(iterable, f) { var vs = []; eachWithIdx(iterable, function (i) { vs.push(f(i));}); return vs; }
function escape(str) {
str = str.replaceAll("\t|\b|\\f", "");
str = com.intellij.openapi.util.text.StringUtil.escapeXml(str);
str = str.replaceAll("\\r|\\n|\\r\\n", "<br/>");
return str;
}
var NEWLINE = "\n";
function output() {
for (var i = 0; i < arguments.length; i++) {
OUT.append(arguments[i]);
}
}
function outputRow(items) {
output("|");
for (var i = 0; i < items.length; i++)
output(escape(items[i]), "|");
output(NEWLINE);
}
// No support for transposed right now.
// Output the heading
outputRow(mapEach(COLUMNS, function (col) { return " " + col.name() + " "; }));
// |-----------|-----------| Line under heading triggers MD table
outputRow(mapEach(COLUMNS, function () { return '-----'; }));
// Write the rows
eachWithIdx(ROWS, function (row) {
outputRow(mapEach(COLUMNS, function (col) { return " " + FORMATTER.format(row, col) + " "; }))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment