DataGrip (IntelliJ) output SQL results to Markdown
Open the scripts directory. There are a couple ways to do this...
Create a new file (Right Click -> New -> File) and name it Markdown-JavaScript.markdown.js
.
Paste the contents of Markdown-JavaScript.markdown.js
into this file.
Execute a query, select Markdown-JavaScript.markdown.js
from the output format dropdown, click the download icon and choose To File
or To Clipboard
.
This is based on the HTML-JavaScript.html.js
extractor which comes with DataGrip. Markdown escape logic taken from https://github.com/kemitchell/markdown-escape.js.
if (!String.prototype.repeat) {
// polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
// Could we try:
// return Array(count + 1).join(this);
return rpt;
}
}
// credit: https://github.com/kemitchell/markdown-escape.js
var replacements = [
[ /\*/g, '\\*' ],
[ /#/g, '\\#' ],
[ /\//g, '\\/' ],
[ /\(/g, '\\(' ],
[ /\)/g, '\\)' ],
[ /\[/g, '\\[' ],
[ /\]/g, '\\]' ],
[ /\</g, '<' ],
[ /\>/g, '>' ],
[ /_/g, '\\_' ] ];
function escapeMarkdown(string) {
return replacements.reduce(
function(string, replacement) {
return string.replace(replacement[0], replacement[1])
},
string)
}
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 escapeMarkdown(str);
}
var NEWLINE = "\n";
function output() { for (var i = 0; i < arguments.length; i++) { OUT.append(arguments[i]); } }
function outputRow(items) {
for (var i = 0; i < items.length; i++) {
output("|");
output(escape(items[i]));
if(i == items.length - 1) {
output("|"); // trailing pipe
}
}
output(NEWLINE);
}
output("Queried: " + (new Date()).toLocaleDateString(), NEWLINE, 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(); })); // header names
outputRow(mapEach(COLUMNS, function (col) { return '-'.repeat(Math.max(4, col.name().length)); })); // header separator
eachWithIdx(ROWS, function (row) {
outputRow(mapEach(COLUMNS, function (col) { return FORMATTER.format(row, col); }))
});
}
output(NEWLINE);