Skip to content

Provide filter-buffer-substring-function in gfm-view-mode #493

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
- Support list of strings of `markdown-command`
- Apply `markdown-translate-filename-function` for `markdown-display-inline-images`
([GH-422][])
- Buffer substring filter function which filter out codeblock markers
is provided by the `gfm-view-mode`

* Bug fixes:

Expand Down
11 changes: 11 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -9517,11 +9517,22 @@ rows and columns and the column alignment."
markdown-view-mode-map
"Keymap for `gfm-view-mode'.")

(defun gfm-view-mode-filter-function (start end &optional delete)
"Remove code block markers from buffer substring between START and END.
If DELETE is non-nil, delete the text between START and END from the buffer.
This function is used as value of the `filter-buffer-substring-function'."
(prog1 (replace-regexp-in-string "^[[:blank:]]*```.*\n*" ""
(buffer-substring start end))
(when delete
(let ((inhibit-read-only t))
(delete-region start end)))))

;;;###autoload
(define-derived-mode gfm-view-mode gfm-mode "GFM-View"
"Major mode for viewing GitHub Flavored Markdown content."
(setq-local markdown-hide-markup markdown-hide-markup-in-view-modes)
(setq-local markdown-fontify-code-blocks-natively t)
(setq-local filter-buffer-substring-function #'gfm-view-mode-filter-function)
(add-to-invisibility-spec 'markdown-markup)
(read-only-mode 1))

Expand Down
13 changes: 13 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
`(markdown-test-file-mode 'gfm-mode ,file ,@body))
(def-edebug-spec markdown-test-file-gfm (form body))

(defmacro markdown-test-file-gfm-view (file &rest body)
"Open FILE from `markdown-test-dir' in `gfm-view-mode' and execute BODY."
(declare (indent 1))
`(markdown-test-file-mode 'gfm-view-mode ,file ,@body))
(def-edebug-spec markdown-test-file-gfm-view (form body))

(defmacro markdown-test-temp-file (file &rest body)
"Open FILE from `markdown-test-dir' visiting temp file and execute BODY.
This file is not saved."
Expand Down Expand Up @@ -2102,6 +2108,13 @@ See GH-245."
(should (invisible-p 154))
(should (invisible-p 156)))))

(ert-deftest test-markdown-markup-hiding/gfm-filter-buffer-string ()
"Test code block markers filtered out when using `filter-buffer-substring'."
(let ((markdown-hide-markuo t))
(markdown-test-file-gfm-view "GFM.md"
(should (not (string-match markdown-regex-gfm-code-block-open
(filter-buffer-substring (point-min) (point-max))))))))

;;; Font lock tests:

(ert-deftest test-markdown-font-lock/italics-1 ()
Expand Down