1package m
2
3import (
4 . "github.com/alecthomas/chroma" // nolint
5 "github.com/alecthomas/chroma/lexers/internal"
6)
7
8// Markdown lexer.
9var Markdown = internal.Register(MustNewLexer(
10 &Config{
11 Name: "markdown",
12 Aliases: []string{"md", "mkd"},
13 Filenames: []string{"*.md", "*.mkd", "*.markdown"},
14 MimeTypes: []string{"text/x-markdown"},
15 },
16 Rules{
17 "root": {
18 {`^(#[^#].+\n)`, ByGroups(GenericHeading), nil},
19 {`^(#{2,6}.+\n)`, ByGroups(GenericSubheading), nil},
20 {`^(\s*)([*-] )(\[[ xX]\])( .+\n)`, ByGroups(Text, Keyword, Keyword, UsingSelf("inline")), nil},
21 {`^(\s*)([*-])(\s)(.+\n)`, ByGroups(Text, Keyword, Text, UsingSelf("inline")), nil},
22 {`^(\s*)([0-9]+\.)( .+\n)`, ByGroups(Text, Keyword, UsingSelf("inline")), nil},
23 {`^(\s*>\s)(.+\n)`, ByGroups(Keyword, GenericEmph), nil},
24 {"^(```\\n)([\\w\\W]*?)(^```$)", ByGroups(String, Text, String), nil},
25 {"^(```)(\\w+)(\\n)([\\w\\W]*?)(^```$)",
26 UsingByGroup(
27 internal.Get,
28 2, 4,
29 String, String, String, Text, String,
30 ),
31 nil,
32 },
33 Include("inline"),
34 },
35 "inline": {
36 {`\\.`, Text, nil},
37 {`(\s)([*_][^*_]+[*_])(\W|\n)`, ByGroups(Text, GenericEmph, Text), nil},
38 {`(\s)((\*\*|__).*?)\3((?=\W|\n))`, ByGroups(Text, GenericStrong, GenericStrong, Text), nil},
39 {`(\s)(~~[^~]+~~)((?=\W|\n))`, ByGroups(Text, GenericDeleted, Text), nil},
40 {"`[^`]+`", LiteralStringBacktick, nil},
41 {`[@#][\w/:]+`, NameEntity, nil},
42 {`(!?\[)([^]]+)(\])(\()([^)]+)(\))`, ByGroups(Text, NameTag, Text, Text, NameAttribute, Text), nil},
43 {`[^\\\s]+`, Text, nil},
44 {`.|\n`, Text, nil},
45 },
46 },
47))