~poptart/x

5d0ee4e19d2656605a5cc201f9948ef9a7d9c232 — terrorbyte 9 months ago ed8913c
Set the html renderer to output non-standalone chroma code blocks
1 files changed, 31 insertions(+), 1 deletions(-)

M md/html/renderer.go
M md/html/renderer.go => md/html/renderer.go +31 -1
@@ 9,6 9,11 @@ import (
	"strconv"
	"strings"

	"github.com/alecthomas/chroma"
	"github.com/alecthomas/chroma/formatters"
	chromaHtml "github.com/alecthomas/chroma/formatters/html"
	"github.com/alecthomas/chroma/lexers"
	"github.com/alecthomas/chroma/styles"
	"github.com/gomarkdown/markdown/ast"
)



@@ 794,7 799,32 @@ func (r *Renderer) codeBlock(w io.Writer, codeBlock *ast.CodeBlock) {
	if r.opts.Comments != nil {
		r.EscapeHTMLCallouts(w, codeBlock.Literal)
	} else {
		EscapeHTML(w, codeBlock.Literal)
		options := []chromaHtml.Option{
			chromaHtml.Standalone(false),
		}
		lexer := lexers.Analyse(string(codeBlock.Literal))
		if lexer == nil {
			lexer = lexers.Fallback
		}
		lexer = chroma.Coalesce(lexer)
		style := styles.Get("monokai")
		if style == nil {
			style = styles.Fallback
		}
		formatters.Register("html", chromaHtml.New(options...))
		formatter := formatters.Get("html")
		if formatter == nil {
			formatter = formatters.Fallback
		}
		iterator, err := lexer.Tokenise(nil, string(codeBlock.Literal))
		if err != nil {
			EscapeHTML(w, codeBlock.Literal)
		}
		err = formatter.Format(w, style, iterator)
		if err != nil {
			EscapeHTML(w, codeBlock.Literal)
		}

	}
	//r.outs(w, "</code>")
	r.outs(w, "</pre>")