Ignore YAML frontmatter

This commit is contained in:
Cristian Dominguez 2020-10-30 12:14:36 -03:00 committed by Christian Muehlhaeuser
parent 9ce4ec9149
commit 998cbf1cc4
3 changed files with 24 additions and 0 deletions

View file

@ -22,6 +22,7 @@ import (
"github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glow/ui"
"github.com/charmbracelet/glow/utils"
)
var (
@ -195,6 +196,8 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
return err
}
b = utils.RemoveFrontmatter(b)
// render
var baseURL string
u, err := url.ParseRequestURI(src.URL)

View file

@ -15,6 +15,7 @@ import (
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/keygen"
"github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/glow/utils"
"github.com/muesli/gitcha"
te "github.com/muesli/termenv"
)
@ -355,6 +356,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case fetchedMarkdownMsg:
m.pager.currentDocument = *msg
msg.Body = string(utils.RemoveFrontmatter([]byte(msg.Body)))
cmds = append(cmds, renderWithGlamour(m.pager, msg.Body))
case contentRenderedMsg:

19
utils/utils.go Normal file
View 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}
}