1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 19:56:16 +00:00
mycorrhiza/markup/link.go

57 lines
1.6 KiB
Go
Raw Normal View History

2020-11-04 17:42:02 +00:00
package markup
import (
2020-12-15 18:59:36 +00:00
"fmt"
2020-11-04 17:42:02 +00:00
"path"
"strings"
)
// LinkParts determines what href, text and class should resulting <a> have based on mycomarkup's addr, display and hypha name.
//
// => addr display
// [[addr|display]]
2020-12-15 19:05:51 +00:00
func LinkParts(addr, display, hyphaName string) (href, text, class, icon string) {
2020-11-04 17:42:02 +00:00
if display == "" {
text = addr
} else {
text = strings.TrimSpace(display)
}
class = "wikilink_internal"
switch {
case strings.ContainsRune(addr, ':'):
2020-12-15 18:59:36 +00:00
pos := strings.IndexRune(addr, ':')
destination := addr[:pos]
text = addr[pos+1:]
if strings.HasPrefix(text, "//") && len(text) > 2 {
text = text[2:]
}
2020-12-15 19:05:51 +00:00
return addr, text, "wikilink_external", fmt.Sprintf(`<img class="wikilink__destination-type" src="/static/icon/%s" width="16" height="16"/>`, destination)
2020-11-04 17:42:02 +00:00
case strings.HasPrefix(addr, "/"):
2020-12-15 19:05:51 +00:00
return addr, text, class, ""
2020-11-04 17:42:02 +00:00
case strings.HasPrefix(addr, "./"):
hyphaName = canonicalName(path.Join(hyphaName, addr[2:]))
case strings.HasPrefix(addr, "../"):
hyphaName = canonicalName(path.Join(path.Dir(hyphaName), addr[3:]))
default:
hyphaName = canonicalName(addr)
}
if !HyphaExists(hyphaName) {
class += " wikilink_new"
}
2020-12-15 19:05:51 +00:00
return "/page/" + hyphaName, text, class, ""
2020-11-04 17:42:02 +00:00
}
// Parse markup line starting with "=>" according to wikilink rules.
// See http://localhost:1737/page/wikilink
2020-12-15 19:05:51 +00:00
func Rocketlink(src, hyphaName string) (href, text, class, icon string) {
2020-11-04 17:42:02 +00:00
src = strings.TrimSpace(src[2:]) // Drop =>
if src == "" {
return
}
// Href is text after => till first whitespace
addr := strings.Fields(src)[0]
display := strings.TrimPrefix(src, addr)
return LinkParts(addr, display, hyphaName)
}