mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-15 02:44:04 +00:00
highlight: Add tabindex when code is not highlighted
This commit is contained in:
parent
2f0945bafe
commit
7a15edafe2
3 changed files with 53 additions and 44 deletions
|
@ -310,49 +310,52 @@ func newHighlighting(cfg highlight.Config) goldmark.Extender {
|
||||||
),
|
),
|
||||||
|
|
||||||
hl.WithWrapperRenderer(func(w util.BufWriter, ctx hl.CodeBlockContext, entering bool) {
|
hl.WithWrapperRenderer(func(w util.BufWriter, ctx hl.CodeBlockContext, entering bool) {
|
||||||
l, hasLang := ctx.Language()
|
|
||||||
var language string
|
var language string
|
||||||
if hasLang {
|
if l, hasLang := ctx.Language(); hasLang {
|
||||||
language = string(l)
|
language = string(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
if entering {
|
if ctx.Highlighted() {
|
||||||
if !ctx.Highlighted() {
|
if entering {
|
||||||
w.WriteString(`<pre>`)
|
writeDivStart(w, ctx)
|
||||||
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...)
|
|
||||||
} else {
|
} 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>")
|
||||||
|
}
|
||||||
|
|
|
@ -155,7 +155,7 @@ description
|
||||||
|
|
||||||
// Code fences
|
// Code fences
|
||||||
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>")
|
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>")
|
||||||
c.Assert(got, qt.Contains, "Code Fences No Lexer</h2>\n<pre><code class=\"language-moo\" data-lang=\"moo\">LINE1\n</code></pre>")
|
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>")
|
||||||
|
|
||||||
// Extensions
|
// Extensions
|
||||||
c.Assert(got, qt.Contains, `Autolink: <a href="https://gohugo.io/">https://gohugo.io/</a>`)
|
c.Assert(got, qt.Contains, `Autolink: <a href="https://gohugo.io/">https://gohugo.io/</a>`)
|
||||||
|
@ -392,7 +392,7 @@ LINE5
|
||||||
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">"Hugo Rocks!"</span>
|
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">"Hugo Rocks!"</span>
|
||||||
</code></pre></div>`)
|
</code></pre></div>`)
|
||||||
result = convertForConfig(c, cfg, `echo "Hugo Rocks!"`, "unknown")
|
result = convertForConfig(c, cfg, `echo "Hugo Rocks!"`, "unknown")
|
||||||
c.Assert(result, qt.Equals, "<pre><code class=\"language-unknown\" data-lang=\"unknown\">echo "Hugo Rocks!"\n</code></pre>")
|
c.Assert(result, qt.Equals, "<pre tabindex=\"0\"><code class=\"language-unknown\" data-lang=\"unknown\">echo "Hugo Rocks!"\n</code></pre>")
|
||||||
})
|
})
|
||||||
|
|
||||||
c.Run("Highlight lines, default config", func(c *qt.C) {
|
c.Run("Highlight lines, default config", func(c *qt.C) {
|
||||||
|
@ -443,7 +443,7 @@ LINE5
|
||||||
cfg.LineNumbersInTable = false
|
cfg.LineNumbersInTable = false
|
||||||
|
|
||||||
result := convertForConfig(c, cfg, lines, "")
|
result := convertForConfig(c, cfg, lines, "")
|
||||||
c.Assert(result, qt.Contains, "<pre><code>LINE1\n")
|
c.Assert(result, qt.Contains, "<pre tabindex=\"0\"><code>LINE1\n")
|
||||||
})
|
})
|
||||||
|
|
||||||
c.Run("No language, guess syntax", func(c *qt.C) {
|
c.Run("No language, guess syntax", func(c *qt.C) {
|
||||||
|
|
|
@ -122,17 +122,17 @@ type preWrapper struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p preWrapper) Start(code bool, styleAttr string) string {
|
func (p preWrapper) Start(code bool, styleAttr string) string {
|
||||||
w := &strings.Builder{}
|
|
||||||
fmt.Fprintf(w, `<pre tabindex="0"%s>`, styleAttr)
|
|
||||||
var language string
|
var language string
|
||||||
if code {
|
if code {
|
||||||
language = p.language
|
language = p.language
|
||||||
}
|
}
|
||||||
WriteCodeTag(w, language)
|
w := &strings.Builder{}
|
||||||
|
WritePreStart(w, language, styleAttr)
|
||||||
return w.String()
|
return w.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteCodeTag(w io.Writer, language string) {
|
func WritePreStart(w io.Writer, language, styleAttr string) {
|
||||||
|
fmt.Fprintf(w, `<pre tabindex="0"%s>`, styleAttr)
|
||||||
fmt.Fprint(w, "<code")
|
fmt.Fprint(w, "<code")
|
||||||
if language != "" {
|
if language != "" {
|
||||||
fmt.Fprint(w, ` class="language-`+language+`"`)
|
fmt.Fprint(w, ` class="language-`+language+`"`)
|
||||||
|
@ -141,6 +141,12 @@ func WriteCodeTag(w io.Writer, language string) {
|
||||||
fmt.Fprint(w, ">")
|
fmt.Fprint(w, ">")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const preEnd = "</code></pre>"
|
||||||
|
|
||||||
func (p preWrapper) End(code bool) string {
|
func (p preWrapper) End(code bool) string {
|
||||||
return "</code></pre>"
|
return preEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
func WritePreEnd(w io.Writer) {
|
||||||
|
fmt.Fprint(w, preEnd)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue