Skip to content

Commit 7a15eda

Browse files
helfperbep
authored andcommitted
highlight: Add tabindex when code is not highlighted
1 parent 2f0945b commit 7a15eda

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

markup/goldmark/convert.go

+37-34
Original file line numberDiff line numberDiff line change
@@ -310,49 +310,52 @@ func newHighlighting(cfg highlight.Config) goldmark.Extender {
310310
),
311311

312312
hl.WithWrapperRenderer(func(w util.BufWriter, ctx hl.CodeBlockContext, entering bool) {
313-
l, hasLang := ctx.Language()
314313
var language string
315-
if hasLang {
314+
if l, hasLang := ctx.Language(); hasLang {
316315
language = string(l)
317316
}
318317

319-
if entering {
320-
if !ctx.Highlighted() {
321-
w.WriteString(`<pre>`)
322-
highlight.WriteCodeTag(w, language)
323-
return
318+
if ctx.Highlighted() {
319+
if entering {
320+
writeDivStart(w, ctx)
321+
} else {
322+
writeDivEnd(w)
324323
}
325-
326-
w.WriteString(`<div class="highlight`)
327-
328-
var attributes []ast.Attribute
329-
if ctx.Attributes() != nil {
330-
attributes = ctx.Attributes().All()
324+
} else {
325+
if entering {
326+
highlight.WritePreStart(w, language, "")
327+
} else {
328+
highlight.WritePreEnd(w)
331329
}
330+
}
331+
}),
332+
)
333+
}
332334

333-
if attributes != nil {
334-
class, found := ctx.Attributes().GetString("class")
335-
if found {
336-
w.WriteString(" ")
337-
w.Write(util.EscapeHTML(class.([]byte)))
335+
func writeDivStart(w util.BufWriter, ctx hl.CodeBlockContext) {
336+
w.WriteString(`<div class="highlight`)
338337

339-
}
340-
_, _ = w.WriteString("\"")
341-
renderAttributes(w, true, attributes...)
342-
} else {
343-
_, _ = w.WriteString("\"")
344-
}
338+
var attributes []ast.Attribute
339+
if ctx.Attributes() != nil {
340+
attributes = ctx.Attributes().All()
341+
}
345342

346-
w.WriteString(">")
347-
return
348-
}
343+
if attributes != nil {
344+
class, found := ctx.Attributes().GetString("class")
345+
if found {
346+
w.WriteString(" ")
347+
w.Write(util.EscapeHTML(class.([]byte)))
349348

350-
if !ctx.Highlighted() {
351-
w.WriteString(`</code></pre>`)
352-
return
353-
}
349+
}
350+
_, _ = w.WriteString("\"")
351+
renderAttributes(w, true, attributes...)
352+
} else {
353+
_, _ = w.WriteString("\"")
354+
}
354355

355-
w.WriteString("</div>")
356-
}),
357-
)
356+
w.WriteString(">")
357+
}
358+
359+
func writeDivEnd(w util.BufWriter) {
360+
w.WriteString("</div>")
358361
}

markup/goldmark/convert_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ description
155155

156156
// Code fences
157157
c.Assert(got, qt.Contains, "<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\">LINE1\n</code></pre></div>")
158-
c.Assert(got, qt.Contains, "Code Fences No Lexer</h2>\n<pre><code class=\"language-moo\" data-lang=\"moo\">LINE1\n</code></pre>")
158+
c.Assert(got, qt.Contains, "Code Fences No Lexer</h2>\n<pre tabindex=\"0\"><code class=\"language-moo\" data-lang=\"moo\">LINE1\n</code></pre>")
159159

160160
// Extensions
161161
c.Assert(got, qt.Contains, `Autolink: <a href="https://gohugo.io/">https://gohugo.io/</a>`)
@@ -392,7 +392,7 @@ LINE5
392392
c.Assert(result, qt.Equals, `<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">echo</span> <span class="s2">&#34;Hugo Rocks!&#34;</span>
393393
</code></pre></div>`)
394394
result = convertForConfig(c, cfg, `echo "Hugo Rocks!"`, "unknown")
395-
c.Assert(result, qt.Equals, "<pre><code class=\"language-unknown\" data-lang=\"unknown\">echo &quot;Hugo Rocks!&quot;\n</code></pre>")
395+
c.Assert(result, qt.Equals, "<pre tabindex=\"0\"><code class=\"language-unknown\" data-lang=\"unknown\">echo &quot;Hugo Rocks!&quot;\n</code></pre>")
396396
})
397397

398398
c.Run("Highlight lines, default config", func(c *qt.C) {
@@ -443,7 +443,7 @@ LINE5
443443
cfg.LineNumbersInTable = false
444444

445445
result := convertForConfig(c, cfg, lines, "")
446-
c.Assert(result, qt.Contains, "<pre><code>LINE1\n")
446+
c.Assert(result, qt.Contains, "<pre tabindex=\"0\"><code>LINE1\n")
447447
})
448448

449449
c.Run("No language, guess syntax", func(c *qt.C) {

markup/highlight/highlight.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,17 @@ type preWrapper struct {
122122
}
123123

124124
func (p preWrapper) Start(code bool, styleAttr string) string {
125-
w := &strings.Builder{}
126-
fmt.Fprintf(w, `<pre tabindex="0"%s>`, styleAttr)
127125
var language string
128126
if code {
129127
language = p.language
130128
}
131-
WriteCodeTag(w, language)
129+
w := &strings.Builder{}
130+
WritePreStart(w, language, styleAttr)
132131
return w.String()
133132
}
134133

135-
func WriteCodeTag(w io.Writer, language string) {
134+
func WritePreStart(w io.Writer, language, styleAttr string) {
135+
fmt.Fprintf(w, `<pre tabindex="0"%s>`, styleAttr)
136136
fmt.Fprint(w, "<code")
137137
if language != "" {
138138
fmt.Fprint(w, ` class="language-`+language+`"`)
@@ -141,6 +141,12 @@ func WriteCodeTag(w io.Writer, language string) {
141141
fmt.Fprint(w, ">")
142142
}
143143

144+
const preEnd = "</code></pre>"
145+
144146
func (p preWrapper) End(code bool) string {
145-
return "</code></pre>"
147+
return preEnd
148+
}
149+
150+
func WritePreEnd(w io.Writer) {
151+
fmt.Fprint(w, preEnd)
146152
}

0 commit comments

Comments
 (0)