Created by
mason.malone
| to_markdown = function(str) {
return str
// Ordered Lists
.replace(/^[ \t]*(\*+)\s+/gm, function(match, stars) {
return Array(stars.length).join(" ") + '* ';
})
// Un-ordered lists
.replace(/^[ \t]*(#+)\s+/gm, function(match, nums) {
return Array(nums.length).join(" ") + '1. ';
})
// Headers 1-6
.replace(/^h([0-6])\.(.*)$/gm, function (match, level, content) {
return Array(parseInt(level) + 1).join('#') + content;
})
// Bold
.replace(/\*(\S.*)\*/g, '**$1**')
// Italic
.replace(/\_(\S.*)\_/g, '*$1*')
// Monospaced text
.replace(/\{\{([^}]+)\}\}/g, '`$1`')
// Citations
//.replace(/\?\?((?:.[^?]|[^?].)+)\?\?/g, '<cite>$1</cite>')
// Inserts
.replace(/\+([^+]*)\+/g, '<ins>$1</ins>')
// Superscript
//.replace(/\^([^^]*)\^/g, '<sup>$1</sup>')
// Subscript
//.replace(/~([^~]*)~/g, '<sub>$1</sub>')
// Strikethrough
//.replace(/-(\S+.*?\S)-/g, '~~$1~~')
// Code Block
.replace(/\{code(:([a-z]+))?\}([^]*)\{code\}/gm, '```$2$3```')
// Pre-formatted text
.replace(/{noformat}/g, '```')
// Un-named Links
//.replace(/\[([^|]+)\]/g, '<$1>')
// Named Links
.replace(/\[(.+?)\|(.+)\]/g, '[$1]($2)')
// Single Paragraph Blockquote
.replace(/^bq\.\s+/gm, '> ')
// Remove color: unsupported in md
.replace(/\{color:[^}]+\}([^]*)\{color\}/gm, '$1')
// panel into table
.replace(/\{panel:title=([^}]*)\}\n?([^]*?)\n?\{panel\}/gm, '\n| $1 |\n| --- |\n| $2 |')
// table header
.replace(/^[ \t]*((?:\|\|.*?)+\|\|)[ \t]*$/gm, function (match, headers) {
var singleBarred = headers.replace(/\|\|/g,'|');
return '\n' + singleBarred + '\n' + singleBarred.replace(/\|[^|]+/g, '| --- ');
})
// remove leading-space of table headers and rows
.replace(/^[ \t]*\|/gm, '|');
};
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : 'foo',
database : 'jira'
});
connection.connect();
var query = connection.query('SELECT id, description FROM jiraissue ORDER by id DESC');
query.on('result', function (row) {
if (!row.description) { return; }
var md = to_markdown(row.description);
console.log("Update Jira Issue #" + row.id);
connection.query('UPDATE jiraissue SET description = ? WHERE id = ?', [md, row.id]);
}).on('end', function() {
connection.end();
});
|