-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.html
239 lines (192 loc) · 8.48 KB
/
index.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="urf-8">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>MarkDown</title>
<link rel="stylesheet" href="css/style.css">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
</head>
<body style="margin:0;padding:20px;background:#f8f8f8">
<div style="margin-top:1em">
<textarea style="height: 400px" autocomplete="off" id="text" name="text" class="markdown-textarea"></textarea>
</div>
<div style="line-height:24px;font-size:12px;color:#999;">By:<a style="color:#f90;text-decoration:none" href="http://www.zi-han.ne/">子涵</a> MarkDown使用手册可参考:<a style="color:#666;text-decoration:none"href="http://wowubuntu.com/markdown/">http://wowubuntu.com/markdown/</a></div>
<script src="js/jquery.js"></script>
<script src="js/jquery.scrollto.js"></script>
<script src="js/pagedown.js"></script>
<script src="js/pagedown-extra.js"></script>
<script src="js/diff.js"></script>
<script>
$(document).ready(function () {
var textarea = $('#text'),
toolbar = $('<div class="markdown-editor" id="md-button-bar" />').insertBefore(textarea.parent())
preview = $('<div id="md-preview" class="md-hidetab" />').insertAfter('.markdown-editor');
var options = {};
options.strings = {
bold: '加粗 <strong> Ctrl+B',
boldexample: '加粗文字',
italic: '斜体 <em> Ctrl+I',
italicexample: '斜体文字',
link: '链接 <a> Ctrl+L',
linkdescription: '请输入链接描述',
quote: '引用 <blockquote> Ctrl+Q',
quoteexample: '引用文字',
code: '代码 <pre><code> Ctrl+K',
codeexample: '请输入代码',
image: '图片 <img> Ctrl+G',
imagedescription: '请输入图片描述',
olist: '数字列表 <ol> Ctrl+O',
ulist: '普通列表 <ul> Ctrl+U',
litem: '列表项目',
heading: '标题 <h1>/<h2> Ctrl+H',
headingexample: '标题文字',
hr: '分割线 <hr> Ctrl+R',
more: '摘要分割线 <!--more--> Ctrl+M',
undo: '撤销 - Ctrl+Z',
redo: '重做 - Ctrl+Y',
redomac: '重做 - Ctrl+Shift+Z',
fullscreen: '全屏 - Ctrl+J',
exitFullscreen: '退出全屏 - Ctrl+E',
fullscreenUnsupport: '此浏览器不支持全屏操作',
imagedialog: '<p><b>插入图片</b></p><p>请在下方的输入框内输入要插入的远程图片地址</p>',
linkdialog: '<p><b>插入链接</b></p><p>请在下方的输入框内输入要插入的链接地址</p>',
ok: '确定',
cancel: '取消'
};
var converter = new Markdown.Converter(),
editor = new Markdown.Editor(converter, '', options),
diffMatch = new diff_match_patch(), last = '', preview = $('#md-preview'),
mark = '@mark' + Math.ceil(Math.random() * 100000000) + '@',
span = '<span class="diff" />';
// 设置markdown
Markdown.Extra.init(converter, {
extensions : ["tables", "fenced_code_gfm", "def_list", "attr_list", "footnotes"]
});
// 自动跟随
converter.hooks.chain('postConversion', function (html) {
// clear special html tags
html = html.replace(/<\/?(\!doctype|html|head|body|link|title|input|select|button|textarea|style|noscript)[^>]*>/ig, function (all) {
return all.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, ''')
.replace(/"/g, '"');
});
// clear hard breaks
html = html.replace(/\s*((?:<br>\n)+)\s*(<\/?(?:p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend|article|section|nav|aside|hgroup|header|footer|figcaption|li|dd|dt)[^\w])/gm, '$2');
if (html.indexOf('<!--more-->') > 0) {
var parts = html.split(/\s*<\!\-\-more\-\->\s*/),
summary = parts.shift(),
details = parts.join('');
html = '<div class="summary">' + summary + '</div>'
+ '<div class="details">' + details + '</div>';
}
var diffs = diffMatch.diff_main(last, html);
last = html;
if (diffs.length > 0) {
var stack = [], markStr = mark;
for (var i = 0; i < diffs.length; i ++) {
var diff = diffs[i], op = diff[0], str = diff[1]
sp = str.lastIndexOf('<'), ep = str.lastIndexOf('>');
if (op != 0) {
if (sp >=0 && sp > ep) {
if (op > 0) {
stack.push(str.substring(0, sp) + markStr + str.substring(sp));
} else {
var lastStr = stack[stack.length - 1], lastSp = lastStr.lastIndexOf('<');
stack[stack.length - 1] = lastStr.substring(0, lastSp) + markStr + lastStr.substring(lastSp);
}
} else {
if (op > 0) {
stack.push(str + markStr);
} else {
stack.push(markStr);
}
}
markStr = '';
} else {
stack.push(str);
}
}
html = stack.join('');
if (!markStr) {
var pos = html.indexOf(mark), prev = html.substring(0, pos),
next = html.substr(pos + mark.length),
sp = prev.lastIndexOf('<'), ep = prev.lastIndexOf('>');
if (sp >= 0 && sp > ep) {
html = prev.substring(0, sp) + span + prev.substring(sp) + next;
} else {
html = prev + span + next;
}
}
}
return html;
});
editor.hooks.chain('onPreviewRefresh', function () {
var diff = $('.diff', preview), scrolled = false;
$('img', preview).load(function () {
if (scrolled) {
preview.scrollTo(diff, {
offset : - 50
});
}
});
if (diff.length > 0) {
var p = diff.position(), lh = diff.parent().css('line-height');
lh = !!lh ? parseInt(lh) : 0;
if (p.top < 0 || p.top > preview.height() - lh) {
preview.scrollTo(diff, {
offset : - 50
});
scrolled = true;
}
}
});
var input = $('#text'), th = textarea.height(), ph = preview.height();
editor.hooks.chain('enterFakeFullScreen', function () {
th = textarea.height();
ph = preview.height();
$(document.body).addClass('fullscreen');
var h = $(window).height() - toolbar.outerHeight();
textarea.css('height', h);
preview.css('height', h);
});
editor.hooks.chain('enterFullScreen', function () {
$(document.body).addClass('fullscreen');
var h = window.screen.height - toolbar.outerHeight();
textarea.css('height', h);
preview.css('height', h);
});
editor.hooks.chain('exitFullScreen', function () {
$(document.body).removeClass('fullscreen');
textarea.height(th);
preview.height(ph);
});
editor.run();
// 编辑预览切换
var edittab = $('#md-button-bar').prepend('<div class="md-edittab"><a href="#md-editarea" class="active">撰写</a><a href="#md-preview">预览</a></div>'),
editarea = $(textarea.parent()).attr("id", "md-editarea");
$(".md-edittab a").click(function() {
$(".md-edittab a").removeClass('active');
$(this).addClass("active");
$("#md-editarea, #md-preview").addClass("md-hidetab");
var selected_tab = $(this).attr("href"),
selected_el = $(selected_tab).removeClass("md-hidetab");
// 预览时隐藏编辑器按钮
if (selected_tab == "#md-preview") {
$("#md-button-row").addClass("md-visualhide");
} else {
$("#md-button-row").removeClass("md-visualhide");
}
// 预览和编辑窗口高度一致
$("#md-preview").outerHeight($("#md-editarea").innerHeight());
return false;
});
});
</script>
</body>
</html>