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	"github.com/tidwall/gjson"
  9	gitroot "gitroot.dev/libs/golang/plugin/model"
 10)
 11
 12type conf struct {
 13	defaultBranch       string
 14	style               string
 15	favicon             string
 16	header              string
 17	menu                []link
 18	footer              string
 19	generateGitWorktree bool
 20	branchesDir         string
 21	meta                map[string]string
 22}
 23
 24var defaultRun = []gitroot.PluginRun{{
 25	Path:   "**/*",
 26	Branch: []string{"main"},
 27	When:   gitroot.PluginRunWhenAll,
 28	Write: gitroot.PluginWrite{
 29		Git: []gitroot.PluginWriteRight{{
 30			Path: "index.md",
 31			Can:  []gitroot.PluginWriteRightCan{gitroot.PluginWriteRightCanAdd},
 32		}},
 33		Web: []gitroot.PluginWriteRight{{
 34			Path: "**/*",
 35			Can:  gitroot.PluginWriteRightCanAll,
 36		}},
 37	},
 38	Configuration: map[string]any{
 39		"defaultBranch": "main",
 40		"style":         "simple.min.css",
 41		"favicon":       "icon.png",
 42		"header":        "<h1>{{repo.name}}</h1>",
 43		"menu": []link{
 44			{Display: "🏠 Home", Link: "/"},
 45			{Display: "📖 Readme", Link: "/README.html"},
 46			{Display: "🐞 Issues", Link: "/boards/issues.html"},
 47			{Display: "⚖ Licence", Link: "/LICENCE.html"},
 48		},
 49		"footer":              "<p>Clone with <code>git clone {{repo.cloneUrl}}</code></p><small>Hosted with ❤️ by <a href=\"https://gitroot.dev\">GitRoot</a></small>",
 50		"generateGitWorktree": true,
 51		"branchesDir":         "branches",
 52		"meta": map[string]string{
 53			"description":       "a short description",
 54			"og:title":          "title displayed in social",
 55			"og:description":    "description displayed in social",
 56			"og:type":           "article",
 57			"fediverse:creator": "@forge@gitroot.dev",
 58		},
 59	},
 60}, {
 61	Path:   "**/*",
 62	Branch: []string{"*", "!main"},
 63	When:   gitroot.PluginRunWhenAll,
 64	Write: gitroot.PluginWrite{
 65		Git: []gitroot.PluginWriteRight{},
 66		Web: []gitroot.PluginWriteRight{{
 67			Path: "branches/**/*",
 68			Can:  gitroot.PluginWriteRightCanAll,
 69		}},
 70	},
 71	Configuration: map[string]any{
 72		"defaultBranch": "main",
 73		"style":         "simple.min.css",
 74		"favicon":       "icon.png",
 75		"header":        "<h1>{{repo.name}}</h1>",
 76		"menu": []link{
 77			{Display: "🏠 Home", Link: "/"},
 78			{Display: "📖 Readme", Link: "/README.html"},
 79			{Display: "🐞 Issues", Link: "/boards/issues.html"},
 80			{Display: "⚖ Licence", Link: "/LICENCE.html"},
 81		},
 82		"footer":              "<p>Clone with <code>git clone {{repo.cloneUrl}}</code></p><small>Hosted with ❤️ by <a href=\"https://gitroot.dev\">GitRoot</a></small>",
 83		"generateGitWorktree": false,
 84		"branchesDir":         "branches",
 85		"meta": map[string]string{
 86			"description":       "a short description",
 87			"og:title":          "title displayed in social",
 88			"og:description":    "description displayed in social",
 89			"og:type":           "article",
 90			"fediverse:creator": "@forge@gitroot.dev",
 91		},
 92	},
 93}}
 94
 95type link struct {
 96	Link    string `json:"link"`
 97	Display string `json:"display"`
 98}
 99
100func (p *Plugin) newConf(serializedConf string) *conf {
101	jsonConf := gjson.Parse(serializedConf)
102	menus := []link{}
103	for _, l := range jsonConf.Get("menu").Array() {
104		menus = append(menus, link{
105			Link:    l.Get("link").String(),
106			Display: l.Get("display").String(),
107		})
108	}
109	metaJson := jsonConf.Get("meta").Map()
110	meta := make(map[string]string, len(metaJson))
111	for key, val := range metaJson {
112		meta[key] = val.String()
113	}
114	defaultBranch := jsonConf.Get("defaultBranch").String()
115	if defaultBranch == "" {
116		defaultBranch = "main"
117	}
118	style := jsonConf.Get("style").String()
119	if style == "" {
120		style = "simple.min.css"
121	}
122	return &conf{
123		defaultBranch:       defaultBranch,
124		style:               style,
125		favicon:             jsonConf.Get("favicon").String(),
126		header:              jsonConf.Get("header").String(),
127		menu:                menus,
128		footer:              jsonConf.Get("footer").String(),
129		generateGitWorktree: jsonConf.Get("generateGitWorktree").Bool(),
130		branchesDir:         jsonConf.Get("branchesDir").String(),
131		meta:                meta,
132	}
133}