From d6ffc0ba40ac664e43396bb85d57a55d58c3d9a7 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 12 Aug 2023 10:52:58 +0200 Subject: [PATCH] Avoid duplicate backlink targets in backlink reports I am not sure yet when duplicate entries happen, but they do happen occasionally. --- site-lisp/db-org.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 01d5c9e..1c0f20f 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -1638,7 +1638,7 @@ files referenced in `org-agenda-text-search-extra-files'." ;; Search directly for “[[id:ITEM-ID]” instead of using the regular ;; expression for links, as the latter seems to be broken (as of - ;; [2022-06-09] when descriptions contain brackets + ;; [2022-06-09]) when descriptions contain brackets (org-ql-query :select '(org-id-get-create) :from files :where (let ((link-expression `(regexp ,(format "\\[\\[id:%s\\]" item-id)))) @@ -1721,7 +1721,7 @@ PARAMS may contain the following values: (setq headlines (->> headlines ;; Transform (headline-id backlink-ids) to pairs - ;; (headline-id . backlink-id) + ;; (backlink-id . headline-id) (-mapcat (pcase-lambda (`(,headline . ,backlinks)) (mapcar #'(lambda (backlink) (cons backlink headline)) @@ -1729,9 +1729,10 @@ PARAMS may contain the following values: ;; Group by backlinks (first entry), returns alist of ;; backlink-ids and list of pairs (backlink-id . headline-id) (-group-by #'car) - ;; Flatten list, to get a list of (backlink-id headline-ids...) + ;; Flatten list, to get a list of (backlink-id headline-ids...); + ;; remove dupliate headline-id entries (-map (pcase-lambda (`(,backlink . ,backlink-headline-conses)) - (cons backlink (-map #'cdr backlink-headline-conses)))))) + (cons backlink (-distinct (-map #'cdr backlink-headline-conses))))))) ;; Replace IDs by headlines and add priority for sorting (setq output-lines