glamour icon indicating copy to clipboard operation
glamour copied to clipboard

Feature/frontmatter yaml parser

Open deicon opened this issue 1 year ago • 0 comments

Parsing Frontmatter in YAML Format.

This PR adds a Frontmatter Parser to detect and parse Frontmatter in YAML Format. Frontmatter produces its own AST Element which can be handled by renderers. But as the Frontmatter is more about adding non-visible meta data, its also possible to register a Callback Handler to receive the Parsed FrontMatter Meta Data once the Parser detected a Frontmatter block

Possible Usage in https://github.com/charmbracelet/glow

func (h Handler) HandleFrontmatter(frontmatter map[string]interface{}) {
	fmt.Printf("Hello Frontmatter %v", frontmatter)
}

// This is where the magic happens.
func glamourRender(m pagerModel, markdown string) (string, error) {
	if !config.GlamourEnabled {
		return markdown, nil
	}

	// initialize glamour
	var gs glamour.TermRendererOption
	if m.common.cfg.GlamourStyle == "auto" {
		gs = glamour.WithAutoStyle()
	} else {
		gs = glamour.WithStylePath(m.common.cfg.GlamourStyle)
	}

	width := max(0, min(int(m.common.cfg.GlamourMaxWidth), m.viewport.Width))
	r, err := glamour.NewTermRenderer(
		gs,
		glamour.WithWordWrap(width),
		glamour.WithFrontMatterHandler(Handler{}),
	)

deicon avatar Nov 23 '22 12:11 deicon