mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 03:32:56 +00:00

Former-commit-id: c9c397710ea65ce273ebbe584276ef5eff7d2ca1 [formerly cef3eaec1126e45e2e64225c2224ab313954dbc4] [formerly 550903ae12bf3d907f67572bb5c204a0079bf42f [formerly 1f52d5d0fda0d1132d04c900565f2ecffccfd778]] Former-commit-id: a66194a23a57f764a76b4f7959812257331e19db [formerly 3827a7330f021001aa2af61678e4ee9219e2571a] Former-commit-id: 5a6fd2a47a07e32e1ea58e05c8b4493be38bb398
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package frontmatter
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// HasRune checks if the file has the frontmatter rune
|
|
func HasRune(file []byte) bool {
|
|
return strings.HasPrefix(string(file), "---") ||
|
|
strings.HasPrefix(string(file), "+++") ||
|
|
strings.HasPrefix(string(file), "{")
|
|
}
|
|
|
|
// AppendRune appends the frontmatter rune to a file
|
|
func AppendRune(frontmatter []byte, mark rune) []byte {
|
|
frontmatter = bytes.TrimSpace(frontmatter)
|
|
|
|
switch mark {
|
|
case '-':
|
|
return []byte("---\n" + string(frontmatter) + "\n---")
|
|
case '+':
|
|
return []byte("+++\n" + string(frontmatter) + "\n+++")
|
|
case '{':
|
|
return []byte("{\n" + string(frontmatter) + "\n}")
|
|
}
|
|
|
|
return frontmatter
|
|
}
|
|
|
|
// RuneToStringFormat converts the rune to a string with the format
|
|
func RuneToStringFormat(mark rune) (string, error) {
|
|
switch mark {
|
|
case '-':
|
|
return "yaml", nil
|
|
case '+':
|
|
return "toml", nil
|
|
case '{', '}':
|
|
return "json", nil
|
|
default:
|
|
return "", errors.New("Unsupported format type")
|
|
}
|
|
}
|
|
|
|
// StringFormatToRune converts the format name to its rune
|
|
func StringFormatToRune(format string) (rune, error) {
|
|
switch format {
|
|
case "yaml":
|
|
return '-', nil
|
|
case "toml":
|
|
return '+', nil
|
|
case "json":
|
|
return '{', nil
|
|
default:
|
|
return '0', errors.New("Unsupported format type")
|
|
}
|
|
}
|