-
Notifications
You must be signed in to change notification settings - Fork 16
/
JiraToMarkdown.js
106 lines (84 loc) · 3.24 KB
/
JiraToMarkdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
{
"id": "co.ameba.Esse.ExternalFunctions.JiraToMD",
"name": "Jira to Markdown",
"description": "Converts Jira markup to GitHub flavored markdown",
"category":"Convert",
"author":"Ameba Labs",
}
**/
//adopted from https://github.com/FokkeZB/J2M
function main(input) {
input = input.replace(/^bq\.(.*)$/gm, function (match, content) {
return '> ' + content + "\n";
});
input = input.replace(/([*_])(.*)\1/g, function (match,wrapper,content) {
var to = (wrapper === '*') ? '**' : '*';
return to + content + to;
});
// multi-level numbered list
input = input.replace(/^((?:#|-|\+|\*)+) (.*)$/gm, function (match, level, content) {
var len = 2;
var prefix = '1.';
if (level.length > 1) {
len = parseInt((level.length - 1) * 4) + 2;
}
// take the last character of the level to determine the replacement
var prefix = level[level.length - 1];
if (prefix == '#') prefix = '1.';
return Array(len).join(" ") + prefix + ' ' + content;
});
// headers, must be after numbered lists
input = input.replace(/^h([0-6])\.(.*)$/gm, function (match,level,content) {
return Array(parseInt(level) + 1).join('#') + content;
});
input = input.replace(/\{\{([^}]+)\}\}/g, '`$1`');
input = input.replace(/\?\?((?:.[^?]|[^?].)+)\?\?/g, '<cite>$1</cite>');
input = input.replace(/\+([^+]*)\+/g, '<ins>$1</ins>');
input = input.replace(/\^([^^]*)\^/g, '<sup>$1</sup>');
input = input.replace(/~([^~]*)~/g, '<sub>$1</sub>');
input = input.replace(/-([^-]*)-/g, '-$1-');
input = input.replace(/\{code(:([a-z]+))?\}([^]*?)\{code\}/gm, '```$2$3```');
input = input.replace(/\{quote\}([^]*)\{quote\}/gm, function(match, content) {
lines = content.split(/\r?\n/gm);
for (var i = 0; i < lines.length; i++) {
lines[i] = '> ' + lines[i];
}
return lines.join("\n");
});
// Images with alt= among their parameters
input = input.replace(/!([^|\n\s]+)\|([^\n!]*)alt=([^\n!\,]+?)(,([^\n!]*))?!/g, '![$3]($1)');
// Images with just other parameters (ignore them)
input = input.replace(/!([^|\n\s]+)\|([^\n!]*)!/g, '![]($1)');
// Images without any parameters or alt
input = input.replace(/!([^\n\s!]+)!/g, '![]($1)');
input = input.replace(/\[([^|]+)\|(.+?)\]/g, '[$1]($2)');
input = input.replace(/\[(.+?)\]([^\(]+)/g, '<$1>$2');
input = input.replace(/{noformat}/g, '```');
input = input.replace(/{color:([^}]+)}([^]*?){color}/gm, '<span style="color:$1">$2</span>');
// Convert header rows of tables by splitting input on lines
lines = input.split(/\r?\n/gm);
lines_to_remove = []
for (var i = 0; i < lines.length; i++) {
line_content = lines[i];
seperators = line_content.match(/\|\|/g);
if (seperators != null) {
lines[i] = lines[i].replace(/\|\|/g, "|");
console.log(seperators)
// Add a new line to mark the header in Markdown,
// we require that at least 3 -'s are between each |
header_line = "";
for (var j = 0; j < seperators.length-1; j++) {
header_line += "|---";
}
header_line += "|";
lines.splice(i+1, 0, header_line);
}
}
// Join the split lines back
input = ""
for (var i = 0; i < lines.length; i++) {
input += lines[i] + "\n"
}
return input;
}