Skip to content

Feature/frontmatter yaml parser#206

Open
deicon wants to merge 7 commits intocharmbracelet:masterfrom
deicon:feature/frontmatter_yaml_parser
Open

Feature/frontmatter yaml parser#206
deicon wants to merge 7 commits intocharmbracelet:masterfrom
deicon:feature/frontmatter_yaml_parser

Conversation

@deicon
Copy link

@deicon deicon commented Nov 23, 2022

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{}),
	)

@caarlos0
Copy link
Member

caarlos0 commented Jul 9, 2024

i wonder if it is worth it trying https://github.com/abhinav/goldmark-frontmatter , as it also supports toml frontmatter 🤔

@aymanbagabas
Copy link
Member

aymanbagabas commented Jul 9, 2024

i wonder if it is worth it trying https://github.com/abhinav/goldmark-frontmatter , as it also supports toml frontmatter 🤔

+1 for either abhinav/goldmark-frontmatter or yuin/goldmark-meta

EDIT: leaning more towards yuin/goldmark-meta

@deicon
Copy link
Author

deicon commented Jul 9, 2024

i wonder if it is worth it trying https://github.com/abhinav/goldmark-frontmatter , as it also supports toml frontmatter 🤔

Sure. I think I wasn't aware of the availability of this extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants