Skip to content

Commit

Permalink
Replace an ugly regex with proper parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoisin committed Dec 13, 2024
1 parent c3649bd commit 3dc96f8
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions internal/reader/rewrite/rewrite_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)$`)
youtubeIdRegex = regexp.MustCompile(`youtube_id"?\s*[:=]\s*"([a-zA-Z0-9_-]{11})"`)
invidioRegex = regexp.MustCompile(`https?://(.*)/watch\?v=(.*)`)
imgRegex = regexp.MustCompile(`<img [^>]+>`)
textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
)

Expand Down Expand Up @@ -84,7 +83,8 @@ func addMailtoSubject(entryContent string) string {
}

func addDynamicImage(entryContent string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
parserHtml, err := nethtml.ParseWithOptions(strings.NewReader(entryContent), nethtml.ParseOptionEnableScripting(false))
doc := goquery.NewDocumentFromNode(parserHtml)
if err != nil {
return entryContent
}
Expand Down Expand Up @@ -149,12 +149,9 @@ func addDynamicImage(entryContent string) string {

if !changed {
doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
matches := imgRegex.FindAllString(noscript.Text(), 2)

if len(matches) == 1 {
if img := noscript.Find("img"); img.Length() == 1 {
img.Unwrap()
changed = true

noscript.ReplaceWithHtml(matches[0])
}
})
}
Expand Down

0 comments on commit 3dc96f8

Please sign in to comment.