Skip to content

Commit 623d2dd

Browse files
authored
Prevent panic in Org mode HighlightCodeBlock (#17140)
When rendering source in org mode there is a mistake in the highlight code that causes a panic. This PR fixes this. Fix #17139 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 5842a55 commit 623d2dd

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

modules/highlight/highlight.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ func Code(fileName, code string) string {
6666
if len(code) > sizeLimit {
6767
return code
6868
}
69-
formatter := html.New(html.WithClasses(true),
70-
html.WithLineNumbers(false),
71-
html.PreventSurroundingPre(true),
72-
)
73-
if formatter == nil {
74-
log.Error("Couldn't create chroma formatter")
75-
return code
76-
}
77-
78-
htmlbuf := bytes.Buffer{}
79-
htmlw := bufio.NewWriter(&htmlbuf)
8069

8170
var lexer chroma.Lexer
8271
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
@@ -97,6 +86,18 @@ func Code(fileName, code string) string {
9786
}
9887
cache.Add(fileName, lexer)
9988
}
89+
return CodeFromLexer(lexer, code)
90+
}
91+
92+
// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
93+
func CodeFromLexer(lexer chroma.Lexer, code string) string {
94+
formatter := html.New(html.WithClasses(true),
95+
html.WithLineNumbers(false),
96+
html.PreventSurroundingPre(true),
97+
)
98+
99+
htmlbuf := bytes.Buffer{}
100+
htmlw := bufio.NewWriter(&htmlbuf)
100101

101102
iterator, err := lexer.Tokenise(nil, string(code))
102103
if err != nil {

modules/markup/orgmode/orgmode.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"code.gitea.io/gitea/modules/highlight"
15+
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/markup"
1617
"code.gitea.io/gitea/modules/setting"
1718
"code.gitea.io/gitea/modules/util"
@@ -51,6 +52,12 @@ func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
5152
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
5253
htmlWriter := org.NewHTMLWriter()
5354
htmlWriter.HighlightCodeBlock = func(source, lang string, inline bool) string {
55+
defer func() {
56+
if err := recover(); err != nil {
57+
log.Error("Panic in HighlightCodeBlock: %v\n%s", err, log.Stack(2))
58+
panic(err)
59+
}
60+
}()
5461
var w strings.Builder
5562
if _, err := w.WriteString(`<pre>`); err != nil {
5663
return ""
@@ -80,7 +87,7 @@ func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
8087
}
8188
lexer = chroma.Coalesce(lexer)
8289

83-
if _, err := w.WriteString(highlight.Code(lexer.Config().Filenames[0], source)); err != nil {
90+
if _, err := w.WriteString(highlight.CodeFromLexer(lexer, source)); err != nil {
8491
return ""
8592
}
8693
}

modules/markup/orgmode/orgmode_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,29 @@ func TestRender_Images(t *testing.T) {
5757
test("[[file:"+url+"]]",
5858
"<p><img src=\""+result+"\" alt=\""+result+"\" title=\""+result+"\" /></p>")
5959
}
60+
61+
func TestRender_Source(t *testing.T) {
62+
setting.AppURL = AppURL
63+
setting.AppSubURL = AppSubURL
64+
65+
test := func(input, expected string) {
66+
buffer, err := RenderString(&markup.RenderContext{
67+
URLPrefix: setting.AppSubURL,
68+
}, input)
69+
assert.NoError(t, err)
70+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
71+
}
72+
73+
test(`#+begin_src go
74+
// HelloWorld prints "Hello World"
75+
func HelloWorld() {
76+
fmt.Println("Hello World")
77+
}
78+
#+end_src
79+
`, `<div class="src src-go">
80+
<pre><code class="chroma language-go"><span class="c1">// HelloWorld prints &#34;Hello World&#34;
81+
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">HelloWorld</span><span class="p">()</span> <span class="p">{</span>
82+
<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>
83+
<span class="p">}</span></code></pre>
84+
</div>`)
85+
}

0 commit comments

Comments
 (0)