Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cba12b7776 | ||
|
|
180350da77 | ||
|
|
363692d79a | ||
|
|
4ec15183fc | ||
|
|
d81f271d4c | ||
|
|
e93fd4e47e | ||
|
|
7c58f2c18b | ||
|
|
134fd33675 | ||
|
|
a7415a3d07 | ||
|
|
3c38932da7 | ||
|
|
50e5baed5c | ||
|
|
adde5adb58 |
@@ -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.
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
;; Author: Igor Shymko <igor.shimko@gmail.com>
|
||||
;; URL: https://github.com/ancane/markdown-preview-mode
|
||||
;; Keywords: markdown, gfm, convenience
|
||||
;; Version: 0.8
|
||||
;; Version: 0.9.1
|
||||
;; Package-Requires: ((emacs "24.3") (websocket "1.6") (markdown-mode "2.0") (cl-lib "0.5") (web-server "0.1.1") (uuidgen "0.3"))
|
||||
|
||||
;; This file is not part of GNU Emacs.
|
||||
@@ -30,6 +30,7 @@
|
||||
;;
|
||||
;;; Code:
|
||||
|
||||
(eval-when-compile (require 'cl))
|
||||
(require 'cl-lib)
|
||||
(require 'websocket)
|
||||
(require 'markdown-mode)
|
||||
@@ -52,6 +53,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
|
||||
@@ -110,14 +116,16 @@
|
||||
(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,10 +133,12 @@
|
||||
"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"))
|
||||
|
||||
@@ -138,7 +148,7 @@ 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 +159,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 contains :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 +197,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."
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user