diff --git a/text/markdown/markdown.go b/text/markdown/markdown.go index 6d0bf2fb..dc03a3c9 100644 --- a/text/markdown/markdown.go +++ b/text/markdown/markdown.go @@ -3,7 +3,11 @@ package markdown import ( "fmt" "regexp" + "sort" "strings" + + "github.com/grokify/mogo/type/slicesutil" + "github.com/grokify/mogo/type/stringsutil" ) // BoldText bodifies the identified text. It looks for start of words @@ -30,6 +34,35 @@ func URLToMarkdownLinkHostname(url string) string { return url } +type Link struct { + Text string + URL string +} + +func (lnk Link) Markdown() string { + return Linkify(lnk.URL, lnk.Text) +} + +type Links []Link + +func (lnks Links) Texts(condense, dedupe, sortAsc bool) []string { + var out []string + for _, lnk := range lnks { + out = append(out, lnk.Text) + } + if condense { + out = stringsutil.SliceCondenseSpace(out, dedupe, sortAsc) + } else { + if dedupe { + slicesutil.Dedupe(out) + } + if sortAsc { + sort.Strings(out) + } + } + return out +} + // Linkify constructs a link from url and text inputs. // This function does not handle escaping so it should // be done before hand, e.g. using `\[` instead of `[`