GitRoot
Craft your forge, Build your project, Grow your community freely
1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5#![no_main]
6
7use gitroot_plugin_sdk::{
8 register, Commit, File, Plugin, PluginFunc, PluginRun, PluginWrite, Server,
9};
10
11pub use gitroot_plugin_sdk::exports::*;
12pub use gitroot_plugin_sdk::imports::*;
13use mermaid_rs_renderer::render_with_options;
14use mermaid_rs_renderer::RenderOptions;
15use serde::{Deserialize, Serialize};
16use std::collections::HashMap;
17use std::sync::Arc;
18
19struct ApexMermaid {}
20
21impl ApexMermaid {
22 fn new(server: Arc<Server>) -> Self {
23 server.export_func("renderCode", |args| {
24 if let Some(value) = args.get("code") {
25 let mut opts = RenderOptions::modern();
26 opts.theme.background = "transparent".to_string();
27 render_with_options(value, opts)
28 .map(|svg| HashMap::from([("html".to_string(), svg)]))
29 .map_err(|err| err.to_string())
30 } else {
31 Ok(HashMap::new())
32 }
33 });
34 Self {}
35 }
36}
37
38#[derive(Serialize, Deserialize)]
39struct Conf {}
40
41impl Plugin for ApexMermaid {
42 fn init(
43 &self,
44 _repo_name: String,
45 _conf_has_changed: bool,
46 _serialized_conf: String,
47 ) -> Result<(), String> {
48 Ok(())
49 }
50
51 fn start_commit(&self, _commit: Commit) -> Result<(), String> {
52 Ok(())
53 }
54
55 fn add_file(&self, _file: File) -> Result<(), String> {
56 Ok(())
57 }
58
59 fn mod_file(&self, _file: File) -> Result<(), String> {
60 Ok(())
61 }
62
63 fn del_file(&self, _file: File) -> Result<(), String> {
64 Ok(())
65 }
66
67 fn end_commit(&self, _commit: Commit) -> Result<(), String> {
68 Ok(())
69 }
70
71 fn finish(&self) -> Result<(), String> {
72 Ok(())
73 }
74}
75
76#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "install"))]
77pub extern "C" fn install() {
78 let conf = PluginRun {
79 path: String::from(""),
80 branch: vec![],
81 when: vec![],
82 func: vec![PluginFunc {
83 func_name: "renderCode".to_string(),
84 args: vec!["code".to_string()],
85 res: vec!["html".to_string()],
86 }],
87 write: PluginWrite {
88 git: vec![],
89 web: vec![],
90 exec: vec![],
91 call_func: vec![],
92 },
93 configuration: serde_json::to_value(Conf {}).unwrap(),
94 };
95 register(vec![conf], ApexMermaid::new);
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn test_plugin() {
104 assert_eq!(true, true);
105 }
106}