Skip to content

Commit 70bfbb6

Browse files
authored
Adding new script RemoveDeadNoteLink (#269)
* Adding new script RemoveDeadNoteLink * Rename removedeadnotelink.qml to removedeadnotelink.qml * Update and rename info.json to info.json * Update and rename info.json to info.json * Rename removedeadnotelink.qml to removedeadnotelink.qml * Update info.json * Update info.json
1 parent bb46a65 commit 70bfbb6

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎removedeadnotelink/info.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "RemoveDeadNoteLink",
3+
"identifier": "removedeadnotelink",
4+
"script": "removedeadnotelink.qml",
5+
"authors": ["@Glin76"],
6+
"platforms": ["linux", "macos", "windows"],
7+
"version": "0.0.1",
8+
"minAppVersion": "25.6.1",
9+
"description": "This script creates a menu item and a button that searches through all notes and removes links to all notes (Markdown files) that no longer exist, also known as dead note links."
10+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import QtQml 2.0
2+
3+
/**
4+
* This script creates a menu item and a button that search in all notes and remove links to note (MD file) no longer existing (aka dead note-link)
5+
*/
6+
7+
QtObject {
8+
9+
function customActionInvoked(identifier) {
10+
switch (identifier) {
11+
case "RemoveDeadNoteLink":
12+
var dialogText = "";
13+
var reLink = RegExp("\\[.*\\]\\(.*\\.md\\)", "g"); // RegExp to find links : "[***](***.md)"
14+
script.fetchNoteIdsByNoteTextPart("").forEach(function (noteId){ // Loop for all notes
15+
var linkText = "";
16+
var note = script.fetchNoteById(noteId);
17+
var newNoteText = note.noteText;
18+
var LinkFound = reLink.exec(note.noteText);
19+
while (LinkFound != null){ // Loop for all links in the note
20+
var LinkNoteName = LinkFound.toString().replace(RegExp(".*\\(", "").exec(LinkFound),"") // RegExp to find the name of the link : from start of the link to "(", and then removed from the string
21+
LinkNoteName = LinkNoteName.toString().replace(RegExp(".*\\/", "").exec(LinkNoteName),"") // RegExp to find the relative path : from start to the last"/", and then removed from the string
22+
LinkNoteName = LinkNoteName.replace(")",""); // removing last ")" of the link
23+
while (LinkNoteName.search("%20") > -1) {
24+
LinkNoteName = LinkNoteName.replace("%20", " "); // replacing all the %20 by space in the filename
25+
}
26+
if (script.fetchNoteByFileName(LinkNoteName).id == 0) { // Does a note exist with that filename
27+
newNoteText = newNoteText.replace(LinkFound, ""); // removing the link in the future NoteText
28+
linkText += "\n - "+LinkFound;
29+
}
30+
LinkFound = reLink.exec(note.noteText);
31+
}
32+
if (note.noteText != newNoteText) {
33+
script.setCurrentNote(note);
34+
script.noteTextEditSelectAll();
35+
script.noteTextEditWrite(newNoteText.trim());
36+
dialogText += "\n\nNote : "+note.name+" link(s) removed :"+linkText;
37+
}
38+
});
39+
40+
if (dialogText != "") {
41+
dialogText = "Here's the result of removing dead note-link(s)"+dialogText;
42+
script.informationMessageBox(dialogText);
43+
} else {
44+
script.informationMessageBox("No dead note-link found");
45+
}
46+
break;
47+
}
48+
}
49+
50+
function init() {
51+
script.registerCustomAction("RemoveDeadNoteLink", "Remove dead links to note", "Remove dead note-link", "bookmark-new", true, false, true);
52+
}
53+
}

0 commit comments

Comments
 (0)