Skip to content

Commit

Permalink
Fallback to HTML lexer for Markdown text.
Browse files Browse the repository at this point in the history
Fixes #321.
  • Loading branch information
alecthomas committed Mar 5, 2020
1 parent 34d9c71 commit 4f3623d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lexers/m/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package m

import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/h"
"github.com/alecthomas/chroma/lexers/internal"
)

// Markdown lexer.
var Markdown = internal.Register(MustNewLexer(
var Markdown = internal.Register(DelegatingLexer(h.HTML, MustNewLexer(
&Config{
Name: "markdown",
Aliases: []string{"md", "mkd"},
Expand Down Expand Up @@ -40,8 +41,8 @@ var Markdown = internal.Register(MustNewLexer(
{"`[^`]+`", LiteralStringBacktick, nil},
{`[@#][\w/:]+`, NameEntity, nil},
{`(!?\[)([^]]+)(\])(\()([^)]+)(\))`, ByGroups(Text, NameTag, Text, Text, NameAttribute, Text), nil},
{`[^\\\s]+`, Text, nil},
{`.|\n`, Text, nil},
{`[^\\\s]+`, Other, nil},
{`.|\n`, Other, nil},
},
},
))
)))
2 changes: 2 additions & 0 deletions lexers/testdata/markdown.actual
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# about

<div class="html">HTML</div>

Multiple **bold** on the **same line**.

## user defined function in cql
Expand Down
14 changes: 13 additions & 1 deletion lexers/testdata/markdown.expected
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
[
{"type":"GenericHeading","value":"# about\n"},
{"type":"Text","value":"\nMultiple "},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"div"},
{"type":"Text","value":" "},
{"type":"NameAttribute","value":"class"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"html\""},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"HTML"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"div"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n\nMultiple "},
{"type":"GenericStrong","value":"**bold**"},
{"type":"Text","value":" on the "},
{"type":"GenericStrong","value":"**same line**"},
Expand Down

0 comments on commit 4f3623d

Please sign in to comment.