From 7a15edafe240471c072d3548b72ccda0271ffd8f Mon Sep 17 00:00:00 2001 From: Helder Pereira Date: Sun, 22 Aug 2021 15:03:20 +0100 Subject: [PATCH] highlight: Add tabindex when code is not highlighted --- markup/goldmark/convert.go | 75 +++++++++++++++++---------------- markup/goldmark/convert_test.go | 6 +-- markup/highlight/highlight.go | 16 ++++--- 3 files changed, 53 insertions(+), 44 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(`
`)
-					highlight.WriteCodeTag(w, language)
-					return
-				}
-
-				w.WriteString(`
") - return } - - if !ctx.Highlighted() { - w.WriteString(`
`) - return - } - - w.WriteString("") }), ) } + +func writeDivStart(w util.BufWriter, ctx hl.CodeBlockContext) { + w.WriteString(`
") +} + +func writeDivEnd(w util.BufWriter) { + w.WriteString("
") +} diff --git a/markup/goldmark/convert_test.go b/markup/goldmark/convert_test.go index f2c6efedd..5fe6997e8 100644 --- a/markup/goldmark/convert_test.go +++ b/markup/goldmark/convert_test.go @@ -155,7 +155,7 @@ description // Code fences c.Assert(got, qt.Contains, "
LINE1\n
") - c.Assert(got, qt.Contains, "Code Fences No Lexer\n
LINE1\n
") + c.Assert(got, qt.Contains, "Code Fences No Lexer\n
LINE1\n
") // Extensions c.Assert(got, qt.Contains, `Autolink: https://gohugo.io/`) @@ -392,7 +392,7 @@ LINE5 c.Assert(result, qt.Equals, `
echo "Hugo Rocks!"
 
`) result = convertForConfig(c, cfg, `echo "Hugo Rocks!"`, "unknown") - c.Assert(result, qt.Equals, "
echo "Hugo Rocks!"\n
") + c.Assert(result, qt.Equals, "
echo "Hugo Rocks!"\n
") }) c.Run("Highlight lines, default config", func(c *qt.C) { @@ -443,7 +443,7 @@ LINE5 cfg.LineNumbersInTable = false result := convertForConfig(c, cfg, lines, "") - c.Assert(result, qt.Contains, "
LINE1\n")
+		c.Assert(result, qt.Contains, "
LINE1\n")
 	})
 
 	c.Run("No language, guess syntax", func(c *qt.C) {
diff --git a/markup/highlight/highlight.go b/markup/highlight/highlight.go
index 772244a91..319426241 100644
--- a/markup/highlight/highlight.go
+++ b/markup/highlight/highlight.go
@@ -122,17 +122,17 @@ type preWrapper struct {
 }
 
 func (p preWrapper) Start(code bool, styleAttr string) string {
-	w := &strings.Builder{}
-	fmt.Fprintf(w, `
`, styleAttr)
 	var language string
 	if code {
 		language = p.language
 	}
-	WriteCodeTag(w, language)
+	w := &strings.Builder{}
+	WritePreStart(w, language, styleAttr)
 	return w.String()
 }
 
-func WriteCodeTag(w io.Writer, language string) {
+func WritePreStart(w io.Writer, language, styleAttr string) {
+	fmt.Fprintf(w, `
`, styleAttr)
 	fmt.Fprint(w, "")
 }
 
+const preEnd = "
" + func (p preWrapper) End(code bool) string { - return "
" + return preEnd +} + +func WritePreEnd(w io.Writer) { + fmt.Fprint(w, preEnd) }