forked from jbt/markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
268 lines (227 loc) · 7.67 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html>
<head>
<title>Markdown Editor</title>
<script src="marked.js"></script>
<script src="highlight.pack.js"></script>
<script src="codemirror/lib/codemirror.js"></script>
<script src="codemirror/xml/xml.js"></script>
<script src="codemirror/markdown/markdown.js"></script>
<script src="codemirror/gfm/gfm.js"></script>
<script src="codemirror/javascript/javascript.js"></script>
<script src="rawinflate.js"></script>
<script src="rawdeflate.js"></script>
<link rel="stylesheet" href="codemirror/lib/codemirror.css">
<link rel="stylesheet" href="default.css">
<style>
.CodeMirror pre{
line-height: 14px;
}
#in, .CodeMirror{
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 50%;
overflow: auto;
font-size: 12px;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}
.CodeMirror-scroll {
height: 100%;
min-height: 100%;
}
#out{
position: fixed;
top: 0;
right: 0;
left: 50%;
bottom: 0;
overflow: auto;
padding: 10px;
padding-left: 20px;
color: #444;
font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif;
font-size: 16px;
line-height: 1.5em
}
@media screen and (max-width: 1024px) {
#in {
display: none;
}
#out {
left: 0;
padding-left: 10px;
}
}
.view #in {
display: none;
}
.view #out {
left: 0;
padding-left: 10px;
}
a{ color: #0645ad; text-decoration:none;}
a:visited{ color: #0b0080; }
a:hover{ color: #06e; }
a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }
p{margin:1em 0;}
img{max-width:100%;}
h1,h2,h3,h4,h5,h6{font-weight:normal;color:#111;line-height:1em;}
h4,h5,h6{ font-weight: bold; }
h1{ font-size:2.5em; }
h2{ font-size:2em; border-bottom:1px solid silver; padding-bottom: 5px; }
h3{ font-size:1.5em; }
h4{ font-size:1.2em; }
h5{ font-size:1em; }
h6{ font-size:0.9em; }
blockquote{color:#666666;margin:0;padding-left: 3em;border-left: 0.5em #EEE solid;}
hr { display: block; height: 2px; border: 0; border-top: 1px solid #aaa;border-bottom: 1px solid #eee; margin: 1em 0; padding: 0; }
pre, code{
color: #000;
font-family: monospace;
font-size: 0.88em;
border-radius:3px;
background-color: #F8F8F8;
border: 1px solid #CCC;
}
pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; padding: 5px;}
pre code { border: 0px !important; background: transparent !important; line-height: 1.3em; }
code { padding: 0 3px 0 3px; }
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }
ul, ol { margin: 1em 0; padding: 0 0 0 2em; }
li p:last-child { margin:0 }
dd { margin: 0 0 0 2em; }
img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; }
table { border-collapse: collapse; border-spacing: 0; }
td, th { vertical-align: top; padding: 4px 10px; border: 1px solid #bbb; }
tr:nth-child(even) td, tr:nth-child(even) th { background: #eee; }
</style>
</head>
<body>
<div id="in"><form><textarea id="code"># (GitHub-Flavored) Markdown Editor
Basic useful feature list:
* Ctrl/Cmd + S to save the file
* Drag and drop a file into here to load it
* File contents are saved in the URL so you can share files
I'm no good at writing sample / filler text, so go write something yourself.
Look, a list!
* foo
* bar
* baz
And here's some code!
```javascript
$(function(){
$('div').html('I am a div.');
});
```
This is [on GitHub](https://github.com/jbt/markdown-editor) so let me know if I've b0rked it somewhere.
Props to Mr. Doob and his [code editor](http://mrdoob.com/projects/code-editor/), from which
the inspiration to this, and some handy implementation hints, came.
### Stuff used to make this:
* [marked](https://github.com/chjj) for Markdown parsing
* [CodeMirror](http://codemirror.net/) for the awesome syntax-highlighted editor
* [highlight.js](http://softwaremaniacs.org/soft/highlight/en/) for syntax highlighting in output code blocks
* [js-deflate](https://github.com/dankogai/js-deflate) for gzipping of data to make it fit in URLs
</textarea></form></div>
<div id="out"></div>
<script>
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob;
window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs;
// Because highlight.js is a bit awkward at times
var languageOverrides = {
js: 'javascript',
html: 'xml'
}
marked.setOptions({
highlight: function(code, lang){
if(languageOverrides[lang]) lang = languageOverrides[lang];
return hljs.LANGUAGES[lang] ? hljs.highlight(lang, code).value : code;
}
});
function update(e){
var val = e.getValue();
setOutput(val);
clearTimeout(hashto);
hashto = setTimeout(updateHash, 1000);
}
function setOutput(val){
val = val.replace(/<equation>((.*?\n)*?.*?)<\/equation>/ig, function(a, b){
return '<img src="http://latex.codecogs.com/png.latex?' + encodeURIComponent(b) + '" />';
});
document.getElementById('out').innerHTML = marked(val);
}
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
mode: 'gfm',
lineNumbers: true,
matchBrackets: true,
lineWrapping: true,
theme: 'default',
onChange: update
});
document.addEventListener('drop', function(e){
e.preventDefault();
e.stopPropagation();
var theFile = e.dataTransfer.files[0];
var theReader = new FileReader();
theReader.onload = function(e){
editor.setValue(e.target.result);
};
theReader.readAsText(theFile);
}, false);
function save(){
var code = editor.getValue();
var blob = new Blob([code], { type: 'text/plain' });
saveBlob(blob);
}
function saveBlob(blob){
var name = "untitled.md";
if(window.saveAs){
window.saveAs(blob, name);
}else if(navigator.saveBlob){
navigator.saveBlob(blob, name);
}else{
url = URL.createObjectURL(blob);
var link = document.createElement("a");
link.setAttribute("href",url);
link.setAttribute("download",name);
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
link.dispatchEvent(event);
}
}
document.addEventListener('keydown', function(e){
if(e.keyCode == 83 && (e.ctrlKey || e.metaKey)){
e.preventDefault();
save();
return false;
}
})
var hashto;
function updateHash(){
window.location.hash = btoa(RawDeflate.deflate(unescape(encodeURIComponent(editor.getValue()))))
}
if(window.location.hash){
var h = window.location.hash.replace(/^#/, '');
if(h.slice(0,5) == 'view:'){
setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5))))));
document.body.className = 'view';
}else{
editor.setValue(decodeURIComponent(escape(RawDeflate.inflate(atob(h)))))
update(editor);
editor.focus();
}
}else{
update(editor);
editor.focus();
}
var GoSquared = { acct: 'GSN-265185-D' };
</script>
<script src="//d1l6p2sc9645hc.cloudfront.net/tracker.js" async></script>
</body>
</html>