Skip to content

Commit

Permalink
#3200 add: start with outgoing link rewriting implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Jan 13, 2025
1 parent 8369e5f commit 4449370
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
43 changes: 39 additions & 4 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3414,14 +3414,27 @@ QString Note::relativeFilePath(const QString &path) const {
* @param oldNote
* @return true if we had to change the current note
*/
bool Note::handleNoteMoving(const Note &oldNote) {
bool Note::handleNoteMoving(Note oldNote) {
const QVector<int> noteIdList = oldNote.findLinkedNoteIds();
const int noteCount = noteIdList.count();

if (noteCount == 0) {
return false;
const auto reverseLinkNotes = oldNote.findReverseLinkNotes();
const int reverseLinkNotesCount = reverseLinkNotes.count();
bool result = false;

if (noteCount >= 0) {
result = handleLinkedNotesAfterMoving(oldNote, noteIdList);
}

if (reverseLinkNotesCount > 0) {
result |= handleReverseLinkedNotesAfterMoving(oldNote, reverseLinkNotes);
}

return result;
}

bool Note::handleLinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList) {
const int noteCount = noteIdList.count();
const QString oldUrl = getNoteURL(oldNote.getName());
const QString newUrl = getNoteURL(_name);

Expand All @@ -3438,7 +3451,7 @@ bool Note::handleNoteMoving(const Note &oldNote) {
QStringLiteral("note-replace-links")) == QMessageBox::Yes) {
// replace the urls in all found notes
for (const int noteId : noteIdList) {
Note note = Note::fetch(noteId);
Note note = fetch(noteId);

if (!note.isFetched()) {
continue;
Expand Down Expand Up @@ -3501,6 +3514,23 @@ bool Note::handleNoteMoving(const Note &oldNote) {
return noteIdList.contains(_id);
}

bool Note::handleReverseLinkedNotesAfterMoving(
const Note &oldNote, const QHash<Note, QSet<BacklinkHit>> &reverseLinkNotes) {
// Iterate over reverseLinkNotes
for (auto it = reverseLinkNotes.begin(); it != reverseLinkNotes.end(); ++it) {
const Note &backlinkNote = it.key();
const QSet<BacklinkHit> &linkTextList = it.value();

qDebug() << __func__ << " - 'oldNote': " << oldNote;

qDebug() << __func__ << " - 'backlinkNote': " << backlinkNote;
qDebug() << __func__ << " - 'linkTextList': " << linkTextList;
}

// TODO: Change to true if we had to change the current note
return false;
}

QSet<Note> Note::findBacklinks() const {
const QVector<int> noteIdList = this->findLinkedNoteIds();
const int noteCount = noteIdList.count();
Expand Down Expand Up @@ -4170,3 +4200,8 @@ bool Note::operator==(const Note &note) const {
return _id == note.getId() && _fileName == note.getFileName() &&
_noteSubFolderId == note.getNoteSubFolderId();
}

QDebug operator<<(QDebug dbg, const BacklinkHit &hit) {
dbg.nospace() << "BacklinkHit(markdown: " << hit.markdown << ", text: " << hit.text << ')';
return dbg.space();
}
7 changes: 6 additions & 1 deletion src/entities/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct BacklinkHit {
return markdown == other.markdown && text == other.text;
}

friend QDebug operator<<(QDebug dbg, const BacklinkHit &hit);

QString markdown;
QString text;
};
Expand Down Expand Up @@ -272,7 +274,7 @@ class Note {

QVector<int> findLinkedNoteIds() const;

bool handleNoteMoving(const Note &oldNote);
bool handleNoteMoving(Note oldNote);

static QString createNoteHeader(const QString &name);

Expand Down Expand Up @@ -422,6 +424,9 @@ class Note {

void addTextToBacklinkNoteHashIfFound(const Note &note, const QString &pattern);
void addTextToBacklinkNoteHashIfFound(const Note &note, const QRegularExpression &pattern);
bool handleReverseLinkedNotesAfterMoving(
const Note &oldNote, const QHash<Note, QSet<BacklinkHit>> &reverseLinkNotes);
bool handleLinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList);
};

inline uint qHash(const Note &note, uint seed) { return qHash(note.getId(), seed); }
Expand Down

0 comments on commit 4449370

Please sign in to comment.