Skip to content

Commit ec0a06e

Browse files
Upgrade chroma to v2.3.0 (#21259)
The behaviour of `PreventSurroundingPre` has changed in alecthomas/chroma#618 so that apparently it now causes line wrapper tags to be no longer emitted, but we need some form of indication to split the HTML into lines, so I did what yuin/goldmark-highlighting#33 did and added the `nopWrapper`. Maybe there are more elegant solutions but for some reason, just splitting the HTML string on `\n` did not work. Co-authored-by: wxiaoguang <[email protected]>
1 parent 2649e7f commit ec0a06e

File tree

7 files changed

+40
-43
lines changed

7 files changed

+40
-43
lines changed

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/42wim/sshsig v0.0.0-20211121163825-841cf5bbc121
1616
github.com/NYTimes/gziphandler v1.1.1
1717
github.com/PuerkitoBio/goquery v1.8.0
18-
github.com/alecthomas/chroma v0.10.0
18+
github.com/alecthomas/chroma/v2 v2.3.0
1919
github.com/blevesearch/bleve/v2 v2.3.2
2020
github.com/buildkite/terminal-to-html/v3 v3.7.0
2121
github.com/caddyserver/certmagic v0.17.0
@@ -89,8 +89,8 @@ require (
8989
github.com/urfave/cli v1.22.9
9090
github.com/xanzy/go-gitlab v0.73.1
9191
github.com/yohcop/openid-go v1.0.0
92-
github.com/yuin/goldmark v1.4.13
93-
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
92+
github.com/yuin/goldmark v1.4.15
93+
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20220924101305-151362477c87
9494
github.com/yuin/goldmark-meta v1.1.0
9595
go.jolheiser.com/hcaptcha v0.0.4
9696
go.jolheiser.com/pwn v0.0.3

go.sum

+9-7
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,12 @@ github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ
158158
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
159159
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
160160
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
161-
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
162-
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
161+
github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs=
162+
github.com/alecthomas/chroma/v2 v2.3.0 h1:83xfxrnjv8eK+Cf8qZDzNo3PPF9IbTWHs7z28GY6D0U=
163+
github.com/alecthomas/chroma/v2 v2.3.0/go.mod h1:mZxeWZlxP2Dy+/8cBob2PYd8O2DwNAzave5AY7A2eQw=
163164
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
165+
github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
166+
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
164167
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
165168
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
166169
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@@ -1467,11 +1470,10 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
14671470
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
14681471
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
14691472
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
1470-
github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
1471-
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
1472-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1473-
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 h1:yHfZyN55+5dp1wG7wDKv8HQ044moxkyGq12KFFMFDxg=
1474-
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594/go.mod h1:U9ihbh+1ZN7fR5Se3daSPoz1CGF9IYtSvWwVQtnzGHU=
1473+
github.com/yuin/goldmark v1.4.15 h1:CFa84T0goNn/UIXYS+dmjjVxMyTAvpOmzld40N/nfK0=
1474+
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1475+
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20220924101305-151362477c87 h1:Py16JEzkSdKAtEFJjiaYLYBOWGXc1r/xHj/Q/5lA37k=
1476+
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20220924101305-151362477c87/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
14751477
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
14761478
github.com/yuin/goldmark-meta v1.1.0/go.mod h1:U4spWENafuA7Zyg+Lj5RqK/MF+ovMYtBvXi1lBb2VP0=
14771479
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=

modules/highlight/highlight.go

+17-22
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/setting"
2121

22-
"github.com/alecthomas/chroma"
23-
"github.com/alecthomas/chroma/formatters/html"
24-
"github.com/alecthomas/chroma/lexers"
25-
"github.com/alecthomas/chroma/styles"
22+
"github.com/alecthomas/chroma/v2"
23+
"github.com/alecthomas/chroma/v2/formatters/html"
24+
"github.com/alecthomas/chroma/v2/lexers"
25+
"github.com/alecthomas/chroma/v2/styles"
2626
lru "github.com/hashicorp/golang-lru"
2727
)
2828

@@ -147,9 +147,6 @@ func File(fileName, language string, code []byte) ([]string, error) {
147147
html.PreventSurroundingPre(true),
148148
)
149149

150-
htmlBuf := bytes.Buffer{}
151-
htmlWriter := bufio.NewWriter(&htmlBuf)
152-
153150
var lexer chroma.Lexer
154151

155152
// provided language overrides everything
@@ -180,23 +177,21 @@ func File(fileName, language string, code []byte) ([]string, error) {
180177
return nil, fmt.Errorf("can't tokenize code: %w", err)
181178
}
182179

183-
err = formatter.Format(htmlWriter, styles.GitHub, iterator)
184-
if err != nil {
185-
return nil, fmt.Errorf("can't format code: %w", err)
186-
}
187-
188-
_ = htmlWriter.Flush()
180+
tokensLines := chroma.SplitTokensIntoLines(iterator.Tokens())
181+
htmlBuf := &bytes.Buffer{}
189182

190-
// at the moment, Chroma generates stable output `<span class="line"><span class="cl">...\n</span></span>` for each line
191-
htmlStr := htmlBuf.String()
192-
lines := strings.Split(htmlStr, `<span class="line"><span class="cl">`)
193-
m := make([]string, 0, len(lines))
194-
for i := 1; i < len(lines); i++ {
195-
line := lines[i]
196-
line = strings.TrimSuffix(line, "</span></span>")
197-
m = append(m, line)
183+
lines := make([]string, 0, len(tokensLines))
184+
for _, tokens := range tokensLines {
185+
iterator = chroma.Literator(tokens...)
186+
err = formatter.Format(htmlBuf, styles.GitHub, iterator)
187+
if err != nil {
188+
return nil, fmt.Errorf("can't format code: %w", err)
189+
}
190+
lines = append(lines, htmlBuf.String())
191+
htmlBuf.Reset()
198192
}
199-
return m, nil
193+
194+
return lines, nil
200195
}
201196

202197
// PlainText returns non-highlighted HTML for code

modules/markup/markdown/markdown.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"code.gitea.io/gitea/modules/setting"
1919
giteautil "code.gitea.io/gitea/modules/util"
2020

21-
chromahtml "github.com/alecthomas/chroma/formatters/html"
21+
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
2222
"github.com/yuin/goldmark"
23-
highlighting "github.com/yuin/goldmark-highlighting"
23+
highlighting "github.com/yuin/goldmark-highlighting/v2"
2424
meta "github.com/yuin/goldmark-meta"
2525
"github.com/yuin/goldmark/extension"
2626
"github.com/yuin/goldmark/parser"

modules/markup/orgmode/orgmode.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"code.gitea.io/gitea/modules/setting"
1818
"code.gitea.io/gitea/modules/util"
1919

20-
"github.com/alecthomas/chroma"
21-
"github.com/alecthomas/chroma/lexers"
20+
"github.com/alecthomas/chroma/v2"
21+
"github.com/alecthomas/chroma/v2/lexers"
2222
"github.com/niklasfasching/go-org/org"
2323
)
2424

