Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dennisobrien
Created October 1, 2017 04:52
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisobrien/d3a6a141ff4a92566cf9fdc1e7d79bd9 to your computer and use it in GitHub Desktop.
Save dennisobrien/d3a6a141ff4a92566cf9fdc1e7d79bd9 to your computer and use it in GitHub Desktop.
DataGrip extension to export results to markdown.
/*
* DataGrip extension to export results to markdown.
* The markdown table format is supported by Github and Jira. I haven't tested others.
*
* Tested on DataGrip 2016.2.2
* - Open the File view. View -> Tool Windows -> Files
* - Activate the "Scratches" tab. You should see "Files", "Scopes", and "Scratches".
* - Copy this file to Extensions/Database Tools and SQL/data/extractors/Markdown.JavaScript.md.js
* - Run a query with some results.
* - Change the export format to Markdown.JavaScript.md.js and click "To Clipboard".
* - Paste the table markdown.
*
* This was largely based off of the extractor "HTML.JavaScript.html.js" that ships with DataGrip.
*/
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) {
var delimiter = " | ";
var left_delimiter = "| ";
var right_delimiter = " |";
output(left_delimiter);
output(items.join(delimiter));
output(right_delimiter);
output(NEWLINE);
}
if (TRANSPOSED) {
var values = mapEach(COLUMNS, function(col) { return [col.name()]; });
eachWithIdx(ROWS, function (row) {
eachWithIdx(COLUMNS, function (col, i) {
values[i].push(FORMATTER.format(row, col));
});
});
eachWithIdx(COLUMNS, function (_, i) { outputRow(values[i]); });
}
else {
outputRow(mapEach(COLUMNS, function (col) { return col.name(); }));
outputRow(mapEach(COLUMNS, function (col) { return "-----"; }));
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