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