Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/frontmatter yaml parser #206

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

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