Skip to content

Commit

Permalink
Add inline css/js support
Browse files Browse the repository at this point in the history
  • Loading branch information
uqix committed Nov 22, 2017
1 parent 134fd33 commit e93fd4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions markdown-preview-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,29 @@
(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")))

(defun markdown-preview--scripts ()
"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"))

Expand All @@ -143,7 +147,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)
Expand Down
2 changes: 1 addition & 1 deletion preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit e93fd4e

Please sign in to comment.