mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

Former-commit-id: de26609879e875b21de329588ecd1dcb44d152f3 [formerly 78b120bf0d33345808a422980db55d33c52304b7] [formerly d77c47bb41c1a7bf6ee2b2522bf7c86638d087bc [formerly 2819ab24b85a578dc1f572f1e64eccc98cabd6c3]] Former-commit-id: 3ddee564ddd5ed4fde01ed95f30386115a07df78 [formerly a3a1da0357874b7d99a88bd785c8f60f8170f663] Former-commit-id: 0e15a59e28993f8178fa9409a466ce93a9936906
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package frontmatter
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// HasRune checks if the file has the frontmatter rune
|
|
func HasRune(file string) bool {
|
|
return strings.HasPrefix(file, "---") ||
|
|
strings.HasPrefix(file, "+++") ||
|
|
strings.HasPrefix(file, "{")
|
|
}
|
|
|
|
// AppendRune appends the frontmatter rune to a file
|
|
func AppendRune(frontmatter string, mark rune) string {
|
|
frontmatter = strings.TrimSpace(frontmatter)
|
|
|
|
switch mark {
|
|
case '-':
|
|
return "---\n" + frontmatter + "\n---"
|
|
case '+':
|
|
return "+++\n" + frontmatter + "\n+++"
|
|
case '{':
|
|
return "{\n" + 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")
|
|
}
|
|
}
|