-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.markdown.js
146 lines (125 loc) · 4.5 KB
/
jquery.markdown.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
(function () {
var markdown = {
// singleton preview iframe
preview: null,
// showdown converter
converter: new Showdown.converter,
init: function (textarea, preview) {
if (!markdown.preview && preview) {
var iframe = $('<iframe/>').appendTo(preview);
iframe = iframe[0];
var idoc = iframe.contentWindow ? iframe.contentWindow.document /* For IE5.5 and IE 6*/:
iframe.contentDocument ? iframe.contentDocument /* For NS6 */: iframe.document /* the rest */;
idoc.open();
idoc.writeln('<div id="formatted"></div>');
idoc.close();
markdown.preview = $('#formatted', idoc);
markdown.preview.html(markdown.converter.makeHtml($(textarea).val()));
}
var show_preview = preview ? {
change: function(value) {
markdown.preview.html(markdown.converter.makeHtml(value));
}
} : {}
var textarea = $(textarea).TextArea(show_preview);
var toolbar = $.Toolbar(textarea, {
className: "markdown_toolbar"
});
markdown.add_buttons(toolbar);
},
add_buttons: function (toolbar) {
toolbar.addButton('Italics',function(){
this.wrapSelection('*','*');
},{
id: 'markdown_italics_button',
title: 'italics'
});
toolbar.addButton('Bold',function(){
this.wrapSelection('**','**');
},{
id: 'markdown_bold_button',
title: 'bold'
});
toolbar.addButton('Link',function(){
var selection = this.getSelection();
var response = prompt('Enter Link URL','');
if(response == null) {
return;
}
this.replaceSelection('[' + (selection == '' ? 'Link Text' : selection) + '](' + (response == '' ? 'http://link_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ')');
},{
id: 'markdown_link_button',
title: 'insert http link'
});
toolbar.addButton('Image',function(){
var selection = this.getSelection();
var response = prompt('Enter Image URL','');
if(response == null)
return;
this.replaceSelection('![' + (selection == '' ? 'Image Alt Text' : selection) + '](' + (response == '' ? 'http://image_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ')');
},{
id: 'markdown_image_button',
title: 'insert image'
});
toolbar.addButton('Heading',function(){
var selection = this.getSelection() || 'Heading';
if (selection == '') {
selection = 'Heading';
}
for (var underscores = [], i = 0; i < Math.max(5,selection.length); i++) {
underscores.push('-')
}
this.replaceSelection("\n" + selection + "\n" + underscores.join(''));
},{
id: 'markdown_heading_button',
title: 'heading'
});
toolbar.addButton('Unordered List',function(event){
this.collectFromEachSelectedLine(function(line){
return event.shiftKey ? (line.match(/^\*{2,}/) ? line.replace(/^\*/,'') : line.replace(/^\*\s/,'')) : (line.match(/\*+\s/) ? '*' : '* ') + line;
});
},{
id: 'markdown_unordered_list_button',
title: 'bullet list'
});
toolbar.addButton('Ordered List',function(event){
var i = 0;
this.collectFromEachSelectedLine(function(line){
if(!line.match(/^\s+$/)){
++i;
return event.shiftKey ? line.replace(/^\d+\.\s/,'') : (line.match(/\d+\.\s/) ? '' : i + '. ') + line;
}
});
},{
id: 'markdown_ordered_list_button',
title: 'number list'
});
toolbar.addButton('Block Quote',function(event){
this.collectFromEachSelectedLine(function(line){
return event.shiftKey ? line.replace(/^\> /,'') : '> ' + line;
});
},{
id: 'markdown_quote_button',
title: 'block quote'
});
toolbar.addButton('Code Block',function(event){
this.collectFromEachSelectedLine(function(line){
return event.shiftKey ? line.replace(/ /,'') : ' ' + line;
});
},{
id: 'markdown_code_button',
title: 'code block'
});
toolbar.addButton('Help',function(){
window.open('http://daringfireball.net/projects/markdown/dingus');
},{
id: 'markdown_help_button',
title: 'help'
});
}
}
$.fn.markdown = function (preview) {
var textarea = $(this).get(0);
markdown.init(textarea, preview);
}
})();