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

fix case when no language given for suggesting gfm code block langs #64

Closed
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
32 changes: 16 additions & 16 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1218,13 +1218,10 @@ Group 2 matches the key sequence.")

(defconst markdown-regex-gfm-code-block
(concat
"^\\s *\\(```\\)[ ]?\\([^[:space:]]+\\|{[^}]*}\\)?"
"^\\s *\\(```\\)[ ]*\\([^[:space:]]+\\|{[^}]*}\\)?"
"[[:space:]]*?\n"
"\\(\\(?:.\\|\n\\)*?\\)?"
;; the newline before the final line could have a ?, but then it gets mixed
;; up with `markdown-regex-code'. this way, there always needs to be at least
;; two newlines between the pair of triple backticks
"\n\\s *?\\(```\\)\\s *?$")
"\n?\\s *?\\(```\\)\\s *?$")
"Regular expression matching opening of GFM code blocks.
Group 1 matches the opening three backticks.
Group 2 matches the language identifier (optional).
Expand Down Expand Up @@ -3296,16 +3293,18 @@ the region boundaries are not on empty lines, these are added
automatically in order to have the correct markup."
(interactive
(list (let ((completion-ignore-case nil))
(markdown-clean-language-string
(completing-read
(format "Programming language [%s]: "
(or markdown-gfm-last-used-language "none"))
(markdown-gfm-get-corpus)
nil 'confirm nil
'markdown-gfm-language-history
(or markdown-gfm-last-used-language
(car markdown-gfm-additional-languages)))))))
(markdown-add-language-if-new lang)
(condition-case _err
(markdown-clean-language-string
(completing-read
(format "Programming language [%s]: "
(or markdown-gfm-last-used-language "none"))
(markdown-gfm-get-corpus)
nil 'confirm nil
'markdown-gfm-language-history
(or markdown-gfm-last-used-language
(car markdown-gfm-additional-languages))))
(quit "")))))
(unless (string= lang "") (markdown-add-language-if-new lang))
(when (> (length lang) 0) (setq lang (concat " " lang)))
(if (markdown-use-region-p)
(let ((b (region-beginning)) (e (region-end)))
Expand Down Expand Up @@ -3336,7 +3335,8 @@ automatically in order to have the correct markup."
(save-excursion
(goto-char (point-min))
(while (re-search-forward markdown-regex-gfm-code-block nil t)
(markdown-add-language-if-new (match-string-no-properties 2))))))
(let ((lang (match-string-no-properties 2)))
(when lang (markdown-add-language-if-new lang)))))))


;;; Footnotes ======================================================================
Expand Down
5 changes: 3 additions & 2 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3157,9 +3157,10 @@ indented the same amount."

(ert-deftest test-markdown-gfm/gfm-parse-buffer-for-languages ()
"Parse buffer for existing languages for `markdown-gfm-used-languages' test."
(markdown-test-string-gfm "``` MADEUP\n\n```\n```LANGUAGES\n\n```\n```MaDeUp\n\n```\n"
(markdown-test-string-gfm "``` MADEUP\n\n```\n``` LANGUAGES\n\n```\n```MaDeUp\n\n```\n```\n\n```\n``` \n\n```\n"
(markdown-gfm-parse-buffer-for-languages)
(should (equal markdown-gfm-used-languages (list "MaDeUp" "LANGUAGES" "MADEUP")))
(should (equal markdown-gfm-used-languages
(list "MaDeUp" "LANGUAGES" "MADEUP")))
(should (equal markdown-gfm-last-used-language "MaDeUp"))
(goto-char (point-max))
(markdown-insert-gfm-code-block "newlang")
Expand Down