qownnotes / scripts

QOwnNotes script repository
http://www.qownnotes.org
GNU General Public License v3.0
104 stars 79 forks source link

[BUG] Backlinks doesn't support setext headers for title #227

Closed pegasushyper closed 6 months ago

pegasushyper commented 6 months ago

Script name

backlinks

Script authors

@dohliam

Expected behavior

Notes that begin with an setext header will register first line as the title and use it in the generated links.

Actual behavior

Only ATX header titles work see responsible line

The html output of files with the setext header title have the link in it, but it is not displayed, because the tag has no text content (which is supposed to be the note title). i.e. <a href="file:///home/pegasushyper/Documents/Notes/Note 2024-04-28T00.18.35.md"></a>

Steps to reproduce

  1. Create new note that uses setext header as title.
  2. Create another note that references it via the wiki-links script syntax (i.e. [[my-test-note]]).
  3. Then look at the preview of the "my-test-note" file and at the end you'll see the h2 "Backlinks" which is either followed by nothing at all (QON preview) or by an empty bullet point (when html is viewed in firefox).

Output from the debug section in the settings dialog

Irrelevant, because it has nothing to do with my machine, version, etc.; it's just not accounted for in the script.

Relevant log output in the Log panel

Nothing interesting, you just create/ change files.

dohliam commented 6 months ago

This looks like it could be fixed by changing the regex in line 85 to match the title rather than the full page text. That might also have the added benefit of speeding up the script a bit. For this to work, the note object would need to have a property containing the note title (regardless of whether it was ATX or Setex format).

I tried to find how to do this in the documentation (i.e., something like pageObj.title) but could not find anything similar.

@pbek Is there an existing way to get the note title from a note object by any chance?

pbek commented 6 months ago

What is it exactly that you are trying to achieve? And what is the title exactly for you? Just the heading 1? Also, something of a front matter? Something done in a script? Are you planing to find a note by that title then?

dohliam commented 6 months ago

@pbek Yes, this would just be to extract the heading 1, regardless of whether it uses ATX or Setext format. Front matter should be ignored. (I guess it is technically possible to have more than one h1 in a document, in which case we would only be looking for the first one to appear.)

In this script, we only need to match the title of the current note with the title of each note from an existing list of note objects. If there is a match, the corresponding note is added to a list of backlinks to the current page.

The issue with the current approach in this script is that it relies on a simple regex to match ATX headers only. It also searches through the text of the entire page, which is rather slow.

Since the title seems to be a special heading in some circumstances (for example, I believe it is set to be the same as the filename by default), I assumed there would be an existing "title" property or method of note objects but couldn't find it documented anywhere.

pbek commented 6 months ago

Usually the heading 1 will be used to determine the filename. And that is stored in name (see https://www.qownnotes.org/scripting/classes.html#properties-and-methods). It will also be used in the note list.

If you need to parse this yourself, then, for example, just take the first line of the note text. You could split the note by \n or something and take the first line that is not empty... If you want to support more than one heading 1 you'd need to take a look at all lines...

dohliam commented 6 months ago

Thanks @pbek! I have taken your suggestion and changed the title parsing to just grab the first line of the file. This has two benefits:

By the way, while looking at this I noticed that Note.name and Note.fileName seem to produce exactly the same result, even with the option to have different filenames and note titles selected. I would have expected Note.name to store the top-level note heading for use the note list etc. Is there a distinction between these two note properties?

pbek commented 6 months ago

By the way, while looking at this I noticed that Note.name and Note.fileName seem to produce exactly the same result, even with the option to have different filenames and note titles selected. I would have expected Note.name to store the top-level note heading for use the note list etc. Is there a distinction between these two note properties?

They are almost all the time equal because of historical reasons... Although they can differ, for example, when a script changes the behavior of how notes are shown in the note list. The heading 1 can, but will not be in all cases, be the note.name.