summaryrefslogtreecommitdiff
path: root/internal/entry/entry.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-24 10:36:17 +0300
committerPaul Buetow <paul@buetow.org>2024-10-24 10:36:17 +0300
commit8b4cd3d9d2af89efdb9b0225742d70edc3483bf4 (patch)
tree6d373a5fc902e766d10eab52f44d2419c14baa1f /internal/entry/entry.go
parent9a68ca0461bdf6d093213a218fbf255678018a16 (diff)
refactor
Diffstat (limited to 'internal/entry/entry.go')
-rw-r--r--internal/entry/entry.go25
1 files changed, 10 insertions, 15 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index 34ce5f0..a103e69 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -77,30 +77,31 @@ func New(filePath string) (Entry, error) {
return e, nil
}
-func (e Entry) Content() (string, error) {
+func (e *Entry) Content() (string, []string, error) {
bytes, err := os.ReadFile(e.Path)
if err != nil {
- return "", err
+ return "", []string{}, err
}
- return strings.TrimSpace(string(bytes)), nil
+ content := strings.TrimSpace(string(bytes))
+ return content, extractURLs(content), nil
}
-func (e Entry) ContentWithLimit(sizeLimit int) (string, error) {
- content, err := e.Content()
+func (e Entry) ContentWithLimit(sizeLimit int) (string, []string, error) {
+ content, urls, err := e.Content()
if err != nil {
- return "", err
+ return "", urls, err
}
if len(content) > sizeLimit {
err := fmt.Errorf("entry content exceeds size limit: %d > %d: %v", len(content), sizeLimit, e)
if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil {
- return "", errors.Join(err, err2)
+ return "", urls, errors.Join(err, err2)
}
if err2 := e.Edit(); err2 != nil {
- return "", errors.Join(err, err2)
+ return "", urls, errors.Join(err, err2)
}
return e.ContentWithLimit(sizeLimit)
}
- return content, nil
+ return content, urls, nil
}
func (e *Entry) MarkPosted() error {
@@ -128,12 +129,6 @@ func (e Entry) Edit() error {
return nil
}
-// extractURLs finds all occurrences of URLs starting with "http://" or "https://" in a given string.
-func (e Entry) ExtractURLs() []string {
- content, _ := e.Content()
- return extractURLs(content)
-}
-
func extractURLs(input string) []string {
urlPattern := `(http://|https://|ftp://)[^\s]+`
re := regexp.MustCompile(urlPattern)