From 87046ba0fc5b9c3be61d23bd8cc2bc6fa84577ba Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 20 Apr 2026 14:07:29 +0200 Subject: feat(go): bindings --- go/typdown.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 go/typdown.go (limited to 'go/typdown.go') diff --git a/go/typdown.go b/go/typdown.go new file mode 100644 index 0000000..8d8fff7 --- /dev/null +++ b/go/typdown.go @@ -0,0 +1,45 @@ +package typdown + +// #cgo LDFLAGS: -L${SRCDIR}/zig-out/lib/ -ltypdown +// #include +// #include "typdown.h" +import "C" +import ( + "errors" + "html/template" + "unsafe" +) + +var ( + codeErrors = map[uint8]error{ + 1: errors.New("out of memory"), + 2: ErrInvalidUtf8, + 3: ErrNotSupported, + 4: ErrModifierNotClosed, + 5: ErrInvalidTitleContent, + 6: ErrIllegalPlacement, + 7: ErrInvalidLink, + } + ErrInvalidUtf8 = errors.New("invalid UTF-8") + ErrNotSupported = errors.New("feature not supported") + ErrModifierNotClosed = errors.New("modifier not closed") + ErrInvalidTitleContent = errors.New("invalid title content") + ErrIllegalPlacement = errors.New("illegal placement") + ErrInvalidLink = errors.New("invalid link") +) + +func Parse(content string) (template.HTML, error) { + code := C.uchar(0) + conv := C.CString(content) + raw := C.parse(conv, &code) + defer C.free(unsafe.Pointer(conv)) + if code > 0 { + err := codeErrors[uint8(code)] + if code == 1 { + panic(err) + } + return "", err + } + defer C.free(unsafe.Pointer(raw)) + return template.HTML(C.GoString(raw)), nil +} -- cgit v1.2.3