olexsmir.xyz/go/main.go(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package main
// #include <stdlib.h>
import "C"
import (
"bytes"
"unsafe"
"github.com/alecthomas/chroma/v2/formatters"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
treeblood "github.com/wyatt915/goldmark-treeblood"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
callout "gitlab.com/staticnoise/goldmark-callout"
)
func main() {}
//export md_to_html
func md_to_html(input *C.char) *C.char {
if input == nil {
return C.CString("")
}
inp := C.GoString(input)
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
highlighting.NewHighlighting(
highlighting.WithFormatOptions(
chromahtml.Standalone(false),
chromahtml.WithClasses(true),
),
),
extension.NewFootnote(
extension.WithFootnoteIDPrefix([]byte("footnote")),
),
extension.Linkify,
treeblood.MathML(),
callout.CalloutExtention,
emoji.Emoji,
),
goldmark.WithRendererOptions(html.WithUnsafe()),
)
var buf bytes.Buffer
if err := md.Convert([]byte(inp), &buf); err != nil {
return C.CString("")
}
return C.CString(buf.String())
}
//export chroma_css
func chroma_css(theme *C.char) *C.char {
if theme == nil {
return C.CString("")
}
thm := C.GoString(theme)
var buf bytes.Buffer
formatter := formatters.Get("html").(*chromahtml.Formatter)
if err := formatter.WriteCSS(&buf, styles.Get(thm)); err != nil {
return C.CString("")
}
return C.CString(buf.String())
}
//export free_cstring
func free_cstring(s *C.char) {
if s != nil {
C.free(unsafe.Pointer(s))
}
}
|