TriliumNext / Notes

Build your personal knowledge base with TriliumNext Notes
https://triliumnext.github.io/Docs/
GNU Affero General Public License v3.0
1.08k stars 60 forks source link

(Bug report) Renaming a Note Doesn't Update Link Text on Internal Links on Share Page #672

Open kleutzinger opened 4 days ago

kleutzinger commented 4 days ago

Description

When you update the title of a note that you are internally linking to, this may not update the internal link's text in certain scenarios. The two key places I see it is in the 'view source' of the outer note doing the linking, and the content part of the outer note's share page.

It may be easier to show rather than tell, see the video or steps to recreate below.

Steps To Recreate

  1. In some note (call the note "outer"), make an internal link to another note (call the note "inner")
  2. Open the inner note and rename it.

Observed Behavior

  1. View the outer note in trilium interface see the note's link is renamed properly
  2. However, on the outer note's share page, the inner note link is renamed in the sidebar and child notes section, but the link to the inner note is not renamed in the content of the share page.
  3. Similarly, back in the trilium interface on the outer notes "view source", the inner note link's text has the old name. This may be why the bug is arising

Expected Behavior

  1. The link to the inner note should have the updated title everywhere (the share page, and in the view source of the outer note as well?)

Potential Workarounds / Fixes

  1. some signal when a note's title is changed that updates the text everywhere it is internally-linked
  2. some cron job like above
  3. some manually-run script that updates link's text like above.
  4. a manual fix for an individual inner link is to go to the outer note, delete the internal link, and re-add it. This is not so nice to do in many places, though.

Video Example

Peek 2024-11-12 15-26-trilium-rename-bug.webm

This is a port of the issue https://github.com/zadam/trilium/issues/4879 from the original trilium project, though the same behavior can be observed on TriliumNext.

TriliumNext Version

0.90.12

What operating system are you using?

Other Linux

What is your setup?

Local + server sync

Operating System Version

Arch Linux

Error logs

No response

kleutzinger commented 4 days ago

For what it's worth I've hacked together a backend js script that I run daily or manually that corrects any un-updated link text in text notes. It may not work in all cases, but it works for most things I've run into (other than internal links to attatchments like mp3).

I'll provide it here, run at your own risk; no warranty etc.

// usage: add as a JS Backend note and click 'run'
//        also you can set the label #run=daily so that it runs automatically

// example link we fix:
// <a class="reference-link" href="#root/4PPhsoF84Rs6/AQYEWg4dGEfA/QVwUgibvsiXT">wrong_title</a>

const notes = api.searchForNotes(`note.type = 'text'`);
for (const bnote of notes) {
  const re = new RegExp(`(<a class="reference-link"[^<]+</a>)`, "g");
  const content = bnote.getContent();
  let match;

  while ((match = re.exec(content)) !== null) {
    const hrefMatch = match[0].match(/href="([^"]+)"/);
    const textMatch = match[0].match(/>([^<]+)</);

    if (hrefMatch && textMatch) {
      const href = hrefMatch[1];
      const text = api.unescapeHtml(textMatch[1]);
      const text_unescaped = textMatch[1];
      const inner_id = href.split("/").pop();

      const real_note = api.getNote(inner_id);
      const real_title = real_note?.title;

      if (real_note === undefined) {
        console.log(`something weird in note ${bnote.title} - ${bnote.noteId}`);
        console.log(href, text, text_unescaped, inner_id);
        //todo handle:
        // #root/AolpcZCFWFzk?viewMode=attachments&amp;attachmentId=pKEPhE2EsKrr Record (online-voice-recorder.com).mp3 Record (online-voice-recorder.com).mp3 AolpcZCFWFzk?viewMode=attachments&amp;attachmentId=pKEPhE2EsKrr
      } else if (real_title !== text) {
        const new_link = `<a class="reference-link" href="${href}">${real_title}</a>`;
        const output_obj = {
          bnote_title: bnote.title,
          bnote_noteId: bnote.noteId,
          old_title: text,
          real_title,
          time: new Date(),
        };
        console.log(output_obj);

        // backup revision
        bnote.saveRevision();
        // rewrite the link inside the note's content
        const newContent = content.replace(match[0], new_link);
        bnote.setContent(newContent);
      }
    }
  }
}

console.log("done running link-fixer.js");