mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 06:00:25 +03:00
Use Chroma as new default syntax highlighter
If you want to use Pygments, set `pygmentsUseClassic=true` in your site config. Fixes #3888
This commit is contained in:
parent
81ed564793
commit
fb33d8286d
18 changed files with 652 additions and 108 deletions
|
@ -48,19 +48,49 @@ type ContentSpec struct {
|
|||
footnoteAnchorPrefix string
|
||||
footnoteReturnLinkContents string
|
||||
|
||||
Highlight func(code, lang, optsStr string) (string, error)
|
||||
defatultPygmentsOpts map[string]string
|
||||
|
||||
cfg config.Provider
|
||||
}
|
||||
|
||||
// NewContentSpec returns a ContentSpec initialized
|
||||
// with the appropriate fields from the given config.Provider.
|
||||
func NewContentSpec(cfg config.Provider) *ContentSpec {
|
||||
return &ContentSpec{
|
||||
func NewContentSpec(cfg config.Provider) (*ContentSpec, error) {
|
||||
spec := &ContentSpec{
|
||||
blackfriday: cfg.GetStringMap("blackfriday"),
|
||||
footnoteAnchorPrefix: cfg.GetString("footnoteAnchorPrefix"),
|
||||
footnoteReturnLinkContents: cfg.GetString("footnoteReturnLinkContents"),
|
||||
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
// Highlighting setup
|
||||
options, err := parseDefaultPygmentsOpts(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
spec.defatultPygmentsOpts = options
|
||||
|
||||
// Use the Pygmentize on path if present
|
||||
useClassic := false
|
||||
h := newHiglighters(spec)
|
||||
|
||||
if cfg.GetBool("pygmentsUseClassic") {
|
||||
if !hasPygments() {
|
||||
jww.WARN.Println("Highlighting with pygmentsUseClassic set requires Pygments to be installed and in the path")
|
||||
} else {
|
||||
useClassic = true
|
||||
}
|
||||
}
|
||||
|
||||
if useClassic {
|
||||
spec.Highlight = h.pygmentsHighlight
|
||||
} else {
|
||||
spec.Highlight = h.chromaHighlight
|
||||
}
|
||||
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
// Blackfriday holds configuration values for Blackfriday rendering.
|
||||
|
@ -198,7 +228,7 @@ func BytesToHTML(b []byte) template.HTML {
|
|||
}
|
||||
|
||||
// getHTMLRenderer creates a new Blackfriday HTML Renderer with the given configuration.
|
||||
func (c ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Renderer {
|
||||
func (c *ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Renderer {
|
||||
renderParameters := blackfriday.HtmlRendererParameters{
|
||||
FootnoteAnchorPrefix: c.footnoteAnchorPrefix,
|
||||
FootnoteReturnLinkContents: c.footnoteReturnLinkContents,
|
||||
|
@ -248,6 +278,7 @@ func (c ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) bl
|
|||
}
|
||||
|
||||
return &HugoHTMLRenderer{
|
||||
cs: c,
|
||||
RenderingContext: ctx,
|
||||
Renderer: blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
|
||||
}
|
||||
|
@ -299,7 +330,7 @@ func (c ContentSpec) markdownRender(ctx *RenderingContext) []byte {
|
|||
}
|
||||
|
||||
// getMmarkHTMLRenderer creates a new mmark HTML Renderer with the given configuration.
|
||||
func (c ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContext) mmark.Renderer {
|
||||
func (c *ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContext) mmark.Renderer {
|
||||
renderParameters := mmark.HtmlRendererParameters{
|
||||
FootnoteAnchorPrefix: c.footnoteAnchorPrefix,
|
||||
FootnoteReturnLinkContents: c.footnoteReturnLinkContents,
|
||||
|
@ -320,8 +351,9 @@ func (c ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContex
|
|||
htmlFlags |= mmark.HTML_FOOTNOTE_RETURN_LINKS
|
||||
|
||||
return &HugoMmarkHTMLRenderer{
|
||||
mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
|
||||
c.cfg,
|
||||
cs: c,
|
||||
Renderer: mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
|
||||
Cfg: c.cfg,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue