Resolve "Turn links to WikiStyle Links / Standard Markdown Links" #11

Merged
marty-oehme merged 2 commits from 4-turn-links-to-wikistyle-links-standard-markdown-links into master 2024-06-24 06:39:52 +00:00
Showing only changes of commit 01cce369ff - Show all commits

View file

@ -4,6 +4,7 @@ import com.qownnotes.noteapi 1.0
QtObject {
property string idMarker;
property bool wikiLinks;
property variant settingsVariables: [
{
@ -12,7 +13,14 @@ QtObject {
"description": "The in-text marker denoting that a Zettel ID is following. Make sure to use something which is not ambiguous, and does not appear in any other way than to actually denote the Zettel ID. Multiple characters are fine (as in the default '§§')",
"type": "string",
"default": "§§",
}
},
{
"identifier": "wikiLinks",
"name": "Default to [[WikiLinks]]",
"description": "By default, links will take the form of standard markdown links. This will make automatically created links between Zettel take the form of wiki-links instead. The go to link function understands both types, regardless of this choice.",
"type": "boolean",
"default": "false",
},
]
function init() {
@ -45,7 +53,6 @@ QtObject {
// Link the from the old Zettel to the new one if anything was selected
if(selected !== "") {
script.log("selected: " + selected);
createLinkInPlace(uid, selected);
}
@ -72,12 +79,12 @@ QtObject {
}
function onJumpToZettelClicked() {
let selected = verifyZettelId(script.noteTextEditSelectedText());
let selected = verifyZettelId(script.noteTextEditSelectedText(), true) ? verifyZettelId(script.noteTextEditSelectedText(), true) : verifyZettelId(script.noteTextEditCurrentWord(true), true);
if(selected == false) {
script.setCurrentNote(zettelSelectorDialog(true));
if (selected) {
setNoteToZettelIdOrElse(selected, function(uid) {script.informationMessageBox("Zettel " + uid + " does not exist.")});
} else {
script.setCurrentNote(getNoteByZettelId(selected));
script.setCurrentNote(zettelSelectorDialog(true));
}
}
@ -86,17 +93,13 @@ QtObject {
// ----------------------------
function autocompletionHook() {
let uid = verifyZettelId(script.noteTextEditCurrentWord(false));
let uid = verifyZettelId(script.noteTextEditCurrentWord(true), true);
if(uid == false) {
return [];
}
let zettel = getNoteByZettelId(uid);
script.log("Autocomplete detected, Switch to " + idMarker + uid + " - " + zettel.name)
script.setCurrentNote(zettel);
setNoteToZettelIdOrElse(uid);
return [];
}
@ -115,7 +118,7 @@ QtObject {
}
let selected = script.inputDialogGetItem("Zettel", "Select a Zettel", zettelArray, 0, editableTextBox);
// FIXME dont display id in names - rather, go getNotebyName(name) -> (get its id) -> set note to it
return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5));
}
@ -123,17 +126,21 @@ QtObject {
let regex = new RegExp(idMarker + '\\d{14}\\s')
let markerpos = text.search(regex) + 2;
if (markerpos == -1) {
script.log("No Zettel ID Found in text");
return;
}
return text.substring(markerpos, markerpos+14);
}
function verifyZettelId(id) {
if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) {
return id.substring(2);
} else if (id.match(/^\d{14}$/) != null) {
function stripZettelId(link) {
return link.replace(/\D*/g, "");
}
function verifyZettelId(id, shouldStrip) {
if (shouldStrip) id = stripZettelId(id);
if (id.match(/^\d{14}$/) != null) {
return id;
} else if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) {
return id.substring(2);
} else {
return false;
}
@ -142,14 +149,22 @@ QtObject {
function getNoteByZettelId(uid) {
let noteId = script.fetchNoteIdsByNoteTextPart(idMarker + uid);
if (noteId.length == 0) {
script.log("No note found for Zettel ID: " + idMarker + uid);
return;
}
return script.fetchNoteById(noteId[0]);
}
function setNoteToZettelIdOrElse(uid, orElse) {
let zettel = getNoteByZettelId(uid);
if (zettel == null) {
if(orElse != null) orElse(uid);
return;
}
script.setCurrentNote(zettel);
}
function createLinkInPlace(uid, selected) {
script.noteTextEditWrite("["+selected+"](zettel://"+uid+")");
wikiLinks ? script.noteTextEditWrite("[[" + uid + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + uid + ")");
}
function createZettel(uid, headline) {