-
Notifications
You must be signed in to change notification settings - Fork 84
/
index.js
119 lines (109 loc) · 2.81 KB
/
index.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
var marked = require('marked')
var _ = require('min-util')
var qs = require('min-qs')
var inlineLexer = marked.inlineLexer
module.exports = exports = markdown2confluence
// https://roundcorner.atlassian.net/secure/WikiRendererHelpAction.jspa?section=all
// https://confluence.atlassian.com/display/DOC/Confluence+Wiki+Markup
// http://blogs.atlassian.com/2011/11/why-we-removed-wiki-markup-editor-in-confluence-4/
var MAX_CODE_LINE = 20
function Renderer() {}
var rawRenderer = marked.Renderer
var langArr = 'actionscript3 bash csharp coldfusion cpp css delphi diff erlang groovy java javafx javascript perl php none powershell python ruby scala sql vb html/xml'.split(/\s+/)
var langMap = {
shell: 'bash',
html: 'html',
xml: 'xml'
}
for (var i = 0, x; x = langArr[i++];) {
langMap[x] = x
}
_.extend(Renderer.prototype, rawRenderer.prototype, {
paragraph: function(text) {
return text + '\n\n'
}
, html: function(html) {
return html
}
, heading: function(text, level, raw) {
return 'h' + level + '. ' + text + '\n\n'
}
, strong: function(text) {
return '*' + text + '*'
}
, em: function(text) {
return '_' + text + '_'
}
, del: function(text) {
return '-' + text + '-'
}
, codespan: function(text) {
return '{{' + text + '}}'
}
, blockquote: function(quote) {
return '{quote}' + quote + '{quote}'
}
, br: function() {
return '\n'
}
, hr: function() {
return '----'
}
, link: function(href, title, text) {
var arr = [href]
if (text) {
arr.unshift(text)
}
return '[' + arr.join('|') + ']'
}
, list: function(body, ordered) {
var arr = _.filter(_.trim(body).split('\n'), function(line) {
return line
})
var type = ordered ? '#' : '*'
return _.map(arr, function(line) {
return type + ' ' + line
}).join('\n') + '\n\n'
}
, listitem: function(body, ordered) {
return body + '\n'
}
, image: function(href, title, text) {
return '!' + href + '!'
}
, table: function(header, body) {
return header + body + '\n'
}
, tablerow: function(content, flags) {
return content + '\n'
}
, tablecell: function(content, flags) {
var type = flags.header ? '||' : '|'
return type + content
}
, code: function(code, lang) {
// {code:language=java|borderStyle=solid|theme=RDark|linenumbers=true|collapse=true}
if(lang) {
lang = lang.toLowerCase()
}
lang = langMap[lang] || 'none'
var param = {
language: lang,
borderStyle: 'solid',
theme: 'RDark', // dark is good
linenumbers: true,
collapse: false
}
var lineCount = _.split(code, '\n').length
if (lineCount > MAX_CODE_LINE) {
// code is too long
param.collapse = true
}
param = qs.stringify(param, '|', '=')
return '{code:' + param + '}\n' + code + '\n{code}\n\n'
}
})
var renderer = new Renderer()
function markdown2confluence(markdown) {
return marked(markdown, {renderer: renderer})
}