highlight: Add tabindex when code is not highlighted

This commit is contained in:
Helder Pereira
2021-08-22 15:03:20 +01:00
committed by Bjørn Erik Pedersen
parent 2f0945bafe
commit 7a15edafe2
3 changed files with 53 additions and 44 deletions

View File

@@ -310,49 +310,52 @@ func newHighlighting(cfg highlight.Config) goldmark.Extender {
),
hl.WithWrapperRenderer(func(w util.BufWriter, ctx hl.CodeBlockContext, entering bool) {
l, hasLang := ctx.Language()
var language string
if hasLang {
if l, hasLang := ctx.Language(); hasLang {
language = string(l)
}
if entering {
if !ctx.Highlighted() {
w.WriteString(`<pre>`)
highlight.WriteCodeTag(w, language)
return
}
w.WriteString(`<div class="highlight`)
var attributes []ast.Attribute
if ctx.Attributes() != nil {
attributes = ctx.Attributes().All()
}
if attributes != nil {
class, found := ctx.Attributes().GetString("class")
if found {
w.WriteString(" ")
w.Write(util.EscapeHTML(class.([]byte)))
}
_, _ = w.WriteString("\"")
renderAttributes(w, true, attributes...)
if ctx.Highlighted() {
if entering {
writeDivStart(w, ctx)
} else {
_, _ = w.WriteString("\"")
writeDivEnd(w)
}
} else {
if entering {
highlight.WritePreStart(w, language, "")
} else {
highlight.WritePreEnd(w)
}
w.WriteString(">")
return
}
if !ctx.Highlighted() {
w.WriteString(`</code></pre>`)
return
}
w.WriteString("</div>")
}),
)
}
func writeDivStart(w util.BufWriter, ctx hl.CodeBlockContext) {
w.WriteString(`<div class="highlight`)
var attributes []ast.Attribute
if ctx.Attributes() != nil {
attributes = ctx.Attributes().All()
}
if attributes != nil {
class, found := ctx.Attributes().GetString("class")
if found {
w.WriteString(" ")
w.Write(util.EscapeHTML(class.([]byte)))
}
_, _ = w.WriteString("\"")
renderAttributes(w, true, attributes...)
} else {
_, _ = w.WriteString("\"")
}
w.WriteString(">")
}
func writeDivEnd(w util.BufWriter) {
w.WriteString("</div>")
}