Documentation ¶
Overview ¶
Package bfchroma provides an easy and extensible blackfriday renderer that uses the chroma syntax highlighter to render code blocks.
Index ¶
- type Analyser
- type Option
- type Renderer
- func (r *Renderer) ChromaCSS(w io.Writer) error
- func (r *Renderer) RenderFooter(w io.Writer, ast *bf.Node)
- func (r *Renderer) RenderHeader(w io.Writer, ast *bf.Node)
- func (r *Renderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus
- func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockData) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Analyser ¶
type Analyser func(src string) chroma.Lexer
Analyser defines a function that returns the correct chroma.Lexer for src.
type Option ¶
type Option func(r *Renderer)
Option defines the functional option type
func ChromaOptions ¶
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.Get("github"))) h := bf.Run([]byte(md), bf.WithRenderer(r)) fmt.Println(string(h))
Output:
func DetectAs ¶
DetectAs causes autodetection to always "detect" lang, using chroma's lexers.Get function. If lang is unknown then it will use lexers.Fallback.
Example ¶
md := "Know your go!\n\n```\nconst SomethingGoey = 1.234\n```" r := NewRenderer(DetectAs("go")) h := bf.Run([]byte(md), bf.WithRenderer(r)) fmt.Println(string(h))
Output: <p>Know your go!</p> <pre style="color:#f8f8f2;background-color:#272822;"><code><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">SomethingGoey</span> = <span style="color:#ae81ff">1.234</span> </span></span></code></pre>
func DetectLexer ¶
func DetectLexer(lex chroma.Lexer) Option
DetectLexer causes autodetection to always return lex.
func EmbedCSS ¶
func EmbedCSS() Option
EmbedCSS will embed CSS needed for html.WithClasses() in beginning of the document
func Extend ¶
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 ¶
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 bf.Renderer Autodetect bool Analyser Analyser 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 ¶
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.Get("github")), ChromaOptions(html.WithLineNumbers(true)), ) h := bf.Run([]byte(md), bf.WithRenderer(r)) fmt.Println(string(h))
Output:
func (*Renderer) RenderFooter ¶
RenderFooter satisfies the Renderer interface
func (*Renderer) RenderHeader ¶
RenderHeader satisfies the Renderer interface
func (*Renderer) RenderNode ¶
RenderNode satisfies the Renderer interface
func (*Renderer) RenderWithChroma ¶
RenderWithChroma will render the given text to the w io.Writer