Skip to content

Commit

Permalink
#3200 add: replace changed note links
Browse files Browse the repository at this point in the history
Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Jan 17, 2025
1 parent 91e1eb0 commit 2d524da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
40 changes: 26 additions & 14 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3141,7 +3141,7 @@ QVector<int> Note::findBacklinkedNoteIds() const {
}

LinkHit Note::findAndReturnLinkHit(const QString &text, const QString &pattern) {
return text.contains(pattern) ? LinkHit(pattern, pattern) : LinkHit("", "");
return text.contains(pattern) ? LinkHit(pattern, {}) : LinkHit();
}

QSet<LinkHit> Note::findAndReturnLinkHits(const QString &text, const QRegularExpression &regex) {
Expand All @@ -3150,10 +3150,6 @@ QSet<LinkHit> Note::findAndReturnLinkHits(const QString &text, const QRegularExp

while (iterator.hasNext()) {
QRegularExpressionMatch match = iterator.next();
QString linkText = match.captured(1);
QString linkUrl = match.captured(2);
// Do something with the link text and URL

linkHits.insert(LinkHit(match.captured(0), match.captured(1)));
}

Expand Down Expand Up @@ -3205,7 +3201,7 @@ void Note::addTextToLinkedNoteHashIfFound(const Note &note, const QString &noteT
if (!_linkedNoteHash.contains(note)) {
_linkedNoteHash.insert(note, linkHits);
} else {
_linkedNoteHash[note] = linkHits;
_linkedNoteHash[note].unite(linkHits);
}
}
}
Expand All @@ -3216,15 +3212,13 @@ void Note::addTextToLinkedNoteHashIfFound(const Note &note, const QString &noteT
* @return Hash of notes and the link hits
*/
QHash<Note, QSet<LinkHit>> Note::findLinkedNotes() {
auto linkedNoteHash = QHash<Note, QSet<LinkHit>>{};
const auto noteText = getNoteText();
_linkedNoteHash.clear();

// Fetch all notes and look if the current note contains a link to those notes
// We don't need to care about legacy links, because they don't know subfolders
const auto noteList = Note::fetchAll();
for (const Note &note : noteList) {
const QString linkText = getNoteURL(note.getName());
const QString &relativePathToNote = getFilePathRelativeToNote(note);

// We now don't escape slashes in the relative file path, but previously we did,
Expand Down Expand Up @@ -3594,6 +3588,8 @@ bool Note::handleLinkedNotesAfterMoving(const Note &oldNote,
bool changed = false;
qDebug() << __func__ << " - 'oldNote': " << oldNote;

// TODO: Show a dialog, like in Note::handleBacklinkedNotesAfterMoving

// Iterate over linkedNoteHits and update the links to the containing notes
for (auto it = linkedNoteHits.begin(); it != linkedNoteHits.end(); ++it) {
const Note &linkedNote = it.key();
Expand All @@ -3604,11 +3600,14 @@ bool Note::handleLinkedNotesAfterMoving(const Note &oldNote,

for (const LinkHit &linkHit : linkHits) {
const QString oldMarkdown = linkHit.markdown;

// TODO: Get the real new relative note url
const QString newUrl = getNoteURL(_name);
// TODO: Find Markdown for all use cases
const QString newMarkdown = "[" + linkedNote.getName() + "](" + newUrl + ")";
const QString linkText = linkHit.text;
const QString relativeFilePath =
urlEncodeNoteUrl(getFilePathRelativeToNote(linkedNote));
const QString newMarkdown = linkText.isEmpty()
? "<" + relativeFilePath + ">"
: "[" + linkText + "](" + relativeFilePath + ")";
qDebug() << __func__ << " - 'oldMarkdown': " << oldMarkdown;
qDebug() << __func__ << " - 'newMarkdown': " << newMarkdown;

if (noteText.contains(oldMarkdown)) {
noteText.replace(oldMarkdown, newMarkdown);
Expand All @@ -3618,8 +3617,21 @@ bool Note::handleLinkedNotesAfterMoving(const Note &oldNote,
}

if (changed) {
// TODO: Uncomment if done
qDebug() << __func__ << " - 'noteText': " << noteText;

// TODO: Why is this not stored in the note? refetch() also fails, id seems to stay the same
// refetch();
qDebug() << __func__ << " - 'refetch();': " << refetch();

// storeNewText(std::move(noteText));
qDebug() << __func__
<< " - 'storeNewText(std::move(noteText))': " << storeNewText(std::move(noteText));

// storeNoteTextFileToDisk();
qDebug() << __func__ << " - 'storeNoteTextFileToDisk()': " << storeNoteTextFileToDisk();

qDebug() << __func__ << " - 'oldNote': " << oldNote;
qDebug() << __func__ << " - 'this': " << *this;
}

return changed;
Expand Down
2 changes: 1 addition & 1 deletion src/entities/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class QUrl;
class QSqlQuery;

struct LinkHit {
LinkHit(QString markdown, QString text) noexcept
explicit LinkHit(QString markdown = "", QString text = "") noexcept
: markdown(std::move(markdown)), text(std::move(text)) {}

bool isEmpty() const noexcept { return markdown.isEmpty() && text.isEmpty(); }
Expand Down

0 comments on commit 2d524da

Please sign in to comment.