Skip to content

Commit

Permalink
#1717 backlinks: show partial link in tree
Browse files Browse the repository at this point in the history
Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Sep 2, 2024
1 parent 6e2afc8 commit d5902bf
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
73 changes: 71 additions & 2 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ QVector<int> Note::searchInNotes(QString search, bool ignoreNoteSubFolder, int n

// we want to search for the text in the note text and the filename
for (int i = 0; i < queryStrings.count(); i++) {
const QString queryString = queryStrings[i];
const QString &queryString = queryStrings[i];

// if we just want to search in the name we use different columns
// skip encrypted notes if search term is not found in (file) name of note
Expand Down Expand Up @@ -3086,7 +3086,7 @@ QVector<int> Note::findLinkedNoteIds() const {
noteIdList << searchInNotes(QChar('<') + linkText + QChar('>'), true);
noteIdList << searchInNotes(QStringLiteral("](") + linkText + QStringLiteral(")"), true);

// search vor legacy links ending with "@"
// search for legacy links ending with "@"
const QString altLinkText = Utils::Misc::appendIfDoesNotEndWith(linkText, QStringLiteral("@"));
if (altLinkText != linkText) {
noteIdList << searchInNotes(QChar('<') + altLinkText + QChar('>'), true);
Expand Down Expand Up @@ -3126,6 +3126,75 @@ QVector<int> Note::findLinkedNoteIds() const {
return noteIdList;
}

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

void Note::addTextToBacklinkNoteHashIfFound(const Note &note, const QString &text,
const QString &pattern) {
const QString found = findAndReturnString(text, pattern);

if (!found.isEmpty()) {
if (!_backlinkNoteHash.contains(note)) {
_backlinkNoteHash.insert(note, QStringList() << found);
} else {
_backlinkNoteHash[note] << found;
}
}
}

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

if (noteCount == 0) {
return {};
}

_backlinkNoteHash.clear();
const QString noteUrl = getNoteURL(this->getName());

for (const int noteId : noteIdList) {
Note note = Note::fetch(noteId);

if (!note.isFetched()) {
continue;
}

QString noteText = note.getNoteText();

// search legacy links
addTextToBacklinkNoteHashIfFound(note, noteText,
QStringLiteral("<") + noteUrl + QStringLiteral(">"));
addTextToBacklinkNoteHashIfFound(note, noteText,
QStringLiteral("](") + noteUrl + QStringLiteral(")"));

// search for legacy links ending with "@"
const QString altLinkText =
Utils::Misc::appendIfDoesNotEndWith(noteUrl, QStringLiteral("@"));
if (altLinkText != noteUrl) {
addTextToBacklinkNoteHashIfFound(
note, noteText, QStringLiteral("<") + altLinkText + QStringLiteral(">"));
addTextToBacklinkNoteHashIfFound(
note, noteText, QStringLiteral("](") + altLinkText + QStringLiteral(")"));
}

const QString relativeFilePath =
Note::urlEncodeNoteUrl(note.getFilePathRelativeToNote(*this));

// search for links to the relative file path in note
// the "#" is for notes with a fragment (link to heading in note)
addTextToBacklinkNoteHashIfFound(
note, noteText, QStringLiteral("<") + relativeFilePath + QStringLiteral(">"));
addTextToBacklinkNoteHashIfFound(
note, noteText, QStringLiteral("](") + relativeFilePath + QStringLiteral(")"));
addTextToBacklinkNoteHashIfFound(
note, noteText, QStringLiteral("](") + relativeFilePath + QStringLiteral("#"));
}

return _backlinkNoteHash;
}

/**
* Returns a (legacy) url to a note
*
Expand Down
10 changes: 10 additions & 0 deletions src/entities/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ class Note {

QList<Note> findBacklinks() const;

QHash<Note, QStringList> findReverseLinkNotes();

protected:
int _id;
int _noteSubFolderId;
Expand All @@ -382,6 +384,7 @@ class Note {
int _shareId;
unsigned int _sharePermissions;
bool _hasDirtyData;
QHash<Note, QStringList> _backlinkNoteHash;

static QRegularExpression getEncryptedNoteTextRegularExpression();
QString getEncryptedNoteText() const;
Expand All @@ -391,8 +394,15 @@ class Note {
static const QString getNoteURLFromFileName(const QString &fileName);

void restoreCreatedDate();

QString findAndReturnString(const QString &text, const QString &pattern);

void addTextToBacklinkNoteHashIfFound(const Note &note, const QString &text,
const QString &pattern);
};

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

Q_DECLARE_TYPEINFO(Note, Q_MOVABLE_TYPE);

#endif // NOTE_H
28 changes: 20 additions & 8 deletions src/widgets/backlinkwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,30 @@ void BacklinkWidget::onItemClicked(QTreeWidgetItem *current, int column) {
*/
void BacklinkWidget::findBacklinks(Note note) {
clear();
auto notes = note.findBacklinks();
QHash<Note, QStringList> reverseLinkNotes = note.findReverseLinkNotes();

qDebug() << __func__ << " - 'notes count': " << notes.count();
// Iterate over reverseLinkNotes
for (auto it = reverseLinkNotes.begin(); it != reverseLinkNotes.end(); ++it) {
const Note &backlinkNote = it.key();
const QStringList &linkTextList = it.value();

foreach (const Note &backlinkNote, notes) {
auto *item = new QTreeWidgetItem();
auto *topItem = new QTreeWidgetItem();

item->setText(0, backlinkNote.getName());
item->setData(0, Qt::UserRole, backlinkNote.getId());
item->setToolTip(0, tr("Open note"));
topItem->setText(0, backlinkNote.getName());
// Disable selection for the top items
topItem->setFlags(topItem->flags() & ~Qt::ItemIsSelectable);

addTopLevelItem(item);
addTopLevelItem(topItem);

for (const QString &linkText : linkTextList) {
auto *item = new QTreeWidgetItem();

item->setText(0, linkText);
item->setData(0, Qt::UserRole, backlinkNote.getId());
item->setToolTip(0, tr("Open note"));

topItem->addChild(item);
}
}

expandAll();
Expand Down

0 comments on commit d5902bf

Please sign in to comment.