diff options
author | Fernando Jorge Mota <fjorgemota@users.noreply.github.com> | 2020-09-13 06:00:16 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-13 11:00:16 +0200 |
commit | fb0f2cc718a54fd0774a0367e0a60718b5731de5 (patch) | |
tree | fe5a09fa587c9e967e92670afec4fa46517a6548 /markup/highlight | |
parent | 748fd4cb0d083de7c173d4b04b874358750fc900 (diff) |
markup/highlight: Add support to linkable line anchors on Chroma
Fixes #7622
Diffstat (limited to 'markup/highlight')
-rw-r--r-- | markup/highlight/config.go | 9 | ||||
-rw-r--r-- | markup/highlight/highlight_test.go | 15 |
2 files changed, 24 insertions, 0 deletions
diff --git a/markup/highlight/config.go b/markup/highlight/config.go index 3f31e65ea..a7c5ab4cb 100644 --- a/markup/highlight/config.go +++ b/markup/highlight/config.go @@ -50,6 +50,10 @@ type Config struct { LineNos bool LineNumbersInTable bool + // When set, add links to line numbers + AnchorLineNos bool + LineAnchors string + // Start the line numbers from this value (default is 1). LineNoStart int @@ -63,12 +67,17 @@ type Config struct { } func (cfg Config) ToHTMLOptions() []html.Option { + var lineAnchors string + if cfg.LineAnchors != "" { + lineAnchors = cfg.LineAnchors + "-" + } var options = []html.Option{ html.TabWidth(cfg.TabWidth), html.WithLineNumbers(cfg.LineNos), html.BaseLineNumber(cfg.LineNoStart), html.LineNumbersInTable(cfg.LineNumbersInTable), html.WithClasses(!cfg.NoClasses), + html.LinkableLineNumbers(cfg.AnchorLineNos, lineAnchors), } if cfg.Hl_Lines != "" { diff --git a/markup/highlight/highlight_test.go b/markup/highlight/highlight_test.go index 308679263..f5992a512 100644 --- a/markup/highlight/highlight_test.go +++ b/markup/highlight/highlight_test.go @@ -79,6 +79,21 @@ User-Agent: foo c.Assert(result, qt.Not(qt.Contains), "class=\"lnt\"") }) + c.Run("Highlight lines, linenumbers default on, anchorlinenumbers default on", func(c *qt.C) { + cfg := DefaultConfig + cfg.NoClasses = false + cfg.LineNos = true + cfg.AnchorLineNos = true + h := New(cfg) + + result, _ := h.Highlight(lines, "bash", "") + c.Assert(result, qt.Contains, "<span class=\"lnt\" id=\"2\">2\n</span>") + result, _ = h.Highlight(lines, "bash", "lineanchors=test") + c.Assert(result, qt.Contains, "<span class=\"lnt\" id=\"test-2\">2\n</span>") + result, _ = h.Highlight(lines, "bash", "anchorlinenos=false,hl_lines=2") + c.Assert(result, qt.Not(qt.Contains), "id=\"2\"") + }) + c.Run("Highlight lines, linenumbers default on, linenumbers in table default off", func(c *qt.C) { cfg := DefaultConfig cfg.NoClasses = false |