charmbracelet / glamour

Stylesheet-based markdown rendering for your CLI apps 💇🏻‍♀️

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Frontmatter support

markusbkk opened this issue · comments

Would be great if there was some way to add (custom) frontmatter to the rendering process.

Can you elaborate in what you would like to do with custom front matter data ? In terms of rendering that is.

Can you elaborate in what you would like to do with custom front matter data ? In terms of rendering that is.

Personally, in my specific case, I need it for things like title and synopsis.
I'm working on an alternative to Unix' man/mandoc. Would be great if there was a way to define a common template for this.
There's other ways to go about it too, of course. But I've always been a fan of using frontmatter to keep things organized and standardized.

Sounds reasonable but as far as I can see this looks like an extension to the underlying markdown parser.

Sounds reasonable but as far as I can see this looks like an extension to the underlying markdown parser.

I feared that.

Although I notice that glow has its own implementation (albeit one that only removes the frontmatter).

Maybe I can do something with this to implement my own preprocessor without depending on changes in glamour or goldmark.

@markusbkk I made some tests and adjusted my glamour fork as well as my local
glow

Essentially adding a FrontMatter YAML Parser to glamour and enabling a way to register a FrontMatter Callback.
As I am currently learning Go, I am not yet sure, if this is something I would PR in this stage. But serves as a POC I think.

// will be called once Frontmatter was parsed and converted to map
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{}),
	)

Sounds reasonable but as far as I can see this looks like an extension to the underlying markdown parser.

I feared that.

Although I notice that glow has its own implementation (albeit one that only removes the frontmatter).

Maybe I can do something with this to implement my own preprocessor without depending on changes in glamour or goldmark.

Sure you could. As I wanted Frontmatter support for some scripts here as well, I thought about too. But this looked too much of a hack. So I decided to try to use the extension feature and add a new Parser. Because a Parser is the thing to do as it is a legit part of Markdown files in the wild