Monorepo for Tangled
tangled.org
1package markup
2
3import (
4 "regexp"
5
6 "github.com/go-git/go-git/v5/plumbing/filemode"
7)
8
9type Format string
10
11const (
12 FormatMarkdown Format = "markdown"
13 FormatText Format = "text"
14)
15
16var FileTypePatterns = map[Format]*regexp.Regexp{
17 FormatMarkdown: regexp.MustCompile(`(?i)\.(md|markdown|mdown|mkdn|mkd)$`),
18}
19
20var ReadmePattern = regexp.MustCompile(`(?i)^readme(?:\.[^.]+)?$`)
21
22// IsReadmeFile reports whether name/mode identifies a readme blob. The git
23// mode is checked so directories or symlinks named "readme" are filtered out.
24func IsReadmeFile(name, mode string) bool {
25 if !ReadmePattern.MatchString(name) {
26 return false
27 }
28 m, err := filemode.New(mode)
29 if err != nil {
30 return false
31 }
32 return m == filemode.Regular || m == filemode.Executable
33}
34
35// GetFormat returns the Format whose extension list matches filename,
36// falling back to FormatText.
37func GetFormat(filename string) Format {
38 for format, pattern := range FileTypePatterns {
39 if pattern.MatchString(filename) {
40 return format
41 }
42 }
43 return FormatText
44}