Add Markdown prettifier functions

* README.org (Default prettifier): New section.
* eldoc-box.el (eldoc-box-buffer-hook): Add prettifier functions.
(eldoc-box--prettify-markdown-separator)
(eldoc-box--replace-en-space)
(eldoc-box--condense-large-newline-gaps)
(eldoc-box--remove-linked-images)
(eldoc-box--fontify-html-header): New functions.
This commit is contained in:
Yuan Fu 2023-03-07 13:52:42 -08:00
parent 5949978bdc
commit 1b5eab4d67
No known key found for this signature in database
GPG Key ID: 56E19BC57664A442
4 changed files with 95 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#+TITLE: ElDoc box #+TITLE: ElDoc box
This package displays ElDoc documentations in a childframe.
This package displays ElDoc documentations in a childframe. The childfrme is selectable and scrollable with mouse, even thought the cursor is hidden.
[[https://melpa.org/#/eldoc-box][file:https://melpa.org/packages/eldoc-box-badge.svg]] [[https://melpa.org/#/eldoc-box][file:https://melpa.org/packages/eldoc-box-badge.svg]]
[[https://stable.melpa.org/#/eldoc-box][file:https://stable.melpa.org/packages/eldoc-box-badge.svg]] [[https://stable.melpa.org/#/eldoc-box][file:https://stable.melpa.org/packages/eldoc-box-badge.svg]]
@ -46,6 +47,18 @@ It is also available on [[https://melpa.org/#/eldoc-box][MELPA]].
To keep eldoc from displaying documentation at point without enabling any minor mode above: =(add-to-list 'eglot-ignored-server-capabilites :hoverProvider)=. To keep eldoc from displaying documentation at point without enabling any minor mode above: =(add-to-list 'eglot-ignored-server-capabilites :hoverProvider)=.
** Default prettifier
By default, eldoc-box tries to prettify the displayed markdown documentation as shown below. If you wish to disable them, remove the prettifier functions from =eldoc-box-buffer-hook=. Report an issue if there are other things can be prettfied away.
Before:
[[./before.png]]
After:
[[./after.png]]
* Credit * Credit
- Thanks to for [[https://github.com/joaotavora][João Távora]] for valuable contribution and explaining eldoc and eglot internals to me. - Thanks to for [[https://github.com/joaotavora][João Távora]] for valuable contribution and explaining eldoc and eglot internals to me.

BIN
after.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

BIN
before.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

View File

@ -161,9 +161,14 @@ It will be passes with two arguments: WIDTH and HEIGHT of the childframe.")
"T means fringe's background color is set to as same as that of default." "T means fringe's background color is set to as same as that of default."
:type 'boolean) :type 'boolean)
(defvar eldoc-box-buffer-hook nil (defvar eldoc-box-buffer-hook '(eldoc-box--prettify-markdown-separator
eldoc-box--replace-en-space
eldoc-box--remove-linked-images
eldoc-box--fontify-html-header
eldoc-box--condense-large-newline-gaps)
"Hook run after buffer for doc is setup. "Hook run after buffer for doc is setup.
Run inside the new buffer.") Run inside the new buffer. By default, it contains some Markdown
prettifiers, which see.")
(defvar eldoc-box-frame-hook nil (defvar eldoc-box-frame-hook nil
"Hook run after doc frame is setup but just before it is made visible. "Hook run after doc frame is setup but just before it is made visible.
@ -608,6 +613,80 @@ You can use \[keyboard-quit] to hide the doc."
(or (line-number-display-width t) 0)) (or (line-number-display-width t) 0))
nil)) nil))
;;;; Markdown compatibility
(defun eldoc-box--prettify-markdown-separator ()
"Prettify the markdown separator in doc returned by Eglot.
Refontify the separator so they span exactly the width of the
childframe."
(save-excursion
(goto-char (point-min))
(let (prop)
(while (setq prop (text-property-search-forward 'markdown-hr))
(add-text-properties (prop-match-beginning prop)
(prop-match-end prop)
'( display (space :width text)
face ( :strike-through t
:height 0.4)))))))
(defun eldoc-box--replace-en-space ()
"Replace the en spaces in documentation with regular spaces."
(save-excursion
(goto-char (point-min))
(while (search-forward "" nil t)
(replace-match " "))))
(defun eldoc-box--condense-large-newline-gaps ()
"Condense exceedingly large gaps made of consecutive newlines.
These gaps are usually made of hidden \"```\" and/or consecutive
newlines. Replace those gaps with a single empty line at 0.5 line
height."
(save-excursion
(goto-char (point-min))
(while (re-search-forward
(rx (>= 2 (or "\n" (seq "```" (+ (syntax word))) "<br>"
(seq bol (+ (or " " "\t" "")) eol))))
nil t)
(if (or (eq (match-beginning 0) (point-min))
(eq (match-end 0) (point-max)))
(replace-match "")
(replace-match "\n\n")
(add-text-properties (1- (point)) (point)
'( font-lock-face (:height 0.4)
face (:height 0.4)))))))
(defun eldoc-box--remove-linked-images ()
"Some documentation embed image links in the doc...remove them."
(save-excursion
(goto-char (point-min))
;; Find every Markdown image link, and remove them.
(while (re-search-forward
(rx "[" (seq "![" (+? anychar) "](" (+? anychar) ")") "]"
"(" (+? anychar) ")")
nil t)
(replace-match ""))))
(defun eldoc-box--fontify-html-header ()
"Fontify <h1> <h2> tags."
(save-excursion
(goto-char (point-min))
(while (re-search-forward
(rx bol
(group "<h" digit ">")
(group (*? anychar))
(group "</h" digit ">")
eol)
nil t)
(add-text-properties (match-beginning 2)
(match-end 2)
'( face (:weight bold)
font-lock-face (:weight bold)))
(put-text-property (match-beginning 1) (match-end 1)
'invisible t)
(put-text-property (match-beginning 3) (match-end 3)
'invisible t))))
(provide 'eldoc-box) (provide 'eldoc-box)
;;; eldoc-box.el ends here ;;; eldoc-box.el ends here