fix: most of the template blocks
This commit is contained in:
@@ -8,92 +8,108 @@ import (
|
||||
|
||||
p "git.max-richter.dev/max/marka/parser"
|
||||
"git.max-richter.dev/max/marka/registry"
|
||||
"git.max-richter.dev/max/marka/template"
|
||||
)
|
||||
|
||||
func matchBlocks(_ js.Value, args []js.Value) any {
|
||||
if len(args) == 0 {
|
||||
return js.ValueOf(map[string]any{"error": "missing markdown"})
|
||||
}
|
||||
t, err := p.MatchBlocks(args[0].String(), args[1].String())
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
}
|
||||
|
||||
jsonString, _ := json.Marshal(t)
|
||||
|
||||
return js.ValueOf(string(jsonString)) // plain string
|
||||
func wrapError(err error) string {
|
||||
errMap := map[string]any{"error": err.Error()}
|
||||
errJSON, _ := json.Marshal(errMap)
|
||||
return string(errJSON)
|
||||
}
|
||||
|
||||
func detectType(_ js.Value, args []js.Value) any {
|
||||
if len(args) == 0 {
|
||||
return js.ValueOf(map[string]any{"error": "missing markdown"})
|
||||
}
|
||||
t, err := p.DetectType(args[0].String())
|
||||
func MatchBlocks(this js.Value, args []js.Value) any {
|
||||
s := args[0].String()
|
||||
t := args[1].String()
|
||||
matched, err := p.MatchBlocks(s, t)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
return js.ValueOf(t) // plain string
|
||||
jsonString, _ := json.Marshal(matched)
|
||||
return string(jsonString)
|
||||
}
|
||||
|
||||
func parseFile(_ js.Value, args []js.Value) any {
|
||||
if len(args) == 0 {
|
||||
return js.ValueOf(map[string]any{"error": "missing markdown"})
|
||||
}
|
||||
res, err := p.ParseFile(args[0].String())
|
||||
func DetectType(this js.Value, args []js.Value) any {
|
||||
markdown := args[0].String()
|
||||
t, err := p.DetectType(markdown)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func ParseFile(this js.Value, args []js.Value) any {
|
||||
markdown := args[0].String()
|
||||
res, err := p.ParseFile(markdown)
|
||||
if err != nil {
|
||||
return wrapError(err)
|
||||
}
|
||||
b, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
return js.ValueOf(string(b))
|
||||
return 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())
|
||||
func ParseFileWithTemplate(this js.Value, args []js.Value) any {
|
||||
markdown := args[0].String()
|
||||
template := args[1].String()
|
||||
res, err := p.ParseFileWithTemplate(markdown, template)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
b, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
return js.ValueOf(string(b))
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func listTemplates(_ js.Value, args []js.Value) any {
|
||||
func ListTemplates(this js.Value, args []js.Value) any {
|
||||
templates, err := registry.ListTemplates()
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
b, err := json.Marshal(templates)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
return js.ValueOf(string(b))
|
||||
return 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())
|
||||
func GetTemplate(this js.Value, args []js.Value) any {
|
||||
name := args[0].String()
|
||||
template, err := registry.GetTemplate(name)
|
||||
if err != nil {
|
||||
return js.ValueOf(map[string]any{"error": err.Error()})
|
||||
return wrapError(err)
|
||||
}
|
||||
return js.ValueOf(template)
|
||||
return template
|
||||
}
|
||||
|
||||
func CompileTemplate(this js.Value, args []js.Value) any {
|
||||
source := args[0].String()
|
||||
template, err := template.CompileTemplate(source)
|
||||
if err != nil {
|
||||
return wrapError(err)
|
||||
}
|
||||
b, err := json.Marshal(template)
|
||||
if err != nil {
|
||||
return wrapError(err)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
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))
|
||||
marka := js.Global().Get("Object").New()
|
||||
|
||||
marka.Set("matchBlocks", js.FuncOf(MatchBlocks))
|
||||
marka.Set("detectType", js.FuncOf(DetectType))
|
||||
marka.Set("parseFile", js.FuncOf(ParseFile))
|
||||
marka.Set("parseFileWithTemplate", js.FuncOf(ParseFileWithTemplate))
|
||||
marka.Set("listTemplates", js.FuncOf(ListTemplates))
|
||||
marka.Set("getTemplate", js.FuncOf(GetTemplate))
|
||||
marka.Set("compileTemplate", js.FuncOf(CompileTemplate))
|
||||
|
||||
js.Global().Set("marka", marka)
|
||||
|
||||
select {}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user