mirror of
https://github.com/charmbracelet/glow
synced 2024-11-10 06:04:18 +00:00
Ignore YAML frontmatter
This commit is contained in:
parent
9ce4ec9149
commit
998cbf1cc4
3 changed files with 24 additions and 0 deletions
3
main.go
3
main.go
|
@ -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)
|
||||
|
|
2
ui/ui.go
2
ui/ui.go
|
@ -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
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…
Reference in a new issue