|
| 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