emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/markdown-mode 088f0e5c22 1/2: Fix invalid code block highl


From: ELPA Syncer
Subject: [nongnu] elpa/markdown-mode 088f0e5c22 1/2: Fix invalid code block highlighting
Date: Wed, 9 Feb 2022 10:02:44 -0500 (EST)

branch: elpa/markdown-mode
commit 088f0e5c22a58444e97ecbcb7a93f083d081c353
Author: Shohei YOSHIDA <syohex@gmail.com>
Commit: Shohei YOSHIDA <syohex@gmail.com>

    Fix invalid code block highlighting
---
 CHANGES.md             |  2 ++
 markdown-mode.el       | 36 +++++++++++++++++++++++-------------
 tests/markdown-test.el | 18 ++++++++++++++++++
 3 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index d94d9352d3..981b3fad55 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -87,6 +87,7 @@
     -   Fix `markdown-enable-math` exception issue [GH-676][]
     -   Fix `markdown-marginalize-headers` rendering in tty mode [GH-677][]
     -   Fix table and list fontification [GH-680][]
+    -   Fix invalid code block highlighting [GH-684][]
 
   [gh-290]: https://github.com/jrblevin/markdown-mode/issues/290
   [gh-311]: https://github.com/jrblevin/markdown-mode/issues/311
@@ -132,6 +133,7 @@
   [gh-676]: https://github.com/jrblevin/markdown-mode/pull/676
   [gh-677]: https://github.com/jrblevin/markdown-mode/pull/677
   [gh-680]: https://github.com/jrblevin/markdown-mode/pull/680
+  [gh-666]: https://github.com/jrblevin/markdown-mode/issues/684
 
 # Markdown Mode 2.4
 
diff --git a/markdown-mode.el b/markdown-mode.el
index 009eed31c9..585dc859af 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -1574,6 +1574,13 @@ MIDDLE-BEGIN is the start of the \"middle\" section of 
the block."
       (put-text-property close-begin close-end
                          (cl-cadadr fence-spec) close-data))))
 
+(defun markdown--triple-quote-single-line-p (begin)
+  (save-excursion
+    (goto-char begin)
+    (save-match-data
+      (and (search-forward "```" nil t)
+           (search-forward "```" (line-end-position) t)))))
+
 (defun markdown-syntax-propertize-fenced-block-constructs (start end)
   "Propertize according to `markdown-fenced-block-pairs' from START to END.
 If unable to propertize an entire block (if the start of a block is within 
START
@@ -1608,7 +1615,8 @@ start which was previously propertized."
       (while (re-search-forward start-reg end t)
         ;; we assume the opening constructs take up (only) an entire line,
         ;; so we re-check the current line
-        (let* ((cur-line (buffer-substring (point-at-bol) (point-at-eol)))
+        (let* ((block-start (match-beginning 0))
+               (cur-line (buffer-substring (point-at-bol) (point-at-eol)))
                ;; find entry in `markdown-fenced-block-pairs' corresponding
                ;; to regex which was matched
                (correct-entry
@@ -1625,18 +1633,20 @@ start which was previously propertized."
                  (cl-caadr correct-entry)
                  (if (and (match-beginning 1) (match-end 1))
                      (- (match-end 1) (match-beginning 1))
-                   0))))
-          ;; get correct match data
-          (save-excursion
-            (beginning-of-line)
-            (re-search-forward
-             (markdown-maybe-funcall-regexp (caar correct-entry))
-             (point-at-eol)))
-          ;; mark starting, even if ending is outside of region
-          (put-text-property (match-beginning 0) (match-end 0)
-                             (cl-cadar correct-entry) (match-data t))
-          (markdown-propertize-end-match
-           end-reg end correct-entry enclosed-text-start))))))
+                   0)))
+               (prop (cl-cadar correct-entry)))
+          (when (or (not (eq prop 'markdown-gfm-block-begin))
+                    (not (markdown--triple-quote-single-line-p block-start)))
+            ;; get correct match data
+            (save-excursion
+              (beginning-of-line)
+              (re-search-forward
+               (markdown-maybe-funcall-regexp (caar correct-entry))
+               (point-at-eol)))
+            ;; mark starting, even if ending is outside of region
+            (put-text-property (match-beginning 0) (match-end 0) prop 
(match-data t))
+            (markdown-propertize-end-match
+             end-reg end correct-entry enclosed-text-start)))))))
 
 (defun markdown-syntax-propertize-blockquotes (start end)
   "Match blockquotes from START to END."
diff --git a/tests/markdown-test.el b/tests/markdown-test.el
index 097780d6fd..50b0e8043a 100644
--- a/tests/markdown-test.el
+++ b/tests/markdown-test.el
@@ -3308,6 +3308,24 @@ takes precedence)."
     (markdown-test-range-has-face 2647 2728 'markdown-pre-face) ; code
     (markdown-test-range-has-face 2730 2732 'markdown-markup-face))) ; ```
 
+(ert-deftest test-markdown-font-lock/gfm-code-block-with-one-line-block ()
+  "Highlighting gfm code block with one line triple quote blocks.
+Detail: https://github.com/jrblevin/markdown-mode/issues/684";
+  (markdown-test-string-gfm "```sudo ln -s `which mkdir` /usr/bin/mkdir```
+
+Install Foreman. For running Rails, Sidekiq and anything else:
+
+```ruby
+rails db:migrate RAILS_ENV=test
+```"
+    (forward-line 2)
+    (markdown-test-range-has-face (point) (line-end-position) nil)
+    (re-search-forward "ruby")
+    (goto-char (match-beginning 0))
+    (markdown-test-range-has-face (point) (+ (point) 3) 
'markdown-language-keyword-face)
+    (forward-line 1)
+    (markdown-test-range-has-face (point) (line-end-position) 
'markdown-pre-face)))
+
 (ert-deftest test-markdown-font-lock/reference-definition ()
   "Reference definitions should not include ]."
   (let ((markdown-hide-urls nil))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]