mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-09 09:49:09 +02:00
Ignore YAML frontmatter
This commit is contained in:
parent
9ce4ec9149
commit
998cbf1cc4
3 changed files with 24 additions and 0 deletions
19
utils/utils.go
Normal file
19
utils/utils.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package utils
|
||||
|
||||
import "regexp"
|
||||
|
||||
func RemoveFrontmatter(content []byte) []byte {
|
||||
if frontmatterBoundaries := detectFrontmatter(content); frontmatterBoundaries[0] == 0 {
|
||||
return content[frontmatterBoundaries[1]:]
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
var yamlPattern = regexp.MustCompile(`(?m)^---\r?\n(\s*\r?\n)?`)
|
||||
|
||||
func detectFrontmatter(c []byte) []int {
|
||||
if matches := yamlPattern.FindAllIndex(c, 2); len(matches) > 1 {
|
||||
return []int{matches[0][0], matches[1][1]}
|
||||
}
|
||||
return []int{-1, -1}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue