wip
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"syscall/js"
|
||||
|
||||
p "git.max-richter.dev/max/marka/parser"
|
||||
"git.max-richter.dev/max/marka/registry"
|
||||
)
|
||||
|
||||
func matchBlocks(_ js.Value, args []js.Value) any {
|
||||
@@ -50,10 +51,51 @@ func parseFile(_ js.Value, args []js.Value) any {
|
||||
return js.ValueOf(string(b))
|
||||
}
|
||||
|
||||
func parseFileWithTemplate(_ js.Value, args []js.Value) any {
|
||||
if len(args) < 2 {
|
||||
return js.ValueOf(map[string]any{"error": "missing markdown or template"})
|
||||
}
|
||||
res, err := p.ParseFileWithTemplate(args[0].String(), args[1].String())
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
}
|
||||
b, err := json.Marshal(res) // return JSON string to avoid reflect-heavy bridging
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
}
|
||||
return js.ValueOf(string(b))
|
||||
}
|
||||
|
||||
func listTemplates(_ js.Value, args []js.Value) any {
|
||||
templates, err := registry.ListTemplates()
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
}
|
||||
b, err := json.Marshal(templates)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
}
|
||||
return js.ValueOf(string(b))
|
||||
}
|
||||
|
||||
func getTemplate(_ js.Value, args []js.Value) any {
|
||||
if len(args) == 0 {
|
||||
return js.ValueOf(map[string]any{"error": "missing template name"})
|
||||
}
|
||||
template, err := registry.GetTemplate(args[0].String())
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
}
|
||||
return js.ValueOf(template)
|
||||
}
|
||||
|
||||
func main() {
|
||||
js.Global().Set("markaDetectType", js.FuncOf(detectType))
|
||||
js.Global().Set("markaParseFile", js.FuncOf(parseFile))
|
||||
js.Global().Set("markaParseFileWithTemplate", js.FuncOf(parseFileWithTemplate))
|
||||
js.Global().Set("markaMatchBlocks", js.FuncOf(matchBlocks))
|
||||
js.Global().Set("markaListTemplates", js.FuncOf(listTemplates))
|
||||
js.Global().Set("markaGetTemplate", js.FuncOf(getTemplate))
|
||||
select {}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user