Skip to content

Commit

Permalink
#3200 add: more link handling refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Jan 15, 2025
1 parent 8af49d5 commit 82163c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
60 changes: 33 additions & 27 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3082,13 +3082,13 @@ bool Note::isSameFile(const Note &note) const {
}

/**
* Finds notes that link to a note with fileName via legacy note:// links or the
* Finds other notes that link to the note with fileName via legacy note:// links or the
* relative file links
*
* @param fileName
* @return list of note ids
*/
QVector<int> Note::findLinkedNoteIds() const {
QVector<int> Note::findBacklinkedNoteIds() const {
QVector<int> noteIdList;

// search for legacy links
Expand Down Expand Up @@ -3140,13 +3140,13 @@ QVector<int> Note::findLinkedNoteIds() const {
return noteIdList;
}

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

QSet<BacklinkHit> Note::findAndReturnBacklinkHit(const QString &text,
QSet<LinkHit> Note::findAndReturnBacklinkHit(const QString &text,
const QRegularExpression &regex) {
QSet<BacklinkHit> backlinkHits;
QSet<LinkHit> backlinkHits;
QRegularExpressionMatchIterator iterator = regex.globalMatch(text);

while (iterator.hasNext()) {
Expand All @@ -3155,14 +3155,14 @@ QSet<BacklinkHit> Note::findAndReturnBacklinkHit(const QString &text,
QString linkUrl = match.captured(2);
// Do something with the link text and URL

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

return backlinkHits;
}

void Note::addTextToBacklinkNoteHashIfFound(const Note &note, const QString &pattern) {
const BacklinkHit backlinkHit = findAndReturnBacklinkHit(note.getNoteText(), pattern);
const LinkHit backlinkHit = findAndReturnBacklinkHit(note.getNoteText(), pattern);

if (!backlinkHit.isEmpty()) {
if (!_backlinkNoteHash.contains(note)) {
Expand All @@ -3185,8 +3185,14 @@ void Note::addTextToBacklinkNoteHashIfFound(const Note &note, const QRegularExpr
}
}

QHash<Note, QSet<BacklinkHit>> Note::findReverseLinkNotes() {
const QVector<int> noteIdList = this->findLinkedNoteIds();
QHash<Note, QSet<LinkHit>> Note::findLinkedNotes() {
// TODO: Implement this, but with linked notes
const QVector<int> noteIdList = this->findBacklinkedNoteIds();
return {};
}

QHash<Note, QSet<LinkHit>> Note::findReverseLinkNotes() {
const QVector<int> noteIdList = this->findBacklinkedNoteIds();
const int noteCount = noteIdList.count();

if (noteCount == 0) {
Expand Down Expand Up @@ -3415,29 +3421,29 @@ QString Note::relativeFilePath(const QString &path) const {
* @return true if we had to change the current note
*/
bool Note::handleNoteMoving(Note oldNote) {
const QVector<int> noteIdList = oldNote.findLinkedNoteIds();
const QVector<int> noteIdList = oldNote.findBacklinkedNoteIds();
const int noteCount = noteIdList.count();
bool result = false;

// Handle incoming note links
if (noteCount >= 0) {
result = handleLinkedNotesAfterMoving(oldNote, noteIdList);
result = handleBacklinkedNotesAfterMoving(oldNote, noteIdList);
}

// Handle outgoing note links (only needed if subfolder was changed)
if (oldNote.getNoteSubFolderId() != getNoteSubFolderId()) {
const auto reverseLinkNotes = oldNote.findReverseLinkNotes();
const int reverseLinkNotesCount = reverseLinkNotes.count();
const auto linkedNoteHits = oldNote.findLinkedNotes();
const int linkedNotesCount = linkedNoteHits.count();

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

return result;
}

bool Note::handleLinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList) {
bool Note::handleBacklinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList) {
const int noteCount = noteIdList.count();
const QString oldUrl = getNoteURL(oldNote.getName());
const QString newUrl = getNoteURL(_name);
Expand Down Expand Up @@ -3518,16 +3524,16 @@ bool Note::handleLinkedNotesAfterMoving(const Note &oldNote, const QVector<int>
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();
bool Note::handleLinkedNotesAfterMoving(
const Note &oldNote, const QHash<Note, QSet<LinkHit>> &linkedNoteHits) {
// Iterate over linkedNoteHits
for (auto it = linkedNoteHits.begin(); it != linkedNoteHits.end(); ++it) {
const Note &linkedNote = it.key();
const QSet<LinkHit> &linkTextList = it.value();

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

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

Expand All @@ -3536,7 +3542,7 @@ bool Note::handleReverseLinkedNotesAfterMoving(
}

QSet<Note> Note::findBacklinks() const {
const QVector<int> noteIdList = this->findLinkedNoteIds();
const QVector<int> noteIdList = this->findBacklinkedNoteIds();
const int noteCount = noteIdList.count();

if (noteCount == 0) {
Expand Down Expand Up @@ -4205,7 +4211,7 @@ bool Note::operator==(const Note &note) const {
_noteSubFolderId == note.getNoteSubFolderId();
}

QDebug operator<<(QDebug dbg, const BacklinkHit &hit) {
dbg.nospace() << "BacklinkHit(markdown: " << hit.markdown << ", text: " << hit.text << ')';
QDebug operator<<(QDebug dbg, const LinkHit &hit) {
dbg.nospace() << "LinkHit(markdown: " << hit.markdown << ", text: " << hit.text << ')';
return dbg.space();
}
30 changes: 16 additions & 14 deletions src/entities/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ class QFile;
class QUrl;
class QSqlQuery;

struct BacklinkHit {
BacklinkHit(QString markdown, QString text) noexcept
struct LinkHit {
LinkHit(QString markdown, QString text) noexcept
: markdown(std::move(markdown)), text(std::move(text)) {}

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

// Add operator== for comparison
bool operator==(const BacklinkHit &other) const {
bool operator==(const LinkHit &other) const {
return markdown == other.markdown && text == other.text;
}

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

QString markdown;
QString text;
};

// Add hash function for BacklinkHit
inline uint qHash(const BacklinkHit &hit, uint seed = 0) {
// Add hash function for LinkHit
inline uint qHash(const LinkHit &hit, uint seed = 0) {
return qHash(hit.markdown, seed) ^ qHash(hit.text, seed);
}

Expand Down Expand Up @@ -272,7 +272,7 @@ class Note {

static QVector<int> fetchAllIds(int limit = -1, int offset = -1);

QVector<int> findLinkedNoteIds() const;
QVector<int> findBacklinkedNoteIds() const;

bool handleNoteMoving(Note oldNote);

Expand Down Expand Up @@ -385,7 +385,9 @@ class Note {

QSet<Note> findBacklinks() const;

QHash<Note, QSet<BacklinkHit>> findReverseLinkNotes();
QHash<Note, QSet<LinkHit>> findLinkedNotes();

QHash<Note, QSet<LinkHit>> findReverseLinkNotes();

protected:
int _id;
Expand All @@ -407,7 +409,7 @@ class Note {
int _shareId;
unsigned int _sharePermissions;
bool _hasDirtyData;
QHash<Note, QSet<BacklinkHit>> _backlinkNoteHash;
QHash<Note, QSet<LinkHit>> _backlinkNoteHash;

static QRegularExpression getEncryptedNoteTextRegularExpression();
QString getEncryptedNoteText() const;
Expand All @@ -418,15 +420,15 @@ class Note {

void restoreCreatedDate();

static BacklinkHit findAndReturnBacklinkHit(const QString &text, const QString &pattern);
static QSet<BacklinkHit> findAndReturnBacklinkHit(const QString &text,
static LinkHit findAndReturnBacklinkHit(const QString &text, const QString &pattern);
static QSet<LinkHit> findAndReturnBacklinkHit(const QString &text,
const QRegularExpression &regex);

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);
bool handleLinkedNotesAfterMoving(
const Note &oldNote, const QHash<Note, QSet<LinkHit>> &linkedNoteHits);
bool handleBacklinkedNotesAfterMoving(const Note &oldNote, const QVector<int> &noteIdList);
};

inline uint qHash(const Note &note, uint seed) { return qHash(note.getId(), seed); }
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/backlinkwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void BacklinkWidget::findBacklinks(Note note) {
// Iterate over reverseLinkNotes
for (auto it = reverseLinkNotes.begin(); it != reverseLinkNotes.end(); ++it) {
const Note &backlinkNote = it.key();
const QSet<BacklinkHit> &linkTextList = it.value();
const QSet<LinkHit> &linkTextList = it.value();

auto *topItem = new QTreeWidgetItem();

Expand All @@ -76,7 +76,7 @@ void BacklinkWidget::findBacklinks(Note note) {

addTopLevelItem(topItem);

for (const BacklinkHit &linkHit : linkTextList) {
for (const LinkHit &linkHit : linkTextList) {
auto *item = new QTreeWidgetItem();

item->setText(0, linkHit.text);
Expand Down

0 comments on commit 82163c4

Please sign in to comment.