From 5e0a725deb1d15efb94b84d379560200a1887b29 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 29 Aug 2023 14:14:40 -0700 Subject: [PATCH] Update link posts to support !nomad entries. --- nomad/post.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/nomad/post.go b/nomad/post.go index 54b725e..26267ce 100644 --- a/nomad/post.go +++ b/nomad/post.go @@ -4,14 +4,22 @@ import ( "bytes" "errors" "fmt" + "net/url" "path/filepath" "git.sr.ht/~thrrgilag/woodstock" + "git.wntrmute.dev/kyle/goutils/log" "github.com/anaskhan96/soup" ) -func nomadLink(url Source) string { - return fmt.Sprintf("[nomad.wntrmute.net](%s)", url.ID()) +func nomadLink(src Source) string { + u, err := url.Parse(src.ID()) + if err != nil { + log.Errf("nomadLink: %s", err) + return fmt.Sprintf("[%s](%s)", src.ID(), src.ID()) + } + + return fmt.Sprintf("[%s](%s)", u.Host, src.ID()) } type sel struct { @@ -22,6 +30,23 @@ func selector(selectors ...string) sel { return sel{selectors: selectors} } +type imageFinder func(soup.Root) (string, bool) + +var imageFinders = []imageFinder{ + func(root soup.Root) (string, bool) { + return find( + root, "src", + selector("div", "class", "entry-image"), + selector("img")) + }, + func(root soup.Root) (string, bool) { + return find( + root, "src", + selector("div", "class", "gh-inner"), + selector("img")) + }, +} + func find(root soup.Root, attr string, selectors ...sel) (string, bool) { result := root for _, selector := range selectors { @@ -65,10 +90,15 @@ func (p *Post) Fetch() error { p.Body = body + " " + p.Body } - imageURL, hasImageURL := find( - root, "src", - selector("div", "class", "entry-image"), - selector("img")) + var imageURL string + var hasImageURL bool + + for _, finder := range imageFinders { + imageURL, hasImageURL = finder(root) + if hasImageURL { + break + } + } if hasImageURL { p.Image = NewURLSource(imageURL)