Skip to content

Commit

Permalink
Merge pull request #598 from jrblevin/pr-596
Browse files Browse the repository at this point in the history
Improve #596
  • Loading branch information
syohex authored Feb 10, 2021
2 parents 3ac7743 + ec15e37 commit eb42d39
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
`markdown-disable-tooltip-prompt`.
- Introduce `markdown-ordered-list-enumeration` variable [GH-587][]
- Search wiki link under project
- Add `markdown-insert-gfm-foldable-block` function [GH-596][]

* Improvements:
- Correct indirect buffer's indentation in `markdown-edit-code-block` [GH-375][]
Expand Down Expand Up @@ -65,6 +66,7 @@
[gh-584]: https://github.com/jrblevin/markdown-mode/issues/584
[gh-587]: https://github.com/jrblevin/markdown-mode/issues/587
[gh-590]: https://github.com/jrblevin/markdown-mode/pull/590
[gh-596]: https://github.com/jrblevin/markdown-mode/pull/596

# Markdown Mode 2.4

Expand Down
46 changes: 46 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -4435,6 +4435,49 @@ at the beginning of the block."
do (progn (when lang (markdown-gfm-add-used-language lang))
(goto-char (next-single-property-change (point) prop)))))))

(defun markdown-insert-foldable-block ()
"Insert details disclosure element to make content foldable.
If a region is active, wrap this region with the disclosure
element. More detais here 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details'."
(interactive)
(let ((details-open-tag "<details>")
(details-close-tag "</details>")
(summary-open-tag "<summary>")
(summary-close-tag " </summary>"))
(if (use-region-p)
(let* ((b (region-beginning))
(e (region-end))
(indent (progn (goto-char b) (current-indentation))))
(goto-char e)
;; if we're on a blank line, don't newline, otherwise the tags
;; should go on its own line
(unless (looking-back "\n" nil)
(newline))
(indent-to indent)
(insert details-close-tag)
(markdown-ensure-blank-line-after)
(goto-char b)
;; if we're on a blank line, insert the quotes here, otherwise
;; add a new line first
(unless (looking-at-p "\n")
(newline)
(forward-line -1))
(markdown-ensure-blank-line-before)
(indent-to indent)
(insert details-open-tag "\n")
(insert summary-open-tag summary-close-tag)
(search-backward summary-close-tag))
(let ((indent (current-indentation)))
(delete-horizontal-space :backward-only)
(markdown-ensure-blank-line-before)
(indent-to indent)
(insert details-open-tag "\n")
(insert summary-open-tag summary-close-tag "\n")
(insert details-close-tag)
(indent-to indent)
(markdown-ensure-blank-line-after)
(search-backward summary-close-tag)))))


;;; Footnotes =================================================================

Expand Down Expand Up @@ -5180,6 +5223,7 @@ Assumes match data is available for `markdown-regex-italic'."
(propertize "C = GFM code" 'face 'markdown-code-face) ", "
(propertize "pre" 'face 'markdown-pre-face) ", "
(propertize "footnote" 'face 'markdown-footnote-text-face) ", "
(propertize "F = foldable" 'face 'markdown-bold-face) ", "
(propertize "q = blockquote" 'face 'markdown-blockquote-face) ", "
(propertize "h & 1-6 = heading" 'face 'markdown-header-face) ", "
(propertize "- = hr" 'face 'markdown-hr-face) ", "
Expand Down Expand Up @@ -5213,6 +5257,7 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "c") 'markdown-insert-code)
(define-key map (kbd "C") 'markdown-insert-gfm-code-block)
(define-key map (kbd "f") 'markdown-insert-footnote)
(define-key map (kbd "F") 'markdown-insert-foldable-block)
(define-key map (kbd "h") 'markdown-insert-header-dwim)
(define-key map (kbd "H") 'markdown-insert-header-setext-dwim)
(define-key map (kbd "i") 'markdown-insert-italic)
Expand Down Expand Up @@ -5524,6 +5569,7 @@ See also `markdown-mode-map'.")
["GFM Code Block" markdown-insert-gfm-code-block]
["Edit Code Block" markdown-edit-code-block
:enable (markdown-code-block-at-point-p)]
["Foldable Block" markdown-insert-foldable-block]
"---"
["Blockquote Region" markdown-blockquote-region]
["Preformatted Region" markdown-pre-region]
Expand Down
15 changes: 15 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,21 @@ Detail: https://github.com/jrblevin/markdown-mode/issues"
(execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET RET RET RET"))
(should (string-equal (buffer-string) "[GNU](https://www.gnu.org/)")))))

(ert-deftest test-markdown-insertion/foldable-block ()
"Test `markdown-insert-foldable-block'."
(markdown-test-string ""
(call-interactively 'markdown-insert-foldable-block)
(should (string= (buffer-string) "<details>\n<summary> </summary>\n</details>"))
(should (looking-back "<summary>")))

(markdown-test-string "foo"
(push-mark (point) t t)
(end-of-line)
(should (use-region-p))
(call-interactively 'markdown-insert-foldable-block)
(should (string= (buffer-string) "<details>\n<summary> </summary>\nfoo\n</details>"))
(should (looking-back "<summary>"))))

;;; Footnote tests:

(ert-deftest test-markdown-footnote/basic-end ()
Expand Down

0 comments on commit eb42d39

Please sign in to comment.