modules/markup/orgmode/orgmode_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ func HelloWorld() {
7979
}
8080
#+end_src
8181
`, `<div class="src src-go">
82-
<pre><code class="chroma language-go"><span class="line"><span class="cl"><span class="c1">// HelloWorld prints &#34;Hello World&#34;
83-
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">func</span> <span class="nf">HelloWorld</span><span class="p">()</span> <span class="p">{</span>
84-
</span></span><span class="line"><span class="cl"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Hello World&#34;</span><span class="p">)</span>
85-
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
82+
<pre><code class="chroma language-go"><span class="c1">// HelloWorld prints &#34;Hello World&#34;
83+
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">HelloWorld</span><span class="p">()</span> <span class="p">{</span>
84+
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Hello World&#34;</span><span class="p">)</span>
85+
<span class="p">}</span></code></pre>
8686
</div>`)
8787
}

services/gitdiff/highlightdiff_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func TestDiffWithHighlight(t *testing.T) {
2121
" run(db)\n",
2222
)
2323

24-
expected := ` <span class="n">run</span><span class="o">(</span><span class="removed-code"><span class="k">&#39;</span><span class="o">&lt;</span><span class="o">&gt;</span><span class="k">&#39;</span></span><span class="o">)</span>` + "\n"
24+
expected := ` <span class="n">run</span><span class="o">(</span><span class="removed-code"><span class="k">&#39;</span><span class="o">&lt;</span><span class="o">&gt;</span><span class="k">&#39;</span></span><span class="o">)</span>`
2525
output := diffToHTML(nil, diffs, DiffLineDel)
2626
assert.Equal(t, expected, output)
2727

28-
expected = ` <span class="n">run</span><span class="o">(</span><span class="added-code"><span class="n">db</span></span><span class="o">)</span>` + "\n"
28+
expected = ` <span class="n">run</span><span class="o">(</span><span class="added-code"><span class="n">db</span></span><span class="o">)</span>`
2929
output = diffToHTML(nil, diffs, DiffLineAdd)
3030
assert.Equal(t, expected, output)
3131

@@ -57,7 +57,7 @@ func TestDiffWithHighlightPlaceholder(t *testing.T) {
5757
assert.Equal(t, "", hcd.placeholderTokenMap[0x00100000])
5858
assert.Equal(t, "", hcd.placeholderTokenMap[0x0010FFFD])
5959

60-
expected := fmt.Sprintf(`<span class="line"><span class="cl"><span class="nx">a</span><span class="o">=</span><span class="s1">&#39;</span><span class="removed-code">%s</span>&#39;</span></span>`, "\U00100000")
60+
expected := fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">&#39;</span><span class="removed-code">%s</span>&#39;`, "\U00100000")
6161
output := diffToHTML(hcd.lineWrapperTags, diffs, DiffLineDel)
6262
assert.Equal(t, expected, output)
6363

0 commit comments

Comments
 (0)