synt
ax h
ighlighting for code cells and semi-structured output (JSON/XML) with chroma
.
go get github.com/bevzzz/nb-synth
Adding rich syntax highlighting to your Jupyter notebooks with synth
is simple and intuitive.
Start by importing both libraries to your project:
import (
"github.com/bevzzz/nb"
synth "github.com/bevzzz/nb-synth"
)
Then, extend nb
converter and convert your Jupyter notebook:
import (
// synth allows configuring chroma's lexer and formatter
// with their "native" options, so you will need to import these
// before adding them to the extension.
"github.com/alecthomas/chroma"
chromahtml "github.com/alecthomas/chroma/formatters/html"
)
// Use default configurations.
c := nb.New(
nb.WithExtensions(
synth.Highlighting,
),
)
// Or, apply options to control lexing, styling, and formatting.
c := nb.New(
nb.WithExtensions(
synth.NewHighlighting(
synth.WithTokenizeOptions(&chroma.TokenizeOptions{
EnsureLF: true,
}),
synth.WithStyle("monokai"),
synth.WithFormatOptions(
chromahtml.WithClasses(true),
chromahtml.WithLineNumbers(true),
),
),
),
)
if err := c.Convert(io.Stdout, b); err != nil {
panic(err)
}
This package draws a lot from the analogous goldmark-highlighting
, which adds syntax highlighting to goldmark
's Markdown. If you've had the chance to work with these packages before, synth
's structure and APIs will be familiar to you.
Repository cover: Synthesizer Vectors by Vecteezy
This software is released under the MIT License.