13 Commits
v0.8 ... v0.9.2

Author SHA1 Message Date
Igor Shymko
f98d9114ca fix #32 drop dep on uuidgen; replace with uuid function 2018-12-13 15:39:40 +02:00
Igor Shymko
cba12b7776 version bump 2018-09-29 10:25:53 +03:00
Igor Shymko
180350da77 Merge pull request #35 from juergenhoetzel/fix-emacs-26-network-server-process
Add workaround for Emacs 26 async network-server-process issues
2018-09-29 10:15:54 +03:00
Juergen Hoetzel
363692d79a Add workaround for Emacs 26 async network-server-process issues
Fixes #33
Fixes #30
2018-09-24 16:41:47 +02:00
Igor Shymko
4ec15183fc Merge pull request #27 from zenkiezhu/inline-css-js-support
Add inline css/js support
2017-11-22 09:23:10 +02:00
Igor Shymko
d81f271d4c Merge pull request #26 from zenkiezhu/fix-issue#25
Fix issue#25
2017-11-22 09:12:00 +02:00
Zenkie Zhu
e93fd4e47e Add inline css/js support 2017-11-22 12:33:16 +08:00
Zenkie Zhu
7c58f2c18b Fix issue#25
https://github.com/ancane/markdown-preview-mode/issues/25
2017-11-21 23:27:55 +08:00
Igor Shymko
134fd33675 doc update; version bump 2017-08-20 14:59:49 +03:00
Igor Shymko
a7415a3d07 Merge pull request #24 from makotoshimazu/restrict_access_from_outside
Restrict accessing form another machine
2017-08-20 14:55:34 +03:00
Makoto Shimazu
3c38932da7 Restrict accessing form another machine 2017-08-18 12:27:04 +09:00
Igor Shymko
50e5baed5c Merge pull request #23 from adelatorrefoss/patch-1
Fix little typo
2017-04-26 23:01:42 +03:00
Antonio de la Torre Fernández
adde5adb58 Fix little typo 2017-04-26 15:25:01 +02:00
3 changed files with 73 additions and 21 deletions

View File

@@ -24,7 +24,7 @@ Markdown preview in emacs features:
## Run
* `markdown-preview-mode` - start mode and open preview window.
* `markdown-preview-open-browser` - open priview window for current buffer.
* `markdown-preview-open-browser` - open preview window for current buffer.
* `markdown-preview-cleanup` - cleanup running processes (close websocket and http servers).
## Customize
@@ -38,6 +38,8 @@ Markdown preview in emacs features:
## Remote access
* Customize `markdown-preview-host` to `0.0.0.0`.
* Customize `markdown-preview-http-host` to `0.0.0.0`.
* Set `markdown-preview-auto-open` to `nil` to disable window opening at remote emacs server.
* Start `markdown-preview-mode`. Http link for preview will be printed to `*Messages*` buffer. If not - run `markdown-preview-open-browser` to get the link printed.
* Setup 2 tunnels for `0.0.0.0:7379` and `0.0.0.0:9000` and then open preview link in local browser. Adjust tunnels according to your custom `ws-port` and `http-port` settings.
@@ -73,4 +75,3 @@ Markdown preview in emacs features:
* [markdown-mode.el](https://github.com/defunkt/markdown-mode)
* [websocket.el](https://github.com/ahyatt/emacs-websocket)
* [web-server.el](https://github.com/eschulte/emacs-web-server)
* [uuidgen](https://github.com/kanru/uuidgen-el)

View File

@@ -5,8 +5,8 @@
;; Author: Igor Shymko <igor.shimko@gmail.com>
;; URL: https://github.com/ancane/markdown-preview-mode
;; Keywords: markdown, gfm, convenience
;; Version: 0.8
;; Package-Requires: ((emacs "24.3") (websocket "1.6") (markdown-mode "2.0") (cl-lib "0.5") (web-server "0.1.1") (uuidgen "0.3"))
;; Version: 0.9.2
;; Package-Requires: ((emacs "24.3") (websocket "1.6") (markdown-mode "2.0") (cl-lib "0.5") (web-server "0.1.1") )
;; This file is not part of GNU Emacs.
@@ -30,11 +30,11 @@
;;
;;; Code:
(eval-when-compile (require 'cl))
(require 'cl-lib)
(require 'websocket)
(require 'markdown-mode)
(require 'web-server)
(require 'uuidgen)
(defgroup markdown-preview nil
"Markdown preview mode."
@@ -52,6 +52,11 @@
: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
@@ -91,7 +96,7 @@
"`markdown-preview' local client.")
(defvar markdown-preview--remote-clients (make-hash-table :test 'equal)
"Remote clients hashtable. UUID -> WS")
"Remote clients hashtable. UUID -> WS.")
(defvar markdown-preview--home-dir (file-name-directory load-file-name)
"`markdown-preview-mode' home directory.")
@@ -103,21 +108,23 @@
"Unique preview identifier.")
(defvar markdown-preview--preview-buffers (make-hash-table :test 'equal)
"Preview buffers hashtable. UUID -> buffer-name.")
"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-links ()
(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)
(concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"" x "\">"))
(if (string-match-p "^[\n\t ]*<style" x)
x
(concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"" x "\">")))
all-styles
"\n")))
@@ -125,20 +132,21 @@
"Get list of javascript script tags for preview."
(mapconcat
(lambda (x)
(concat
"<script src=\"" (if (consp x) (car x) x) "\""
(if (consp x) (format " %s" (cdr x)))
"></script>"))
(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."
"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 (expand-file-name "preview.html" markdown-preview--home-dir))
(when (search-forward "${MD_STYLE}" nil t)
(replace-match (markdown-preview--css-links) t))
(replace-match (markdown-preview--css) t))
(when (search-forward "${MD_JS}" nil t)
(replace-match (markdown-preview--scripts) t))
(when (search-forward "${WS_HOST}" nil t)
@@ -149,10 +157,21 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
(replace-match (format "%s" preview-uuid) 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
(lexical-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)
@@ -176,7 +195,8 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
(ws-send-file process filename)
(ws-send-404 process)
))))))
markdown-preview-http-port)))))
markdown-preview-http-port nil :host markdown-preview-http-host))
(advice-remove 'make-network-process #'my-filter))))
(defun markdown-preview--parse-uuid (headers)
"Find uuid query param in HEADERS."
@@ -262,12 +282,12 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
(setq markdown-preview--local-client nil))))))
(defun markdown-preview--send-preview (preview-uuid)
"Send the `markdown-preview' preview to clients."
"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' to a specific WEBSOCKET."
"Send the `markdown-preview' with PREVIEW-UUID to a specific WEBSOCKET."
(let ((mark-position-percent
(number-to-string
(truncate
@@ -297,7 +317,7 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
(defun markdown-preview--start ()
"Start `markdown-preview' mode."
(setq-local markdown-preview--uuid (uuidgen-1))
(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
@@ -323,6 +343,37 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
(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."

View File

@@ -23,7 +23,7 @@
console.log('Code: ' + event.code + ' reason: ' + event.reason);
};
socket.onmessage = function(event) {
$("#markdown-body").html($(event.data).find("#content").html());
$("#markdown-body").html($(event.data).find("#content").html()).trigger('mdContentChange');
var scroll = $(document).height() * ($(event.data).find("#position-percentage").html() / 100);
$("html, body").animate({ scrollTop: scroll }, 600);
};