1. package lexers
    
  2. 
    
  3. import (
    
  4. 	. "github.com/alecthomas/chroma/v2" // nolint
    
  5. )
    
  6. 
    
  7. // Markdown lexer.
    
  8. var Markdown = Register(DelegatingLexer(HTML, MustNewLexer(
    
  9. 	&Config{
    
  10. 		Name:      "markdown",
    
  11. 		Aliases:   []string{"md", "mkd"},
    
  12. 		Filenames: []string{"*.md", "*.mkd", "*.markdown"},
    
  13. 		MimeTypes: []string{"text/x-markdown"},
    
  14. 	},
    
  15. 	markdownRules,
    
  16. )))
    
  17. 
    
  18. func markdownRules() Rules {
    
  19. 	return Rules{
    
  20. 		"root": {
    
  21. 			{`^(#[^#].+\n)`, ByGroups(GenericHeading), nil},
    
  22. 			{`^(#{2,6}.+\n)`, ByGroups(GenericSubheading), nil},
    
  23. 			{`^(\s*)([*-] )(\[[ xX]\])( .+\n)`, ByGroups(Text, Keyword, Keyword, UsingSelf("inline")), nil},
    
  24. 			{`^(\s*)([*-])(\s)(.+\n)`, ByGroups(Text, Keyword, Text, UsingSelf("inline")), nil},
    
  25. 			{`^(\s*)([0-9]+\.)( .+\n)`, ByGroups(Text, Keyword, UsingSelf("inline")), nil},
    
  26. 			{`^(\s*>\s)(.+\n)`, ByGroups(Keyword, GenericEmph), nil},
    
  27. 			{"^(```\\n)([\\w\\W]*?)(^```$)", ByGroups(String, Text, String), nil},
    
  28. 			{
    
  29. 				"^(```)(\\w+)(\\n)([\\w\\W]*?)(^```$)",
    
  30. 				UsingByGroup(2, 4, String, String, String, Text, String),
    
  31. 				nil,
    
  32. 			},
    
  33. 			Include("inline"),
    
  34. 		},
    
  35. 		"inline": {
    
  36. 			{`\\.`, Text, nil},
    
  37. 			{`(\s)(\*|_)((?:(?!\2).)*)(\2)((?=\W|\n))`, ByGroups(Text, GenericEmph, GenericEmph, 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]+`, Other, nil},
    
  44. 			{`.|\n`, Other, nil},
    
  45. 		},
    
  46. 	}
    
  47. }