Skip to content

Commit 800be46

Browse files
authored
Merge pull request #685 from jrblevin/issue-684
Fix invalid code block highlighting
2 parents 541bd7b + 088f0e5 commit 800be46

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
- Fix `markdown-enable-math` exception issue [GH-676][]
8888
- Fix `markdown-marginalize-headers` rendering in tty mode [GH-677][]
8989
- Fix table and list fontification [GH-680][]
90+
- Fix invalid code block highlighting [GH-684][]
9091

9192
[gh-290]: https://github.com/jrblevin/markdown-mode/issues/290
9293
[gh-311]: https://github.com/jrblevin/markdown-mode/issues/311
@@ -132,6 +133,7 @@
132133
[gh-676]: https://github.com/jrblevin/markdown-mode/pull/676
133134
[gh-677]: https://github.com/jrblevin/markdown-mode/pull/677
134135
[gh-680]: https://github.com/jrblevin/markdown-mode/pull/680
136+
[gh-666]: https://github.com/jrblevin/markdown-mode/issues/684
135137

136138
# Markdown Mode 2.4
137139

markdown-mode.el

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,13 @@ MIDDLE-BEGIN is the start of the \"middle\" section of the block."
15741574
(put-text-property close-begin close-end
15751575
(cl-cadadr fence-spec) close-data))))
15761576

1577+
(defun markdown--triple-quote-single-line-p (begin)
1578+
(save-excursion
1579+
(goto-char begin)
1580+
(save-match-data
1581+
(and (search-forward "```" nil t)
1582+
(search-forward "```" (line-end-position) t)))))
1583+
15771584
(defun markdown-syntax-propertize-fenced-block-constructs (start end)
15781585
"Propertize according to `markdown-fenced-block-pairs' from START to END.
15791586
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."
16081615
(while (re-search-forward start-reg end t)
16091616
;; we assume the opening constructs take up (only) an entire line,
16101617
;; so we re-check the current line
1611-
(let* ((cur-line (buffer-substring (point-at-bol) (point-at-eol)))
1618+
(let* ((block-start (match-beginning 0))
1619+
(cur-line (buffer-substring (point-at-bol) (point-at-eol)))
16121620
;; find entry in `markdown-fenced-block-pairs' corresponding
16131621
;; to regex which was matched
16141622
(correct-entry
@@ -1625,18 +1633,20 @@ start which was previously propertized."
16251633
(cl-caadr correct-entry)
16261634
(if (and (match-beginning 1) (match-end 1))
16271635
(- (match-end 1) (match-beginning 1))
1628-
0))))
1629-
;; get correct match data
1630-
(save-excursion
1631-
(beginning-of-line)
1632-
(re-search-forward
1633-
(markdown-maybe-funcall-regexp (caar correct-entry))
1634-
(point-at-eol)))
1635-
;; mark starting, even if ending is outside of region
1636-
(put-text-property (match-beginning 0) (match-end 0)
1637-
(cl-cadar correct-entry) (match-data t))
1638-
(markdown-propertize-end-match
1639-
end-reg end correct-entry enclosed-text-start))))))
1636+
0)))
1637+
(prop (cl-cadar correct-entry)))
1638+
(when (or (not (eq prop 'markdown-gfm-block-begin))
1639+
(not (markdown--triple-quote-single-line-p block-start)))
1640+
;; get correct match data
1641+
(save-excursion
1642+
(beginning-of-line)
1643+
(re-search-forward
1644+
(markdown-maybe-funcall-regexp (caar correct-entry))
1645+
(point-at-eol)))
1646+
;; mark starting, even if ending is outside of region
1647+
(put-text-property (match-beginning 0) (match-end 0) prop (match-data t))
1648+
(markdown-propertize-end-match
1649+
end-reg end correct-entry enclosed-text-start)))))))
16401650

16411651
(defun markdown-syntax-propertize-blockquotes (start end)
16421652
"Match blockquotes from START to END."

tests/markdown-test.el

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,6 +3308,24 @@ takes precedence)."
33083308
(markdown-test-range-has-face 2647 2728 'markdown-pre-face) ; code
33093309
(markdown-test-range-has-face 2730 2732 'markdown-markup-face))) ; ```
33103310

3311+
(ert-deftest test-markdown-font-lock/gfm-code-block-with-one-line-block ()
3312+
"Highlighting gfm code block with one line triple quote blocks.
3313+
Detail: https://github.com/jrblevin/markdown-mode/issues/684"
3314+
(markdown-test-string-gfm "```sudo ln -s `which mkdir` /usr/bin/mkdir```
3315+
3316+
Install Foreman. For running Rails, Sidekiq and anything else:
3317+
3318+
```ruby
3319+
rails db:migrate RAILS_ENV=test
3320+
```"
3321+
(forward-line 2)
3322+
(markdown-test-range-has-face (point) (line-end-position) nil)
3323+
(re-search-forward "ruby")
3324+
(goto-char (match-beginning 0))
3325+
(markdown-test-range-has-face (point) (+ (point) 3) 'markdown-language-keyword-face)
3326+
(forward-line 1)
3327+
(markdown-test-range-has-face (point) (line-end-position) 'markdown-pre-face)))
3328+
33113329
(ert-deftest test-markdown-font-lock/reference-definition ()
33123330
"Reference definitions should not include ]."
33133331
(let ((markdown-hide-urls nil))

0 commit comments

Comments
 (0)