This repository was archived by the owner on Jan 13, 2025. It is now read-only.
forked from kylefarris/J2M
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·156 lines (146 loc) · 5.82 KB
/
index.js
File metadata and controls
executable file
·156 lines (146 loc) · 5.82 KB
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
var marked = require('marked');
marked.setOptions({
breaks: true,
smartyPants: true
});
var J2M = function() {};
J2M.prototype.md_to_html = function(str) {
return marked(str);
};
J2M.prototype.jira_to_html = function(str) {
return marked(this.to_markdown(str));
};
J2M.prototype.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 (buggy)
//.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+.*?\S)-(\s+)/g, '$1~~$2~~$3')
// Code Block
.replace(/\{code(:([a-z]+))?([:|]?(title|borderStyle|borderColor|borderWidth|bgColor|titleBGColor)=.+?)*\}([^]*?)\n?\{code\}/gm, '```$2\n$5\n```')
// Pre-formatted text
.replace(/{noformat}/g, '```')
// Un-named Links
.replace(/\[([^|]+?)\]/g, '<$1>')
// Images
.replace(/!(.+)!/g, '')
// 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, '|');
};
J2M.prototype.to_jira = function(str) {
var map = {
//cite: '??',
del: '-',
ins: '+',
sup: '^',
sub: '~'
};
return str
// tables
.replace(/^\n((?:\|.*?)+\|)[ \t]*\n((?:\|\s*?\-{3,}\s*?)+\|)[ \t]*\n((?:(?:\|.*?)+\|[ \t]*\n)*)$/gm,
function (match, headerLine, separatorLine, rowstr) {
var headers = headerLine.match(/[^|]+(?=\|)/g);
var separators = separatorLine.match(/[^|]+(?=\|)/g);
if (headers.length !== separators.length) {
return match;
}
var rows = rowstr.split('\n');
if (rows.length === 1 + 1 && headers.length === 1) {
// panel
return '{panel:title=' + headers[0].trim() + '}\n' +
rowstr.replace(/^\|(.*)[ \t]*\|/, '$1').trim() +
'\n{panel}\n';
} else {
return '||' + headers.join('||') + '||\n' + rowstr;
}
})
// Bold, Italic, and Combined (bold+italic)
.replace(/([*_]+)(\S.*?)\1/g, function (match,wrapper,content) {
switch (wrapper.length) {
case 1: return '_' + content + '_';
case 2: return '*' + content + '*';
case 3: return '_*' + content + '*_';
default: return wrapper + content * wrapper;
}
})
// All Headers (# format)
.replace(/^([#]+)(.*?)$/gm, function (match,level,content) {
return 'h' + level.length + '.' + content;
})
// Headers (H1 and H2 underlines)
.replace(/^(.*?)\n([=-]+)$/gm, function (match,content,level) {
return 'h' + (level[0] === '=' ? 1 : 2) + '. ' + content;
})
// Ordered lists
.replace(/^([ \t]*)\d+\.\s+/gm, function(match, spaces) {
return Array(Math.floor(spaces.length/2 + 1)).join("#") + '# ';
})
// Un-Ordered Lists
.replace(/^([ \t]*)\*\s+/gm, function(match, spaces) {
return Array(Math.floor(spaces.length/2 + 1)).join("*") + '* ';
})
// Headers (h1 or h2) (lines "underlined" by ---- or =====)
// Citations, Inserts, Subscripts, Superscripts, and Strikethroughs
.replace(new RegExp('<(' + Object.keys(map).join('|') + ')>(.*?)<\/\\1>', 'g'), function (match,from,content) {
var to = map[from];
return to + content + to;
})
// Other kind of strikethrough
.replace(/(\s+)~~(.*?)~~(\s+)/g, '$1-$2-$3')
// Named/Un-Named Code Block
.replace(/```(.+\n)?((?:.|\n)*?)```/g, function(match, synt, content) {
var code = '{code}';
if (synt) {
code = '{code:' + synt.replace(/\n/g, '') + "}\n";
}
return code + content + '{code}';
})
// Inline-Preformatted Text
.replace(/`([^`]+)`/g, '{{$1}}')
// Images
.replace(/!\[[^\]]*\]\(([^)]+)\)/g, '!$1!')
// Named Link
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '[$1|$2]')
// Un-Named Link
.replace(/<([^>]+)>/g, '[$1]')
// Single Paragraph Blockquote
.replace(/^>/gm, 'bq.');
};
module.exports = new J2M();