diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-20 14:07:29 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-20 14:07:29 +0200 |
| commit | 87046ba0fc5b9c3be61d23bd8cc2bc6fa84577ba (patch) | |
| tree | c53336a0dcc223819986ddba016b31cc32e73de9 /go/typdown.go | |
| parent | db2f49115713c47b48587022511b3654a7042493 (diff) | |
feat(go): bindings
Diffstat (limited to 'go/typdown.go')
| -rw-r--r-- | go/typdown.go | 45 |
1 files changed, 45 insertions, 0 deletions
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 <stdlib.h> +// #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 +} |
