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

Add support for go-grip #37

Merged
merged 1 commit into from
Feb 10, 2025
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- markdown-toc end -->

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

## Prerequisite

Expand All @@ -32,6 +32,7 @@ Instant Github-flavored Markdown/Org preview using [Grip](https://github.com/joe

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

## Install

Expand Down Expand Up @@ -61,12 +62,14 @@ From melpa, `M-x package-install RET grip-mode RET`.
;; Or using hooks
(use-package grip-mode
:ensure t
:config (setq grip-use-mdopen t) ;; to use `mdopen` instead of `grip`
:config (setq grip-use-gogrip t) ;; to use `go-grip` 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. ⚠️

⚠️ NOTE: `go-grips` runs locally, does not use the github API, and supports 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
113 changes: 78 additions & 35 deletions grip-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@
:type 'boolean
:group 'grip)

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

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

(defcustom grip-gogrip-theme "auto"
"Theme choice for go-grip."
:type '(choice
(const :tag "Automatic" "auto")
(const :tag "Dark" "dark")
(const :tag "Light" "light"))
:group 'grip)

(defcustom grip-preview-use-webkit t
"Use embedded webkit to preview.

Expand Down Expand Up @@ -159,47 +177,72 @@ Use default browser unless `xwidget' is available."

(defun grip--preview-url ()
"Return grip preview url."
(format "http://%s:%d" grip-preview-host grip--port))
(if grip-use-gogrip
(format "http://%s:%d/%s" grip-preview-host grip--port
(file-name-nondirectory grip--preview-file))
(format "http://%s:%d" grip-preview-host grip--port)))

(defun grip-start-process ()
"Render and preview with grip or mdopen."
(unless (processp grip--process)
(if grip-use-mdopen
(progn
(unless (and grip-mdopen-path (executable-find grip-mdopen-path))
(grip-mode -1) ; Force to disable
(user-error "The `mdopen' is not available in PATH environment"))
(when grip--preview-file
(setq grip--process
(start-process "mdopen" "*mdopen*"
grip-mdopen-path
grip--preview-file))
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))))
(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)))))))
(cond (grip-use-mdopen
(progn
(unless (and grip-mdopen-path (executable-find grip-mdopen-path))
(grip-mode -1) ; Force to disable
(user-error "The `mdopen' is not available in PATH environment"))
(when grip--preview-file
(setq grip--process
(start-process "mdopen" "*mdopen*"
grip-mdopen-path
grip--preview-file))
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url)))))
(grip-use-gogrip
(progn
(unless (and grip-gogrip-path (executable-find grip-gogrip-path))
(grip-mode -1) ; Force to disable
(user-error "The `go-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-gogrip-path
(format "--port=%d" grip--port)
(format "--theme=%s" grip-gogrip-theme)
"--browser=false"
grip--preview-file))
(message "Preview `%s' on %s" buffer-file-name (grip--preview-url))
(sleep-for grip-sleep-time)
(grip--browse-url (grip--preview-url)))))
(t
(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 or mdopen process."
"Kill grip or mdopen or go-grip process."
(when grip--process
;; Delete xwidget buffer
(when (and grip-preview-use-webkit
Expand Down