Skip to content
/ hugo Public
forked from gohugoio/hugo

Commit c08dabc

Browse files
committed
markup/highlight: Allow cfg.LineNos to be true, false, "inline", or "table"
Closes gohugoio#13481
1 parent eebea9e commit c08dabc

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

markup/highlight/config.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package highlight
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"strconv"
2021
"strings"
@@ -60,7 +61,7 @@ type Config struct {
6061
NoClasses bool
6162

6263
// When set, line numbers will be printed.
63-
LineNos bool
64+
LineNos any
6465
LineNumbersInTable bool
6566

6667
// When set, add links to line numbers
@@ -85,14 +86,37 @@ type Config struct {
8586
GuessSyntax bool
8687
}
8788

88-
func (cfg Config) toHTMLOptions() []html.Option {
89+
func (cfg Config) toHTMLOptions() ([]html.Option, error) {
8990
var lineAnchors string
9091
if cfg.LineAnchors != "" {
9192
lineAnchors = cfg.LineAnchors + "-"
9293
}
94+
95+
var lineNos bool
96+
err := errors.New(`lineNos must be one of true, false, "inline", or "table"`)
97+
switch v := cfg.LineNos.(type) {
98+
case string:
99+
switch v {
100+
case "inline":
101+
lineNos = true
102+
cfg.LineNumbersInTable = false
103+
case "table":
104+
lineNos = true
105+
cfg.LineNumbersInTable = true
106+
default:
107+
return nil, err
108+
}
109+
case bool:
110+
lineNos = v
111+
case nil:
112+
lineNos = false
113+
default:
114+
return nil, err
115+
}
116+
93117
options := []html.Option{
94118
html.TabWidth(cfg.TabWidth),
95-
html.WithLineNumbers(cfg.LineNos),
119+
html.WithLineNumbers(lineNos),
96120
html.BaseLineNumber(cfg.LineNoStart),
97121
html.LineNumbersInTable(cfg.LineNumbersInTable),
98122
html.WithClasses(!cfg.NoClasses),
@@ -117,7 +141,7 @@ func (cfg Config) toHTMLOptions() []html.Option {
117141
}
118142
}
119143

120-
return options
144+
return options, nil
121145
}
122146

123147
func applyOptions(opts any, cfg *Config) error {

markup/highlight/highlight.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ func highlight(fw hugio.FlexiWriter, code, lang string, attributes []attributes.
205205
writeDivStart(w, attributes, cfg.WrapperClass)
206206
}
207207

208-
options := cfg.toHTMLOptions()
208+
options, err := cfg.toHTMLOptions()
209+
if err != nil {
210+
return 0, 0, err
211+
}
212+
209213
var wrapper html.PreWrapper
210214

211215
if cfg.Hl_inline {

markup/highlight/highlight_integration_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
package highlight_test
1515

1616
import (
17+
"strings"
1718
"testing"
1819

20+
qt "github.com/frankban/quicktest"
1921
"github.com/gohugoio/hugo/hugolib"
2022
)
2123

@@ -129,3 +131,75 @@ xəx := 0
129131
<div class="highlight no-prose"><pre
130132
`)
131133
}
134+
135+
func TestHighlightLineNos(t *testing.T) {
136+
t.Parallel()
137+
c := qt.New(t)
138+
139+
files := `
140+
-- config.toml --
141+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
142+
[markup.highlight]
143+
noClasses = false
144+
lineNoStart = 42
145+
#LINENOS
146+
-- content/_index.md --
147+
---
148+
title: home
149+
---
150+
§§§go
151+
aaa
152+
§§§
153+
-- layouts/index.html --
154+
{{ .Content }}
155+
`
156+
157+
b := hugolib.Test(t, files)
158+
b.AssertFileContent("public/index.html",
159+
`<span class="nx">aaa</span>`,
160+
`! <table class="lntable">`,
161+
`! 42`,
162+
)
163+
164+
f := strings.ReplaceAll(files, "#LINENOS", `linenos = false`)
165+
b = hugolib.Test(t, f)
166+
b.AssertFileContent("public/index.html",
167+
`<span class="nx">aaa</span>`,
168+
`! <table class="lntable">`,
169+
`! 42`,
170+
)
171+
172+
f = strings.ReplaceAll(files, "#LINENOS", `linenos = true`)
173+
b = hugolib.Test(t, f)
174+
b.AssertFileContent("public/index.html",
175+
`<span class="nx">aaa</span>`,
176+
`<table class="lntable">`,
177+
`42`,
178+
)
179+
180+
f = strings.ReplaceAll(files, "#LINENOS", `linenos = "table"`)
181+
b = hugolib.Test(t, f)
182+
b.AssertFileContent("public/index.html",
183+
`<span class="nx">aaa</span>`,
184+
`<table class="lntable">`,
185+
`42`,
186+
)
187+
188+
f = strings.ReplaceAll(files, "#LINENOS", `linenos = "inline"`)
189+
b = hugolib.Test(t, f)
190+
b.AssertFileContent("public/index.html",
191+
`<span class="nx">aaa</span>`,
192+
`! <table class="lntable">`,
193+
`42`,
194+
)
195+
196+
want := `.* lineNos must be one of .*`
197+
198+
f = strings.ReplaceAll(files, "#LINENOS", `linenos = "foo"`)
199+
b, err := hugolib.TestE(c, f)
200+
b.Assert(err, qt.ErrorMatches, want)
201+
202+
f = strings.ReplaceAll(files, "#LINENOS", `linenos = 123`)
203+
b, err = hugolib.TestE(c, f)
204+
b.Assert(err, qt.ErrorMatches, want)
205+
}

0 commit comments

Comments
 (0)