highlight: support highlighting lines in the default writer

The hl_lines key is specifically the same as Hugo's key.
This commit is contained in:
Kisaragi Hiu 2022-07-18 03:56:15 +09:00
parent 9b56fc914c
commit ab8d3bc16a
No known key found for this signature in database
GPG key ID: 40ECBEAEA8775FC2
6 changed files with 97 additions and 2 deletions

View file

@ -63,7 +63,14 @@ func highlightCodeBlock(source, lang string, inline bool, params map[string]stri
}
l = chroma.Coalesce(l)
it, _ := l.Tokenise(nil, source)
_ = html.New().Format(&w, styles.Get("github"), it)
options := []html.Option{}
if params[":hl_lines"] != "" {
ranges := org.ParseRanges(params[":hl_lines"])
if ranges != nil {
options = append(options, html.HighlightLines(ranges))
}
}
_ = html.New(options...).Format(&w, styles.Get("github"), it)
if inline {
return `<div class="highlight-inline">` + "\n" + w.String() + "\n" + `</div>`
}