I have thought about this a little bit more, I think a much simpler solution could be implemented - namely that which is already implemented for unlinked references - to just get the immediate line where the link is embedded.
For this, youd have to agree not to think in terms of characters back and forth - but in terms of lines
(defun custom/org-roam-preview-default-function ()
"Return the preview content at point.
This function returns the text of the immediate line(s) where the link is located."
(let ((beg (save-excursion
(org-beginning-of-line 1) (point)))
(end (save-excursion
(org-end-of-line 1) (point))))
(string-trim (buffer-substring-no-properties beg end))))
(setopt org-roam-preview-function #'custom/org-roam-preview-default-function)
To have say more than one line showing - change the 1’s to some N where N-1 lines back and forth would be shown
For example to show 2 lines before and 2 lines after do
(defun custom/org-roam-preview-default-function ()
"Return the preview content at point.
This function returns the immediate line(s) where the link is located."
(let ((beg (save-excursion
(org-beginning-of-line -1) (point)))
(end (save-excursion
(org-end-of-line 3) (point))))
(string-trim (buffer-substring-no-properties beg end))))
since
-1-1 = -2 & 3-1 =2
Therefore the following
:PROPERTIES:
:ID: 20240922T081550.535686
:END:
#+title: target
* h1
-4
-3
-2
-1
[[id:20240922T081423.122777][source]]
1
2
3
4
[[id:20240922T081423.122777][source]]
* h2
would show up as this
Backlinks (2)
target (h1)
-2
-1
[[id:20240922T081423.122777][source]]
1
2
target (h1)
3
4
[[id:20240922T081423.122777][source]]
* h2
This is a much simple solution.