Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kleutzinger opened this issue Nov 25, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kleutzinger
Copy link
Contributor

kleutzinger commented Nov 25, 2024

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 zadam/trilium#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
Copy link
Contributor Author

kleutzinger commented Nov 25, 2024

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">title</a>

// you'll have to create a text note somewhere and put its ID here
// it will log what changes we make to a note. If this note doesn't exist, we don't log.
const log_note_id = "wtiffFWxDFar";

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

function note2title(b_note) {
  return `${b_note?.noteId} - ${b_note?.title}`;
}

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 observed_title_raw = textMatch[1];
      const observed_title_text = api.unescapeHtml(observed_title_raw);
      const inner_id = href.split("/").pop();

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

      if (real_note?.isProtected) {
        // the link is to a protected note don't modify it
        console.log("dont modify link to protected note " + note2title(real_note))
        continue;
      }

      if (real_note === undefined) {
        console.log(`something weird in note ${bnote.title} - ${bnote.noteId}`);
        console.log(href, observed_title_text, observed_title_raw, 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 !== observed_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: observed_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);
        const logNote = api.getNote(log_note_id);
        if (logNote) {
          logNote.setContent(
            `<p><code>${JSON.stringify(output_obj, null, 2)}</code></p>${logNote.getContent()}`,
            { forceSave: true },
          );
          console.log("wrote to " + note2title(logNote));
        } else {
          console.log("no log note found with id " + log_note_id);
        }
      }
    }
  }
}

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

@eliandoran eliandoran added the bug Something isn't working label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants