package filter

import (
	"gitbase.de/apairon/mark2web/pkg/helper"
	"gitbase.de/apairon/mark2web/pkg/mark2web"
	"github.com/flosch/pongo2"
)

// MarkdownFilter is a pongo2 filter, which converts markdown to html
func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	chromaRenderer := false
	chromaStyle := "monokai"
	if m := mark2web.CurrentPathConfig.Markdown; m != nil {
		if m.ChromaRenderer != nil && *m.ChromaRenderer {
			chromaRenderer = true
		}
		if m.ChromaStyle != nil && *m.ChromaStyle != "" {
			chromaStyle = *m.ChromaStyle
		}
	}
	return pongo2.AsSafeValue(
			string(
				helper.RenderMarkdown(
					[]byte(in.String()),
					chromaRenderer,
					chromaStyle,
				))),
		nil
}
