|
| 1 | +import QtQml 2.0 |
| 2 | + |
| 3 | +/** |
| 4 | + * This script creates links to all notes containing the selected text at the end of the current note. Caution ! This script doesn't work with note-subfolders. |
| 5 | + */ |
| 6 | +QtObject { |
| 7 | + |
| 8 | + function init() { |
| 9 | + // create the menu entry |
| 10 | + script.registerCustomAction("Text2link", "Create links for all notes containing selected text", "Text 2 link", "bookmark-new", true, false, true); |
| 11 | + } |
| 12 | + |
| 13 | + function customActionInvoked(identifier) { |
| 14 | + switch (identifier) { |
| 15 | + case "Text2link": |
| 16 | + |
| 17 | + var text = script.noteTextEditSelectedText(); |
| 18 | + if (text == "") {break;} |
| 19 | + var foundedNotes = -1; // Current note will not be counted |
| 20 | + var addedLinks = 0; |
| 21 | + var oldLinks = 0; |
| 22 | + // loop for all notes containing the raw text |
| 23 | + script.fetchNoteIdsByNoteTextPart(text).forEach(function (noteId) { |
| 24 | + var note = script.fetchNoteById(noteId); |
| 25 | + // first condition : text should be a complete word in the note |
| 26 | + var reTest = RegExp("\\b"+text+"\\b","gi").exec(note.noteText); |
| 27 | + // second condition : the note should not be already linked in this note |
| 28 | + var link = "("+note.fileName+")"; |
| 29 | + while (link.search(" ") > -1) { // need to loop for each space because .replace() only works once |
| 30 | + link = link.replace(" ","%20") |
| 31 | + } |
| 32 | + var alreadyLinked = script.currentNote().noteText.search(link) |
| 33 | + // third condition : the note should not be self |
| 34 | + if (reTest != null & alreadyLinked == -1 & script.currentNote().id != noteId) { |
| 35 | + script.noteTextEditSetCursorPosition(-1); // end of the this note |
| 36 | + script.noteTextEditWrite("\n\n"+"["+note.name+"]"+link); // add a blank line and the link |
| 37 | + addedLinks += 1; |
| 38 | + } |
| 39 | + if (reTest != null) { foundedNotes += 1;} |
| 40 | + if (alreadyLinked > 0) { oldLinks += 1;} |
| 41 | + }); |
| 42 | + script.informationMessageBox(foundedNotes+" note(s) containing '"+text+"'\n"+oldLinks+" already linked\n"+addedLinks+" added", "Results"); |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments