Skip to content
Permalink
Browse files

Adding implementation

  • Loading branch information...
Depado committed Oct 9, 2017
1 parent fb8fc86 commit 4dab1ca57ec3a73f04e8e6c15bf96dc5d441f14e
Showing with 95 additions and 0 deletions.
  1. +31 −0 README.md
  2. +64 −0 renderer.go
@@ -1,2 +1,33 @@
# blackfriday-chroma
Integrating Chroma syntax highlighter as a blackfriday renderer

## Usage

```go
package main
import (
"fmt"
"github.com/Depado/blackfriday-chroma"
bf "gopkg.in/russross/blackfriday.v2"
)
var md = "This is some sample code.\n\n```go\n" +
`func main() {
fmt.Println("Hi")
}
` + "```"
func main() {
r := bfchroma.ChromaRenderer{
Base: bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
}),
Style: "monokai",
}
html := bf.Run([]byte(md), bf.WithRenderer(&r))
fmt.Println(string(html))
}
```
@@ -0,0 +1,64 @@
package bfchroma

import (
"io"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"

bf "gopkg.in/russross/blackfriday.v2"
)

func (r *ChromaRenderer) renderWithChroma(w io.Writer, text []byte, data bf.CodeBlockData) error {
var lexer chroma.Lexer
if len(data.Info) > 0 {
lexer = lexers.Get(string(data.Info))
} else {
lexer = lexers.Analyse(string(text))
}
if lexer == nil {
lexer = lexers.Fallback
}
cstyle := styles.Get(r.Style)
if cstyle == nil {
cstyle = styles.Fallback
}
formatter := html.New()
iterator, err := lexer.Tokenise(nil, string(text))
if err != nil {
return err
}
return formatter.Format(w, cstyle, iterator)
}

// ChromaRenderer is a custom Blackfriday renderer that uses the capabilities of
// chroma to highlight code with triple backtick notation
type ChromaRenderer struct {
Base *bf.HTMLRenderer
Style string
}

// RenderNode satisfies the Renderer interface
func (r *ChromaRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
switch node.Type {
case bf.CodeBlock:
if err := r.renderWithChroma(w, node.Literal, node.CodeBlockData); err != nil {
return r.Base.RenderNode(w, node, entering)
}
return bf.SkipChildren
default:
return r.Base.RenderNode(w, node, entering)
}
}

// RenderHeader satisfies the Renderer interface
func (r *ChromaRenderer) RenderHeader(w io.Writer, ast *bf.Node) {
r.Base.RenderHeader(w, ast)
}

// RenderFooter satisfies the Renderer interface
func (r *ChromaRenderer) RenderFooter(w io.Writer, ast *bf.Node) {
r.Base.RenderFooter(w, ast)
}

0 comments on commit 4dab1ca

Please sign in to comment.
You can’t perform that action at this time.