Skip to content

Commit db593d1

Browse files
authored
Add script Text2link (#268)
* New script text2linq * Delete text2link/text2linq.qml * Add files via upload * Update info.json * Update info.json * Delete text2link/text2linq.qml * Add files via upload * Update info.json * Update text2link.qml * Adding a MessageBox showing stats
1 parent f1e629a commit db593d1

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

text2link/info.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Text2link",
3+
"identifier": "text2link",
4+
"script": "text2link.qml",
5+
"authors": [
6+
"@Glin76"
7+
],
8+
"platforms": [
9+
"linux",
10+
"macos",
11+
"windows"
12+
],
13+
"version": "0.0.1",
14+
"minAppVersion": "25.05.3",
15+
"description": "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."
16+
}

text2link/text2link.qml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)