Skip to content

Commit

Permalink
Fix brackets in links in Markdown lexer (#1445)
Browse files Browse the repository at this point in the history
Brackets (with backticks) inside link text was not being highlighted
correctly in the Markdown lexer. This commit causes the lexer to search
for a closing bracket that is followed by either an opening bracket
(indicating this is a reference-style link) or an opening parenthesis
(indicating this is an inline link).
  • Loading branch information
Ravlen committed Feb 23, 2020
1 parent 081edfd commit abe35e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rouge/lexers/markdown.rb
Expand Up @@ -102,7 +102,7 @@ def html
end

# links and images
rule %r/(!?\[)(#{edot}*?)(\])/ do
rule %r/(!?\[)(#{edot}*?)(\])(?=[\[(])/ do
groups Punctuation, Name::Variable, Punctuation
push :link
end
Expand All @@ -117,14 +117,16 @@ def html
rule %r/<.*?@.+[.].+>/, Name::Variable
rule %r[<(https?|mailto|ftp)://#{edot}*?>], Name::Variable


rule %r/[^\\`\[*\n&<]+/, Text

# inline html
rule(/&\S*;/) { delegate html }
rule(/<#{edot}*?>/) { delegate html }
rule %r/[&<]/, Text

# An opening square bracket that is not a link
rule %r/\[/, Text

rule %r/\n/, Text
end

Expand Down
13 changes: 13 additions & 0 deletions spec/visual/samples/markdown
Expand Up @@ -1078,3 +1078,16 @@ can't highlight me
```` console
$ hello "world"
````

````markdown
Here is an example of [feature](../feature.md):

```console
$ echo "Sample feature output"
```
````

[This is a link `with backticks`](example.com)
[This is a link to a TOML section `[with brackets]` (and backticks)](example.com)
[This is a link to a TOML section `[[with double brackets]]` (and backticks)](example.com)
[This is not a link `with backticks`] (example.com)

0 comments on commit abe35e8

Please sign in to comment.