markup/highlight: Add support to linkable line anchors on Chroma

Fixes #7622
This commit is contained in:
Fernando Jorge Mota 2020-09-13 06:00:16 -03:00 committed by GitHub
parent 748fd4cb0d
commit fb0f2cc718
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View file

@ -43,6 +43,8 @@ Options:
* `linenos`: configure line numbers. Valid values are `true`, `false`, `table`, or `inline`. `false` will turn off line numbers if it's configured to be on in site config. {{< new-in "0.60.0" >}} `table` will give copy-and-paste friendly code blocks. * `linenos`: configure line numbers. Valid values are `true`, `false`, `table`, or `inline`. `false` will turn off line numbers if it's configured to be on in site config. {{< new-in "0.60.0" >}} `table` will give copy-and-paste friendly code blocks.
* `hl_lines`: lists a set of line numbers or line number ranges to be highlighted. * `hl_lines`: lists a set of line numbers or line number ranges to be highlighted.
* `linenostart=199`: starts the line number count from 199. * `linenostart=199`: starts the line number count from 199.
* `anchorlinenos`: Configure anchors on line numbers. Valid values are `true` or `false`;
* `lineanchors`: Configure a prefix for the anchors on line numbers. Will be suffixed with `-`, so linking to the line number 1 with the option `lineanchors=prefix` adds the anchor `prefix-1` to the page.
### Example: Highlight Shortcode ### Example: Highlight Shortcode

View file

@ -50,6 +50,10 @@ type Config struct {
LineNos bool LineNos bool
LineNumbersInTable bool LineNumbersInTable bool
// When set, add links to line numbers
AnchorLineNos bool
LineAnchors string
// Start the line numbers from this value (default is 1). // Start the line numbers from this value (default is 1).
LineNoStart int LineNoStart int
@ -63,12 +67,17 @@ type Config struct {
} }
func (cfg Config) ToHTMLOptions() []html.Option { func (cfg Config) ToHTMLOptions() []html.Option {
var lineAnchors string
if cfg.LineAnchors != "" {
lineAnchors = cfg.LineAnchors + "-"
}
var options = []html.Option{ var options = []html.Option{
html.TabWidth(cfg.TabWidth), html.TabWidth(cfg.TabWidth),
html.WithLineNumbers(cfg.LineNos), html.WithLineNumbers(cfg.LineNos),
html.BaseLineNumber(cfg.LineNoStart), html.BaseLineNumber(cfg.LineNoStart),
html.LineNumbersInTable(cfg.LineNumbersInTable), html.LineNumbersInTable(cfg.LineNumbersInTable),
html.WithClasses(!cfg.NoClasses), html.WithClasses(!cfg.NoClasses),
html.LinkableLineNumbers(cfg.AnchorLineNos, lineAnchors),
} }
if cfg.Hl_Lines != "" { if cfg.Hl_Lines != "" {

View file

@ -79,6 +79,21 @@ User-Agent: foo
c.Assert(result, qt.Not(qt.Contains), "class=\"lnt\"") 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) { c.Run("Highlight lines, linenumbers default on, linenumbers in table default off", func(c *qt.C) {
cfg := DefaultConfig cfg := DefaultConfig
cfg.NoClasses = false cfg.NoClasses = false