GitRoot

Craft your forge, Build your project, Grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: EUPL-1.2
 4
 5package main
 6
 7import (
 8	"encoding/json"
 9	"fmt"
10
11	gitroot "gitroot.dev/libs/golang/plugin"
12	"gitroot.dev/libs/golang/plugin/model"
13)
14
15const PLUGIN_CODE = "apex_code"
16const PLUGIN_CODE_FUNC = "renderCode"
17const PLUGIN_MERMAID = "apex_mermaid"
18const PLUGIN_MERMAID_FUNC = "renderCode"
19
20var defaultRun = []model.PluginRun{{
21	Func: []model.PluginFunc{{FuncName: "renderMd", Args: []string{"fp", "md", "extraMedata"}, Res: []string{"html", "metas"}}},
22	Write: model.PluginWrite{
23		CallFunc: []model.PluginCallFuncRight{
24			{PluginName: PLUGIN_CODE, FuncName: PLUGIN_CODE_FUNC},
25			{PluginName: PLUGIN_MERMAID, FuncName: PLUGIN_MERMAID_FUNC},
26		},
27	},
28}}
29
30type Plugin struct {
31	server               model.Server
32	canCallCodePlugin    bool
33	canCallMermaidPlugin bool
34}
35
36func (p *Plugin) Init(repoName string, confHasChanged bool, serializedConf string) error { return nil }
37func (p *Plugin) StartCommit(commit model.Commit) error                                  { return nil }
38func (p *Plugin) AddFile(file model.File) error                                          { return nil }
39func (p *Plugin) ModFile(file model.File) error                                          { return nil }
40func (p *Plugin) DelFile(file model.File) error                                          { return nil }
41func (p *Plugin) EndCommit(commit model.Commit) error                                    { return nil }
42func (p *Plugin) Finish() error                                                          { return nil }
43
44func Build(server model.Server) model.Plugin {
45	p := &Plugin{
46		server: server,
47	}
48	server.ExportFunc("renderMd", func(args map[string]string) (map[string]string, error) {
49		p.canCallCodePlugin = server.CanCallFunc(PLUGIN_CODE, PLUGIN_CODE_FUNC, map[string]string{"code": "", "lang": ""})
50		p.canCallMermaidPlugin = server.CanCallFunc(PLUGIN_MERMAID, PLUGIN_MERMAID_FUNC, map[string]string{"code": ""})
51		fp := args["fp"]
52		md := args["md"]
53		extraMetadataJson := args["extraMetadata"]
54		extraMetadata := map[string]string{}
55		err := json.Unmarshal([]byte(extraMetadataJson), &extraMetadata)
56		if err != nil {
57			return nil, err
58		}
59		html, metas := p.mdToHTML(fp, []byte(md), extraMetadata)
60		metasJson, err := json.Marshal(metas)
61		if err != nil {
62			return nil, err
63		}
64		server.Log(fmt.Sprintf("fp=%s for renderred html=%s", fp, html))
65		return map[string]string{
66			"html":  html,
67			"metas": string(metasJson),
68		}, nil
69	})
70	return p
71}
72
73//go:wasmexport install
74func main() {
75	gitroot.Register(defaultRun, Build)
76}