1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-10-08 22:52:04 +02:00

fix(markdown): don't delegate to HTML lexer

This causes issues whereby Markdown code fragments such as:

```
`<script>`
```

Are treated as HTML, resulting in errors and backtracking.

Note that this does mean that HTML in Markdown is not highlighted, but
we can potentially add rules to the Markdown lexer to solve that.

Fixes #1119
This commit is contained in:
Alec Thomas
2025-08-04 13:49:13 +10:00
parent ea89498e83
commit 1f48e65abc
2 changed files with 4 additions and 16 deletions

View File

@@ -5,7 +5,7 @@ import (
)
// Markdown lexer.
var Markdown = Register(DelegatingLexer(HTML, MustNewLexer(
var Markdown = Register(MustNewLexer(
&Config{
Name: "markdown",
Aliases: []string{"md", "mkd"},
@@ -13,7 +13,7 @@ var Markdown = Register(DelegatingLexer(HTML, MustNewLexer(
MimeTypes: []string{"text/x-markdown"},
},
markdownRules,
)))
))
func markdownRules() Rules {
return Rules{
@@ -40,7 +40,7 @@ func markdownRules() Rules {
{"`[^`]+`", LiteralStringBacktick, nil},
{`[@#][\w/:]+`, NameEntity, nil},
{`(!?\[)([^]]+)(\])(\()([^)]+)(\))`, ByGroups(Text, NameTag, Text, Text, NameAttribute, Text), nil},
{`.|\n`, Other, nil},
{`.|\n`, Text, nil},
},
}
}

View File

@@ -1,18 +1,6 @@
[
{"type":"GenericHeading","value":"# about\n"},
{"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":"Text","value":"\n\u003cdiv class=\"html\"\u003eHTML\u003c/div\u003e\n\nMultiple "},
{"type":"GenericStrong","value":"**bold**"},
{"type":"Text","value":" on the "},
{"type":"GenericStrong","value":"**same line**"},