diff options
author | Helder Pereira <helfper@gmail.com> | 2021-08-22 15:03:20 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-08-23 12:28:45 +0200 |
commit | 7a15edafe240471c072d3548b72ccda0271ffd8f (patch) | |
tree | 19617e70943aab2f49f5943bb3c3030c9c4cb4b9 /markup/goldmark/convert.go | |
parent | 2f0945bafe501103abe97b2f2b5566b28ec48e52 (diff) |
highlight: Add tabindex when code is not highlighted
Diffstat (limited to 'markup/goldmark/convert.go')
-rw-r--r-- | markup/goldmark/convert.go | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go index 639fddace..dcaf8d3e1 100644 --- a/markup/goldmark/convert.go +++ b/markup/goldmark/convert.go @@ -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 + if ctx.Highlighted() { + if entering { + writeDivStart(w, ctx) + } else { + writeDivEnd(w) } - - w.WriteString(`<div class="highlight`) - - var attributes []ast.Attribute - if ctx.Attributes() != nil { - attributes = ctx.Attributes().All() + } else { + if entering { + highlight.WritePreStart(w, language, "") + } else { + highlight.WritePreEnd(w) } + } + }), + ) +} - if attributes != nil { - class, found := ctx.Attributes().GetString("class") - if found { - w.WriteString(" ") - w.Write(util.EscapeHTML(class.([]byte))) +func writeDivStart(w util.BufWriter, ctx hl.CodeBlockContext) { + w.WriteString(`<div class="highlight`) - } - _, _ = w.WriteString("\"") - renderAttributes(w, true, attributes...) - } else { - _, _ = w.WriteString("\"") - } + var attributes []ast.Attribute + if ctx.Attributes() != nil { + attributes = ctx.Attributes().All() + } - w.WriteString(">") - return - } + if attributes != nil { + class, found := ctx.Attributes().GetString("class") + if found { + w.WriteString(" ") + w.Write(util.EscapeHTML(class.([]byte))) - if !ctx.Highlighted() { - w.WriteString(`</code></pre>`) - return - } + } + _, _ = w.WriteString("\"") + renderAttributes(w, true, attributes...) + } else { + _, _ = w.WriteString("\"") + } - w.WriteString("</div>") - }), - ) + w.WriteString(">") +} + +func writeDivEnd(w util.BufWriter) { + w.WriteString("</div>") } |