1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 22:00:27 +00:00
mycorrhiza/shroom/header_links.go

58 lines
1.8 KiB
Go
Raw Normal View History

2022-06-06 14:33:27 +00:00
package shroom
2021-05-11 08:33:00 +00:00
import (
"github.com/bouncepaw/mycomarkup/v5"
"github.com/bouncepaw/mycomarkup/v5/blocks"
"github.com/bouncepaw/mycomarkup/v5/mycocontext"
2022-06-06 14:41:33 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/hyphae"
2022-06-10 15:45:27 +00:00
"github.com/bouncepaw/mycorrhiza/mycoopts"
2022-06-06 14:33:27 +00:00
"github.com/bouncepaw/mycorrhiza/viewutil"
2022-06-06 14:41:33 +00:00
"os"
2021-05-11 08:33:00 +00:00
)
2022-06-06 14:41:33 +00:00
// SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values.
func SetHeaderLinks() {
switch userLinksHypha := hyphae.ByName(cfg.HeaderLinksHypha).(type) {
case *hyphae.EmptyHypha:
setDefaultHeaderLinks()
case hyphae.ExistingHypha:
contents, err := os.ReadFile(userLinksHypha.TextFilePath())
if err != nil || len(contents) == 0 {
setDefaultHeaderLinks()
} else {
text := string(contents)
parseHeaderLinks(text)
}
}
}
// setDefaultHeaderLinks sets the header links to the default list of: home hypha, recent changes, hyphae list, random hypha.
func setDefaultHeaderLinks() {
2022-06-06 14:33:27 +00:00
viewutil.HeaderLinks = []viewutil.HeaderLink{
2021-05-11 08:33:00 +00:00
{"/recent-changes", "Recent changes"},
{"/list", "All hyphae"},
{"/random", "Random"},
2021-07-24 19:16:49 +00:00
{"/help", "Help"},
{"/category", "Categories"},
2021-05-11 08:33:00 +00:00
}
}
2022-06-06 14:41:33 +00:00
// parseHeaderLinks extracts all rocketlinks from the given text and saves them as header links.
func parseHeaderLinks(text string) {
2022-06-06 14:33:27 +00:00
viewutil.HeaderLinks = []viewutil.HeaderLink{}
2022-06-10 15:45:27 +00:00
ctx, _ := mycocontext.ContextFromStringInput(text, mycoopts.MarkupOptions(""))
// We call for side-effects
_ = mycomarkup.BlockTree(ctx, func(block blocks.Block) {
switch launchpad := block.(type) {
case blocks.LaunchPad:
for _, rocket := range launchpad.Rockets {
2022-06-06 14:33:27 +00:00
viewutil.HeaderLinks = append(viewutil.HeaderLinks, viewutil.HeaderLink{
Href: rocket.LinkHref(ctx),
Display: rocket.DisplayedText(),
})
}
2021-05-11 08:33:00 +00:00
}
})
2021-05-11 08:33:00 +00:00
}