Skip to content

Commit

Permalink
feat: text/markdown: add Link{}, Links{}
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Nov 23, 2024
1 parent f119405 commit e051164
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions text/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 `[`
Expand Down

0 comments on commit e051164

Please sign in to comment.