Skip to content

Commit 4449370

Browse files
committed
#3200 add: start with outgoing link rewriting implementation
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent 8369e5f commit 4449370

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/entities/note.cpp

+39-4
Original file line numberDiff line numberDiff line change
@@ -3414,14 +3414,27 @@ QString Note::relativeFilePath(const QString &path) const {
34143414
* @param oldNote
34153415
* @return true if we had to change the current note
34163416
*/
3417-
bool Note::handleNoteMoving(const Note &oldNote) {
3417+
bool Note::handleNoteMoving(Note oldNote) {
34183418
const QVector<int> noteIdList = oldNote.findLinkedNoteIds();
34193419
const int noteCount = noteIdList.count();
34203420

3421-
if (noteCount == 0) {
3422-
return false;
3421+
const auto reverseLinkNotes = oldNote.findReverseLinkNotes();
3422+
const int reverseLinkNotesCount = reverseLinkNotes.count();
3423+
bool result = false;
3424+
3425+
if (noteCount >= 0) {
3426+
result = handleLinkedNotesAfterMoving(oldNote, noteIdList);
3427+
}
3428+
3429+
if (reverseLinkNotesCount > 0) {
3430+
result |= handleReverseLinkedNotesAfterMoving(oldNote, reverseLinkNotes);
34233431
}
34243432

3433+
return result;
3434+
}
3435+
3436+
bool Note::handleLinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList) {
3437+
const int noteCount = noteIdList.count();
34253438
const QString oldUrl = getNoteURL(oldNote.getName());
34263439
const QString newUrl = getNoteURL(_name);
34273440

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

34433456
if (!note.isFetched()) {
34443457
continue;
@@ -3501,6 +3514,23 @@ bool Note::handleNoteMoving(const Note &oldNote) {
35013514
return noteIdList.contains(_id);
35023515
}
35033516

3517+
bool Note::handleReverseLinkedNotesAfterMoving(
3518+
const Note &oldNote, const QHash<Note, QSet<BacklinkHit>> &reverseLinkNotes) {
3519+
// Iterate over reverseLinkNotes
3520+
for (auto it = reverseLinkNotes.begin(); it != reverseLinkNotes.end(); ++it) {
3521+
const Note &backlinkNote = it.key();
3522+
const QSet<BacklinkHit> &linkTextList = it.value();
3523+
3524+
qDebug() << __func__ << " - 'oldNote': " << oldNote;
3525+
3526+
qDebug() << __func__ << " - 'backlinkNote': " << backlinkNote;
3527+
qDebug() << __func__ << " - 'linkTextList': " << linkTextList;
3528+
}
3529+
3530+
// TODO: Change to true if we had to change the current note
3531+
return false;
3532+
}
3533+
35043534
QSet<Note> Note::findBacklinks() const {
35053535
const QVector<int> noteIdList = this->findLinkedNoteIds();
35063536
const int noteCount = noteIdList.count();
@@ -4170,3 +4200,8 @@ bool Note::operator==(const Note &note) const {
41704200
return _id == note.getId() && _fileName == note.getFileName() &&
41714201
_noteSubFolderId == note.getNoteSubFolderId();
41724202
}
4203+
4204+
QDebug operator<<(QDebug dbg, const BacklinkHit &hit) {
4205+
dbg.nospace() << "BacklinkHit(markdown: " << hit.markdown << ", text: " << hit.text << ')';
4206+
return dbg.space();
4207+
}

src/entities/note.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct BacklinkHit {
2525
return markdown == other.markdown && text == other.text;
2626
}
2727

28+
friend QDebug operator<<(QDebug dbg, const BacklinkHit &hit);
29+
2830
QString markdown;
2931
QString text;
3032
};
@@ -272,7 +274,7 @@ class Note {
272274

273275
QVector<int> findLinkedNoteIds() const;
274276

275-
bool handleNoteMoving(const Note &oldNote);
277+
bool handleNoteMoving(Note oldNote);
276278

277279
static QString createNoteHeader(const QString &name);
278280

@@ -422,6 +424,9 @@ class Note {
422424

423425
void addTextToBacklinkNoteHashIfFound(const Note &note, const QString &pattern);
424426
void addTextToBacklinkNoteHashIfFound(const Note &note, const QRegularExpression &pattern);
427+
bool handleReverseLinkedNotesAfterMoving(
428+
const Note &oldNote, const QHash<Note, QSet<BacklinkHit>> &reverseLinkNotes);
429+
bool handleLinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList);
425430
};
426431

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

0 commit comments

Comments
 (0)