bfchroma

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2021 License: MIT Imports: 8 Imported by: 0

README

mdchroma

Integrating Chroma syntax highlighter as a Go Markdown renderer.

Install and prerequisites

This project requires and uses the Go Markdown package.

$ go get -u github.com/saward/mdchroma

This project uses the module approach of go 1.11

Features

This renderer integrates chroma to highlight code with triple backtick notation. It will try to use the given language when available otherwise it will try to detect the language. If none of these two method works it will fallback to sane defaults.

This is a fork of the mdchroma package, modified to instead work with Go Markdown.

Usage

output := markdown.ToHTML(md, nil, mdchroma.NewRenderer())

Examples

package main

import (
	"fmt"

	chroma "github.com/saward/mdchroma"
	"github.com/gomarkdown/markdown"
)

var md = "This is some sample code.\n\n```go\n" +
	`func main() {
	fmt.Println("Hi")
}
` + "```"

func main() {
	html := markdown.ToHTML([]byte(md), nil, chroma.NewRenderer())
	fmt.Println(string(html))
}

Will output :

<p>This is some sample code.</p>
<pre style="color:#f8f8f2;background-color:#272822"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Hi&#34;</span>)
}
</pre>

Documentation

Overview

Package bfchroma provides an easy and extensible blackfriday renderer that uses the chroma syntax highlighter to render code blocks.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(r *Renderer)

Option defines the functional option type

func ChromaOptions

func ChromaOptions(options ...html.Option) Option

ChromaOptions allows to pass Chroma html.Option such as Standalone() WithClasses(), ClassPrefix(prefix)...

Example
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(ChromaOptions(html.WithLineNumbers(true)))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
Output:

func ChromaStyle

func ChromaStyle(s *chroma.Style) Option

ChromaStyle is an option to directly set the style of the renderer using a chroma style instead of a string

Example
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(ChromaStyle(styles.GitHub))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
Output:

func EmbedCSS added in v1.4.0

func EmbedCSS() Option

EmbedCSS will embed CSS needed for html.WithClasses() in beginning of the document

func Extend

func Extend(br md.Renderer) Option

Extend allows to specify the blackfriday renderer which is extended

Example
md := "```go\npackage main\n\nfunc main() {\n}\n```"

b := bf.NewHTMLRenderer(bf.HTMLRendererParameters{
	Flags: bf.CommonHTMLFlags,
})
r := NewRenderer(Extend(b))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
Output:

func Style

func Style(s string) Option

Style is a function option allowing to set the style used by chroma Default : "monokai"

Example
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(Style("github"))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
Output:

func WithoutAutodetect

func WithoutAutodetect() Option

WithoutAutodetect disables chroma's language detection when no codeblock extra information is given. It will fallback to a sane default instead of trying to detect the language.

Example
md := "```\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(WithoutAutodetect())

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
Output:

type Renderer

type Renderer struct {
	Base          md.Renderer
	Autodetect    bool
	ChromaOptions []html.Option
	Style         *chroma.Style
	Formatter     *html.Formatter
	// contains filtered or unexported fields
}

Renderer is a custom Blackfriday renderer that uses the capabilities of chroma to highlight code with triple backtick notation

func NewRenderer

func NewRenderer(options ...Option) *Renderer

NewRenderer will return a new bfchroma renderer with sane defaults

Example
// Complex example on how to initialize the renderer
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(
	Extend(bf.NewHTMLRenderer(bf.HTMLRendererParameters{
		Flags: bf.CommonHTMLFlags,
	})),
	WithoutAutodetect(),
	ChromaStyle(styles.GitHub),
	ChromaOptions(html.WithLineNumbers(true)),
)

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
Output:

func (*Renderer) ChromaCSS added in v1.4.0

func (r *Renderer) ChromaCSS(w io.Writer) error

ChromaCSS returns CSS used with chroma's html.WithClasses() option

func (*Renderer) RenderFooter

func (r *Renderer) RenderFooter(w io.Writer, ast ast.Node)

RenderFooter satisfies the Renderer interface

func (*Renderer) RenderHeader

func (r *Renderer) RenderHeader(w io.Writer, ast ast.Node)

RenderHeader satisfies the Renderer interface

func (*Renderer) RenderNode

func (r *Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus

RenderNode satisfies the Renderer interface

func (*Renderer) RenderWithChroma

func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data ast.CodeBlock) error

RenderWithChroma will render the given text to the w io.Writer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL