mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
highlight: Add tabindex when code is not highlighted
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
2f0945bafe
commit
7a15edafe2
@@ -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>")
|
||||
}
|
||||
|
Reference in New Issue
Block a user