From 19d5ee688c20e03921137a36d78d113ec2d68dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20B=C4=83lu=C8=9B?= Date: Wed, 18 Dec 2019 16:57:09 +0100 Subject: [PATCH] Highlight the line number targeted by the URL anchor This currently works only when `html.WithClasses(true)` is used. The same colors as for the pre-highlighted lines are used. Only the line number is highlighted, so difficult to notice, but better than nothing. --- formatters/html/html.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/formatters/html/html.go b/formatters/html/html.go index 76697c0..d3fef2e 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -345,6 +345,13 @@ func (f *Formatter) WriteCSS(w io.Writer, style *chroma.Style) error { return err } } + // Special-case line number highlighting when targeted. + if f.lineNumbers || f.lineNumbersInTable { + targetedLineCSS := StyleEntryToCSS(style.Get(chroma.LineHighlight)) + for _, tt := range []chroma.TokenType{chroma.LineNumbers, chroma.LineNumbersTable} { + fmt.Fprintf(w, "/* %s targeted by URL anchor */ .%schroma .%s:target { %s }\n", tt, f.prefix, f.class(tt), targetedLineCSS) + } + } tts := []int{} for tt := range css { tts = append(tts, int(tt))