-
Notifications
You must be signed in to change notification settings - Fork 23
/
markdown-preview-mode.el
431 lines (375 loc) · 16.8 KB
/
markdown-preview-mode.el
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
;;; markdown-preview-mode.el --- markdown realtime preview minor mode. -*- lexical-binding: t -*-
;; Copyright (C) 2014 <[email protected]>
;; Author: Igor Shymko <[email protected]>
;; URL: https://github.com/ancane/markdown-preview-mode
;; Keywords: markdown, gfm, convenience
;; Version: 0.9.4
;; Package-Requires: ((emacs "24.4") (websocket "1.6") (markdown-mode "2.0") (cl-lib "0.5") (web-server "0.1.1") )
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This package makes use of websockets to deliver rendered markdown to a web browser.
;; Updates happen upon buffer save or on idle.
;;
;;; Code:
(require 'cl-lib)
(require 'websocket)
(require 'markdown-mode)
(require 'web-server)
(defgroup markdown-preview nil
"Markdown preview mode."
:group 'text
:prefix "markdown-preview-"
:link '(url-link "https://github.com/ancane/markdown-preview-mode"))
(defcustom markdown-preview-host "localhost"
"Markdown preview websocket server address."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-ws-port 7379
"Markdown preview websocket server port."
:group 'markdown-preview
:type 'integer)
(defcustom markdown-preview-http-host "localhost"
"Markdown preview http server address."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-http-port 9000
"Markdown preview http server port."
:group 'markdown-preview
:type 'integer)
(defcustom markdown-preview-style nil
"Deprecated. Use `markdown-preview-stylesheets'."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-file-name ".markdown-preview.html"
"Markdown preview file name."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-auto-open 'http
"Markdown preview websocket server address."
:group 'markdown-preview
:type '(choice (const :tag "As local file" file)
(const :tag "Via http" http)
(const :tag "Off" nil)))
(defcustom markdown-preview-delay-time 2.0
"Refresh preview after this certain of time."
:group 'markdown-preview
:type 'float)
(defcustom markdown-preview-script-oninit ""
"Markdown preview javascript which runs on init."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-script-onupdate ""
"Markdown preview javascript which runs on update preview."
:group 'markdown-preview
:type 'string)
(defvar markdown-preview-javascript '()
"List of client javascript libs for preview.")
(defvar markdown-preview-stylesheets
(list "https://thomasf.github.io/solarized-css/solarized-dark.min.css")
"List of client stylesheets for preview.")
(defvar markdown-preview--websocket-server nil
"`markdown-preview' Websocket server.")
(defvar markdown-preview--http-server nil
"`markdown-preview' http server.")
(defvar markdown-preview--local-client nil
"`markdown-preview' local client.")
(defvar markdown-preview--remote-clients (make-hash-table :test 'equal)
"Remote clients hashtable. UUID -> WS.")
(defvar markdown-preview--home-dir (file-name-directory load-file-name)
"`markdown-preview-mode' home directory.")
(defvar markdown-preview--preview-template
(expand-file-name "preview.html" markdown-preview--home-dir)
"`markdown-preview-mode' html preview template.")
(defvar markdown-preview--idle-timer nil
"Preview idle timer.")
(defvar markdown-preview--uuid nil
"Unique preview identifier.")
(defvar markdown-preview--preview-buffers (make-hash-table :test 'equal)
"Preview buffers hashtable. UUID -> buffer name.")
(defun markdown-preview--stop-idle-timer ()
"Stop the `markdown-preview' idle timer."
(when (timerp markdown-preview--idle-timer)
(cancel-timer markdown-preview--idle-timer)))
(defun markdown-preview--css ()
"Get list of styles for preview in backward compatible way."
(let* ((custom-style (list markdown-preview-style))
(all-styles
(mapc (lambda (x) (add-to-list 'custom-style x t)) markdown-preview-stylesheets)))
(mapconcat
(lambda (x)
(if (string-match-p "^[\n\t ]*<style" x)
x
(concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"" x "\">")))
all-styles
"\n")))
(defun markdown-preview--scripts ()
"Get list of javascript script tags for preview."
(mapconcat
(lambda (x)
(if (string-match-p "^[\n\t ]*<script" x)
x
(concat
"<script src=\"" (if (consp x) (car x) x) "\""
(if (consp x) (format " %s" (cdr x)))
"></script>")))
markdown-preview-javascript
"\n"))
(defun markdown-preview--read-preview-template (preview-uuid preview-file)
"Read preview template and writes identified by PREVIEW-UUID rendered copy to PREVIEW-FILE, ready to be open in browser."
(with-temp-file preview-file
(insert-file-contents markdown-preview--preview-template)
(when (search-forward "${MD_STYLE}" nil t)
(replace-match (markdown-preview--css) t))
(when (search-forward "${MD_JS}" nil t)
(replace-match (markdown-preview--scripts) t))
(when (search-forward "${MD_JS_ONINIT}" nil t)
(replace-match markdown-preview-script-oninit t))
(when (search-forward "${WS_HOST}" nil t)
(replace-match markdown-preview-host t))
(when (search-forward "${WS_PORT}" nil t)
(replace-match (format "%s" markdown-preview-ws-port) t))
(when (search-forward "${MD_UUID}" nil t)
(replace-match (format "%s" preview-uuid) t))
(when (search-forward "${MD_JS_ONUPDATE}" nil t)
(replace-match markdown-preview-script-onupdate t))
(buffer-string)))
;; Emacs 26 async network workaround
(defun markdown-preview--fix-network-process-wait (plist)
"Ensure PLIST contain :nowait nil."
(if (and (>= emacs-major-version 26)
(equal (plist-get plist :name) "ws-server")
(plist-get plist :server)
(plist-get plist :nowait))
(plist-put plist :nowait nil)
plist))
(defun markdown-preview--start-http-server (port)
"Start http server at PORT to serve preview file via http."
(unless markdown-preview--http-server
(let ((docroot default-directory))
(advice-add 'make-network-process :filter-args #'markdown-preview--fix-network-process-wait)
(setq markdown-preview--http-server
(ws-start
(lambda (request)
(with-slots (process headers) request
(let* ((path (substring (cdr (assoc :GET headers)) 1))
(filename (expand-file-name path docroot)))
(if (string= path "")
(progn
(ws-send-file
process
(expand-file-name
markdown-preview-file-name
(with-current-buffer
(gethash (markdown-preview--parse-uuid headers)
markdown-preview--preview-buffers)
default-directory
))))
(if (string= path "favicon.ico")
(ws-send-file process (expand-file-name path markdown-preview--home-dir))
(if (and (not (file-directory-p filename)) (file-exists-p filename))
(ws-send-file process filename)
(ws-send-404 process)
))))))
markdown-preview-http-port nil :host markdown-preview-http-host))
(advice-remove 'make-network-process #'markdown-preview--fix-network-process-wait))))
(defun markdown-preview--parse-uuid (headers)
"Find uuid query param in HEADERS."
(let ((found (cl-find-if (lambda (x)
(when (stringp (car x))
(equal "uuid" (format "%s" (car x)))))
headers)))
(when found (cdr found))))
(defun markdown-preview--open-browser-preview ()
"Open the markdown preview in the browser."
(when (eq markdown-preview-auto-open 'file)
(browse-url (expand-file-name markdown-preview-file-name default-directory)))
(when (eq markdown-preview-auto-open 'http)
(browse-url
(format "http://localhost:%d/?uuid=%s" markdown-preview-http-port markdown-preview--uuid)))
(unless markdown-preview-auto-open
(message
(format
"Preview address: http://0.0.0.0:%d/?uuid=%s"
markdown-preview-http-port
markdown-preview--uuid))))
(defun markdown-preview--stop-websocket-server ()
"Stop the `markdown-preview' websocket server."
(clrhash markdown-preview--preview-buffers)
(when markdown-preview--local-client
(websocket-close markdown-preview--local-client))
(when markdown-preview--websocket-server
(delete-process markdown-preview--websocket-server)
(setq markdown-preview--websocket-server nil)
(clrhash markdown-preview--remote-clients)))
(defun markdown-preview--stop-http-server ()
"Stop the `markdown-preview' http server."
(when markdown-preview--http-server
(ws-stop markdown-preview--http-server)
(setq markdown-preview--http-server nil)))
(defun markdown-preview--drop-closed-clients ()
"Clean closed clients in `markdown-preview--remote-clients' list."
(maphash (lambda (ws-uuid websocket)
(unless (websocket-openp websocket)
(remhash ws-uuid markdown-preview--remote-clients)))
markdown-preview--remote-clients))
(defun markdown-preview--start-websocket-server ()
"Start `markdown-preview' websocket server."
(when (not markdown-preview--websocket-server)
(setq markdown-preview--websocket-server
(websocket-server
markdown-preview-ws-port
:host markdown-preview-host
:on-message (lambda (websocket frame)
(let ((ws-frame-text (websocket-frame-payload frame)))
(if (and
(stringp ws-frame-text)
(string-prefix-p "MDPM-Register-UUID: " ws-frame-text))
(let ((ws-uuid (substring ws-frame-text 20)))
(puthash ws-uuid websocket markdown-preview--remote-clients)
(markdown-preview--send-preview-to websocket ws-uuid))
(progn
(websocket-send
(gethash markdown-preview--uuid markdown-preview--remote-clients)
frame))
)))
:on-open (lambda (websocket) (message "Websocket opened"))
:on-error (lambda (websocket type err) (message (format "====> Error: %s" err)))
:on-close (lambda (websocket) (markdown-preview--drop-closed-clients))))
(add-hook 'kill-emacs-hook 'markdown-preview--stop-websocket-server))
(markdown-preview--open-browser-preview))
(defun markdown-preview--start-local-client ()
"Start the `markdown-preview' local client."
(when (not markdown-preview--local-client)
(setq markdown-preview--local-client
(websocket-open
(format "ws://%s:%d" markdown-preview-host markdown-preview-ws-port)
:on-error (lambda (ws type err)
(message "error connecting"))
:on-close (lambda (websocket)
(setq markdown-preview--local-client nil))))))
(defun markdown-preview--send-preview (preview-uuid)
"Send the `markdown-preview' with PREVIEW-UUID preview to clients."
(when (bound-and-true-p markdown-preview-mode)
(markdown-preview--send-preview-to markdown-preview--local-client preview-uuid)))
(defun markdown-preview--send-preview-to (websocket preview-uuid)
"Send the `markdown-preview' with PREVIEW-UUID to a specific WEBSOCKET."
(let ((mark-position-percent
(number-to-string
(truncate
(* 100
(/
(float (- (line-number-at-pos) (/ (count-screen-lines (window-start) (point)) 2)))
(count-lines (point-min) (point-max))))))))
(let ((md-buffer (gethash preview-uuid markdown-preview--preview-buffers)))
(when md-buffer
(with-current-buffer md-buffer
(markdown markdown-output-buffer-name))))
(with-current-buffer markdown-output-buffer-name ;; get-buffer
(websocket-send-text websocket
(concat
"<div>"
"<span id='position-percentage'>"
mark-position-percent
"</span>"
"<div id='content'>"
(buffer-substring-no-properties (point-min) (point-max))
"</div>"
"</div>")
))))
(defun markdown-preview--start ()
"Start `markdown-preview' mode."
(setq-local markdown-preview--uuid (markdown-preview--random-uuid))
(puthash markdown-preview--uuid (buffer-name) markdown-preview--preview-buffers)
;; (gethash markdown-preview--uuid markdown-preview--preview-buffers)
(markdown-preview--read-preview-template
markdown-preview--uuid
(expand-file-name markdown-preview-file-name default-directory))
(markdown-preview--start-websocket-server)
(markdown-preview--start-local-client)
(markdown-preview--start-http-server markdown-preview-http-port)
(setq markdown-preview--idle-timer
(run-with-idle-timer markdown-preview-delay-time t
(lambda ()
(when markdown-preview--uuid
(markdown-preview--send-preview markdown-preview--uuid)))))
(add-hook 'after-save-hook (lambda ()
(when markdown-preview--uuid
(markdown-preview--send-preview markdown-preview--uuid))) nil t))
(defun markdown-preview--stop ()
"Stop `markdown-preview' mode."
(remove-hook 'after-save-hook 'markdown-preview--send-preview t)
(markdown-preview--stop-idle-timer)
(remhash markdown-preview--uuid markdown-preview--preview-buffers)
(let* ((preview-file-dir (if (buffer-file-name)
(file-name-directory (buffer-file-name))
default-directory))
(preview-file (concat preview-file-dir markdown-preview-file-name)))
(if (file-exists-p preview-file)
(delete-file preview-file))))
(defun markdown-preview--random-uuid ()
"Insert a UUID using a simple hashing of variable data.
Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d
Note: this code uses https://en.wikipedia.org/wiki/Md5,
which is not cryptographically safe. I'm not sure what's
the implication of its use here.
Version 2015-01-30
URL `http://ergoemacs.org/emacs/elisp_generate_uuid.html'"
;; by Christopher Wellons, 2011-11-18. Editted by Xah Lee.
;; Edited by Hideki Saito further to generate all valid variants for "N" in xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx format.
;; (interactive)
(let ((myStr (md5 (format "%s%s%s%s%s%s%s%s%s%s"
(user-uid)
(emacs-pid)
(system-name)
(user-full-name)
(current-time)
(emacs-uptime)
(garbage-collect)
(buffer-string)
(random)
(recent-keys)))))
(format "%s-%s-4%s-%s%s-%s"
(substring myStr 0 8)
(substring myStr 8 12)
(substring myStr 13 16)
(format "%x" (+ 8 (random 4)))
(substring myStr 17 20)
(substring myStr 20 32))))
;;;###autoload
(defun markdown-preview-open-browser ()
"Open the `markdown-preview' in the browser."
(interactive)
(markdown-preview--open-browser-preview))
;;;###autoload
(defun markdown-preview-cleanup ()
"Cleanup `markdown-preview' mode."
(interactive)
(markdown-preview--stop-websocket-server)
(markdown-preview--stop-http-server))
;;;###autoload
(define-minor-mode markdown-preview-mode
"Markdown preview mode."
:group 'markdown-preview
:init-value nil
(when (not (or
(equal major-mode 'markdown-mode)
(equal major-mode 'gfm-mode)))
(markdown-mode))
(if markdown-preview-mode
(markdown-preview--start)
(markdown-preview--stop)))
(provide 'markdown-preview-mode)
;;; markdown-preview-mode.el ends here