This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJ2M.js
190 lines (153 loc) · 4.96 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
(function() {
/**
* Takes Jira markup and converts it to Markdown.
*
* @param {string} input - Jira markup text
* @returns {string} - Markdown formatted text
*/
function toM(input) {
input = input.replace(/^h([0-6])\.(.*)$/gm, function (match,level,content) {
return Array(parseInt(level) + 1).join('#') + content;
});
input = input.replace(/([*_])(.*)\1/g, function (match,wrapper,content) {
var to = (wrapper === '*') ? '**' : '*';
return to + content + to;
});
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(/!([^\n\s]+)!/, '![]($1)');
input = input.replace(/\[(.+?)\|(.+)\]/g, '[$1]($2)');
input = input.replace(/\[(.+?)\]([^\(]*)/g, '<$1>$2');
input = input.replace(/{noformat}/g, '```');
input = input.replace(/{quote}([\s\S]{1,10240}?){quote}/g, '<blockquote>$1</blockquote>');
// 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;
});
// Make multi-level bulleted lists work
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;
});
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(/~~(.*?)~~/g, '-$1-');
input = input.replace(/!\[[^\]]+\]\(([^)]+)\)/g, '!$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;
}
})();