… more an annoyance really, and not elfeed’s fault at all but …

[first posted on reddit/r/emacs but probably more interesting here]

lemmy RSS feeds (such as “https://lemmy.ml/feeds/c/emacs.xml”) often (but not always) have mis-guided “Link:” elements which target an external link, an image file or other material instead of the lemmy post itself. Consequently, hitting ‘b’ elfeed-search-browse-url may send one on a surprising if not always useful journey.

eg

Title: Keymacs, a program to generate Emacs keybindings | Plain DrOps
Author: https://feddit.de/u/DrOps
Date: Tue, 23 Apr 2024 23:35:25 AEST
Feed: Lemmy - emacs
Tags: emacs, lemmy
Link: https://plaindrops.de/blog/2024/keymacs/

submitted by DrOps to emacs
8 points | 2 comments
https://plaindrops.de/blog/2024/keymacs/

In this case, the link to lemmy itself is in the “2 comments” => https://lemmy.ml/post/14798221

Here’s a little hook to fix it up - it also marks the entry with the tag ‘lemmy-fixed’ …

(defun elfeed-fix-lemmy-link (entry)
  "Fix lemmy.ml RSS feed links in elfeed."
  (when-let ((url-base-regexp "https://lemmy\\.ml/")
             (feed (elfeed-entry-feed entry))
             (feed-url (elfeed-feed-url feed))
             ((string-match-p (concat url-base-regexp "feeds/c/") feed-url))
             (entry-link (elfeed-entry-link entry))
             (link-url-regexp (concat url-base-regexp "post/[0-9]+"))
             ((not (string-match-p link-url-regexp entry-link))))
    (when-let ((content (elfeed-deref (elfeed-entry-content entry))))
      (let ((lines (split-string content "\n")))
        (dolist (line lines)
          (when (string-match link-url-regexp line)
            (let ((post-link (substring line (match-beginning 0) (match-end 0))))
              (setf (elfeed-entry-link entry) post-link)
              (elfeed-tag entry 'lemmy-fixed)
              (message "Fixed lemmy link in elfeed: %s" post-link)
              (cl-return))))))))
(add-hook 'elfeed-new-entry-hook #'elfeed-fix-lemmy-link)

Thanks to u/karthik for getting me started with this. The crappy elisp is mine not his (roast me!)