Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MDOPEN #31

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
<!-- markdown-toc end -->

Instant Github-flavored Markdown/Org preview using [Grip](https://github.com/joeyespo/grip)
(GitHub Readme Instant Preview).
(GitHub Readme Instant Preview) or [mdopen](https://github.com/immanelg/mdopen).

## Prerequisite

- [Python](https://www.python.org/)
- [Grip](https://github.com/joeyespo/grip): `pip install grip`

### Alternative markdown preview without accessing GitHub API
- [mdopen](https://github.com/immanelg/mdopen): `cargo install mdopen`

## Install

### Manual
Expand All @@ -51,15 +54,19 @@ From melpa, `M-x package-install RET grip-mode RET`.
;; Use keybindings
(use-package grip-mode
:ensure t
:config (setq grip-use-mdopen t) ;; to use `mdopen` instead of `grip`
:bind (:map markdown-mode-command-map
("g" . grip-mode)))

;; Or using hooks
(use-package grip-mode
:ensure t
:config (setq grip-use-mdopen t) ;; to use `mdopen` instead of `grip`
:hook ((markdown-mode org-mode) . grip-mode))
```

⚠️ NOTE: `mdopen` opens `markdown` preview in default browser, and doesn't support emacs webkit preview. ⚠️

Run `M-x grip-mode` to preview the markdown and org buffers in the embedded
webkit browser if Emacs supports (built with `--with-xwidgets`), or in the
default browser (Chrome, Firefox, etc.).
Expand Down
73 changes: 45 additions & 28 deletions grip-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,55 @@ Use default browser unless `xwidget' is available."
"Return grip preview url."
(format "http://%s:%d" grip-preview-host grip--port))

(defcustom grip-mdopen-path "mdopen"
"Path to the mdopen binary."
:type 'file
:group 'grip)

(defcustom grip-use-mdopen nil
"Use mdopen instead of grip if non-nil."
:type 'boolean
:group 'grip)

(defun grip-start-process ()
"Render and preview with grip."
"Render and preview with grip or mdopen."
(unless (processp grip--process)
(unless (executable-find grip-binary-path)
(grip-mode -1) ; Force to disable
(user-error "The `grip' is not available in PATH environment"))

;; Generat random port
(while (< grip--port 6419)
(setq grip--port (random 65535)))

;; Start a new grip process
(when grip--preview-file
(setq grip--process
(start-process (format "grip-%d" grip--port)
(format " *grip-%d*" grip--port)
grip-binary-path
(format "--api-url=%s" grip-github-api-url)
(format "--user=%s" grip-github-user)
(format "--pass=%s" grip-github-password)
(format "--title=%s - Grip" (buffer-name))
grip--preview-file
(number-to-string grip--port)))

(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
(sleep-for grip-sleep-time) ; Ensure the server has started
(grip--browse-url (grip--preview-url)))))
(if grip-use-mdopen
(progn
(unless (executable-find grip-mdopen-path)
(grip-mode -1) ; Force to disable
(user-error "The `mdopen' is not available in PATH environment"))
(when buffer-file-name
(setq grip--process
(start-process "mdopen" "*mdopen*"
grip-mdopen-path
buffer-file-name))
(message "Preview `%s' with mdopen" buffer-file-name)))
(progn
(unless (and grip-binary-path (executable-find grip-binary-path))
(grip-mode -1) ; Force to disable
(user-error "The `grip' is not available in PATH environment"))
;; Generate random port
(while (< grip--port 6419)
(setq grip--port (random 65535)))
;; Start a new grip process
(when grip--preview-file
(setq grip--process
(start-process (format "grip-%d" grip--port)
(format " *grip-%d*" grip--port)
grip-binary-path
(format "--api-url=%s" grip-github-api-url)
(format "--user=%s" grip-github-user)
(format "--pass=%s" grip-github-password)
(format "--title=%s - Grip" (buffer-name))
grip--preview-file
(number-to-string grip--port)))
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
(sleep-for grip-sleep-time)
(grip--browse-url (grip--preview-url)))))))

(defun grip--kill-process ()
"Kill grip process."
"Kill grip or mdopen process."
(when grip--process
;; Delete xwidget buffer
(when (and grip-preview-use-webkit
Expand All @@ -190,13 +209,11 @@ Use default browser unless `xwidget' is available."
(buf (xwidget-buffer (xwidget-webkit-current-session))))
(when (buffer-live-p buf)
(kill-buffer buf))))

;; Delete process
(delete-process grip--process)
(message "Process `%s' killed" grip--process)
(setq grip--process nil)
(setq grip--port 6418)

;; Delete preview temporary file
(when (and grip--preview-file
(not (string-equal grip--preview-file buffer-file-name)))
Expand Down