Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Integrating Chroma syntax highlighter as a Blackfriday renderer

License

NotificationsYou must be signed in to change notification settings

depado/bfchroma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

forthebadgeforthebadge

Go Report CardLicenseGodocSourcegraphSay Thanks!

IntegratingChroma syntax highlighter asaBlackfriday renderer.

Install and prerequisites

This project requires and uses thev2 version ofBlackfriday.

$ go get github.com/Depado/bfchroma/v2

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 todetect the language. If none of these two method works it will fallback to sanedefaults.

Usage

bfchroma uses the functional options approach so you can customize the behaviorof the renderer. It uses sane defaults when no option is passed so you can usethe renderer simply by doing so :

html:=bf.Run([]byte(md),bf.WithRenderer(bfchroma.NewRenderer()))

Options

  • Style(s string)Define the style used by chroma for the rendering. The full list can be foundhere
  • ChromaStyle(*chroma.Style)This option can be used to passe directly a*chroma.Style instead of thestring representing the style as with theStyle(string) option.
  • WithoutAutodetect()By default when no language information is written in the code block, thisrenderer will try to auto-detect the used language. This option disablesthis behavior and will fallback to a sane default when no languageinformation is available.
  • EmbedCSS()This option will embed CSS needed for chroma'shtml.WithClasses() at the beginning of blackfriday document.CSS can also be extracted separately by callingRenderer's.ChromaCSS(w) method, which will return styleshet for currently set style
  • Extend(bf.Renderer)This option allows to define the base blackfriday that will be extended.
  • ChromaOptions(...html.Option)This option allows you to pass Chroma's html options in the renderer. Suchoptions can be foundhere.

Option examples

Disabling language auto-detection and displaying line numbers

r:=bfchroma.NewRenderer(bfchroma.WithoutAutodetect(),bfchroma.ChromaOptions(html.WithLineNumbers()),)

Extend a blackfriday renderer

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

Use a different style

r:=bfchroma.NewRenderer(bfchroma.Style("dracula"))// Orr=bfchroma.NewRenderer(bfchroma.ChromaStyle(styles.Dracula))

Examples

package mainimport ("fmt""github.com/Depado/bfchroma/v2"bf"github.com/russross/blackfriday/v2")varmd="This is some sample code.\n\n```go\n"+`func main() {fmt.Println("Hi")}`+"```"funcmain() {html:=bf.Run([]byte(md),bf.WithRenderer(bfchroma.NewRenderer()))fmt.Println(string(html))}

Will output :

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

Real-life example

Insmallblog I'm using bfchroma to rendermy articles. It's using a combination of both bfchroma's options and blackfridayextensions and flags.

package mainimport ("github.com/Depado/bfchroma/v2""github.com/alecthomas/chroma/v2/formatters/html"bf"github.com/russross/blackfriday/v2")// Defines the extensions that are usedvarexts=bf.NoIntraEmphasis|bf.Tables|bf.FencedCode|bf.Autolink|bf.Strikethrough|bf.SpaceHeadings|bf.BackslashLineBreak|bf.DefinitionLists|bf.Footnotes// Defines the HTML rendering flags that are usedvarflags=bf.UseXHTML|bf.Smartypants|bf.SmartypantsFractions|bf.SmartypantsDashes|bf.SmartypantsLatexDashes|bf.TOC// render will take a []byte input and will render it using a new renderer each// time because reusing the same can mess with TOC and header IDsfuncrender(input []byte) []byte {returnbf.Run(input,bf.WithRenderer(bfchroma.NewRenderer(bfchroma.WithoutAutodetect(),bfchroma.ChromaOptions(html.WithLineNumbers(),),bfchroma.Extend(bf.NewHTMLRenderer(bf.HTMLRendererParameters{Flags:flags,}),),),),bf.WithExtensions(exts),)}

Classes

If you have loads of code in your markdown, you might want to consider usinghtml.WithClasses() in yourbfchroma.ChromaOptions(). The CSS of the styleyou chose can then be accessed like this :

r:=bfchroma.NewRenderer(bfchroma.WithoutAutodetect(),bfchroma.Extend(bf.NewHTMLRenderer(bf.HTMLRendererParameters{Flags:flags}),),bfchroma.Style("monokai"),bfchroma.ChromaOptions(html.WithClasses()),)varcss template.CSSb:=new(bytes.Buffer)iferr:=r.ChromaCSS(b);err!=nil {logrus.WithError(err).Warning("Couldn't write CSS")}css=template.CSS(b.String())bf.Run(input,bf.WithRenderer(r),bf.WithExtensions(exts))

This way, you can pass yourcss var to any template and render it along therendered markdown.


[8]ページ先頭

©2009-2025 Movatter.jp