Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code block highlight to orgmode back #14222

Merged
merged 10 commits into from Jun 23, 2021
24 changes: 24 additions & 0 deletions modules/markup/orgmode/orgmode.go
Expand Up @@ -10,10 +10,12 @@ import (
"html"
"strings"

"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/util"

"github.com/alecthomas/chroma/lexers"
"github.com/niklasfasching/go-org/org"
)

Expand All @@ -38,6 +40,28 @@ func (Parser) Extensions() []string {
// Render renders orgmode rawbytes to HTML
func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
htmlWriter := org.NewHTMLWriter()
htmlWriter.HighlightCodeBlock = func(source, lang string, inline bool) string {
var w strings.Builder
if _, err := w.WriteString(`<pre>`); err != nil {
return ""
}

// include language-x class as part of commonmark spec
if _, err := w.WriteString(`<code class="chroma language-` + string(lang) + `">`); err != nil {
return ""
}

lexer := lexers.Get(lang)
if _, err := w.WriteString(highlight.Code(lexer.Config().Filenames[0], source)); err != nil {
return ""
}

if _, err := w.WriteString("</code></pre>"); err != nil {
return ""
}

return w.String()
}
zeripath marked this conversation as resolved.
Show resolved Hide resolved

renderer := &Renderer{
HTMLWriter: htmlWriter,
Expand Down