Skip to content

Commit 3a4fcf1

Browse files
committed
Fix missed orgmode code block hightlight
1 parent 7576e37 commit 3a4fcf1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

modules/markup/orgmode/orgmode.go

+24
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"html"
1111
"strings"
1212

13+
"code.gitea.io/gitea/modules/highlight"
1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/markup"
1516
"code.gitea.io/gitea/modules/util"
1617

18+
"github.com/alecthomas/chroma/lexers"
1719
"github.com/niklasfasching/go-org/org"
1820
)
1921

@@ -38,6 +40,28 @@ func (Parser) Extensions() []string {
3840
// Render renders orgmode rawbytes to HTML
3941
func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
4042
htmlWriter := org.NewHTMLWriter()
43+
htmlWriter.HighlightCodeBlock = func(source, lang string, inline bool) string {
44+
var w strings.Builder
45+
if _, err := w.WriteString(`<pre>`); err != nil {
46+
return ""
47+
}
48+
49+
// include language-x class as part of commonmark spec
50+
if _, err := w.WriteString(`<code class="chroma language-` + string(lang) + `">`); err != nil {
51+
return ""
52+
}
53+
54+
lexer := lexers.Get(lang)
55+
if _, err := w.WriteString(highlight.Code(lexer.Config().Filenames[0], source)); err != nil {
56+
return ""
57+
}
58+
59+
if _, err := w.WriteString("</code></pre>"); err != nil {
60+
return ""
61+
}
62+
63+
return w.String()
64+
}
4165

4266
renderer := &Renderer{
4367
HTMLWriter: htmlWriter,

0 commit comments

Comments
 (0)