60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package main
|
|
|
|
// #include <stdlib.h>
|
|
import "C"
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
|
|
"github.com/yuin/goldmark"
|
|
highlighting "github.com/yuin/goldmark-highlighting/v2"
|
|
"github.com/yuin/goldmark/util"
|
|
)
|
|
|
|
var md = goldmark.New(
|
|
goldmark.WithExtensions(
|
|
highlighting.NewHighlighting(
|
|
highlighting.WithStyle("github-dark"),
|
|
highlighting.WithWrapperRenderer(func(w util.BufWriter, context highlighting.CodeBlockContext, entering bool) {
|
|
lang, _ := context.Language()
|
|
|
|
if entering {
|
|
if lang == nil {
|
|
w.WriteString(`<div><pre class="aero"><code>`)
|
|
return
|
|
}
|
|
|
|
w.WriteString(fmt.Sprintf(`<div class="code-block"><p class="code-block-header"><span class="language-name">%s</span></p><pre class="aero"><code>`, lang))
|
|
} else {
|
|
w.WriteString(`</code></pre></div>`)
|
|
}
|
|
}),
|
|
highlighting.WithFormatOptions(
|
|
chromahtml.PreventSurroundingPre(true),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
|
|
//export MarkdownToHTML
|
|
func MarkdownToHTML(source *C.char) *C.char {
|
|
src := C.GoString(source)
|
|
|
|
var buf bytes.Buffer
|
|
if err := md.Convert([]byte(src), &buf); err != nil {
|
|
return C.CString("")
|
|
}
|
|
|
|
return C.CString(buf.String())
|
|
}
|
|
|
|
//export FreeString
|
|
func FreeString(s *C.char) {
|
|
C.free(unsafe.Pointer(s))
|
|
}
|
|
|
|
func main() {} // required, unused
|