This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
J2M.js
244 lines (195 loc) · 6.55 KB
/
J2M.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(function() {
/**
* Takes Jira markup and converts it to Markdown.
*
* https://jira.atlassian.com/secure/WikiRendererHelpAction.jspa?section=all
*
* @param {string} input - Jira markup text
* @returns {string} - Markdown formatted text
*/
function toM(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;
};
/**
* Takes Markdown and converts it to Jira formatted text
*
* @param {string} input
* @returns {string}
*/
function toJ(input) {
// remove sections that shouldn't be recursively processed
var START = 'J2MBLOCKPLACEHOLDER';
var replacementsList = [];
var counter = 0;
input = input.replace(/`{3,}(\w+)?((?:\n|.)+?)`{3,}/g, function(match, synt, content) {
var code = '{code';
if (synt) {
code += ':' + synt;
}
code += '}' + content + '{code}';
var key = START + counter++ + '%%';
replacementsList.push({key: key, value: code});
return key;
});
input = input.replace(/`([^`]+)`/g, function(match, content) {
var code = '{{'+ content + '}}';
var key = START + counter++ + '%%';
replacementsList.push({key: key, value: code});
return key;
});
input = input.replace(/`([^`]+)`/g, '{{$1}}');
input = input.replace(/^(.*?)\n([=-])+$/gm, function (match,content,level) {
return 'h' + (level[0] === '=' ? 1 : 2) + '. ' + content;
});
input = input.replace(/^([#]+)(.*?)$/gm, function (match,level,content) {
return 'h' + level.length + '.' + content;
});
input = input.replace(/([*_]+)(.*?)\1/g, function (match,wrapper,content) {
var to = (wrapper.length === 1) ? '_' : '*';
return to + content + to;
});
// multi-level bulleted list
input = input.replace(/^(\s*)- (.*)$/gm, function (match,level,content) {
var len = 2;
if(level.length > 0) {
len = parseInt(level.length/4.0) + 2;
}
return Array(len).join("-") + ' ' + content;
});
// multi-level numbered list
input = input.replace(/^(\s+)1. (.*)$/gm, function (match, level, content) {
var len = 2;
if (level.length > 1) {
len = parseInt(level.length / 4) + 2;
}
return Array(len).join("#") + ' ' + content;
});
var map = {
cite: '??',
del: '-',
ins: '+',
sup: '^',
sub: '~'
};
input = input.replace(new RegExp('<(' + Object.keys(map).join('|') + ')>(.*?)<\/\\1>', 'g'), function (match,from,content) {
//console.log(from);
var to = map[from];
return to + content + to;
});
input = input.replace(/<span style="color:(#[^"]+)">([^]*?)<\/span>/gm, '{color:$1}$2{color}');
input = input.replace(/~~(.*?)~~/g, '-$1-');
// Images without alt
input = input.replace(/!\[\]\(([^)\n\s]+)\)/g, '!$1!');
// Images with alt
input = input.replace(/!\[([^\]\n]+)\]\(([^)\n\s]+)\)/g, '!$2|alt=$1!');
input = input.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '[$1|$2]');
input = input.replace(/<([^>]+)>/g, '[$1]');
// restore extracted sections
for(var i =0; i < replacementsList.length; i++){
var sub = replacementsList[i];
input = input.replace(sub["key"], sub["value"]);
}
// 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];
if (line_content.match(/\|---/g) != null) {
lines[i-1] = lines[i-1].replace(/\|/g, "||")
lines.splice(i, 1)
}
}
// Join the split lines back
input = ""
for (var i = 0; i < lines.length; i++) {
input += lines[i] + "\n"
}
return input;
};
/**
* Exports object
* @type {{toM: toM, toJ: toJ}}
*/
var J2M = {
toM: toM,
toJ: toJ
};
// exporting that can be used in a browser and in node
try {
window.J2M = J2M;
} catch (e) {
// not a browser, we assume it is node
module.exports = J2M;
}
})();