Enable WikiStyle links

Creates new option to allow new default style for links. 
Made link following more flexible, allowing for a wide range of
selection or cursor positions.
Ensure a Zettel exists before attempting to jump there.
This commit is contained in:
Marty 2018-11-28 16:49:24 +00:00
parent 1bb44eeae0
commit 01cce369ff

View file

@ -4,6 +4,7 @@ import com.qownnotes.noteapi 1.0
QtObject { QtObject {
property string idMarker; property string idMarker;
property bool wikiLinks;
property variant settingsVariables: [ 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 '§§')", "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", "type": "string",
"default": "§§", "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() { function init() {
@ -45,7 +53,6 @@ QtObject {
// Link the from the old Zettel to the new one if anything was selected // Link the from the old Zettel to the new one if anything was selected
if(selected !== "") { if(selected !== "") {
script.log("selected: " + selected);
createLinkInPlace(uid, selected); createLinkInPlace(uid, selected);
} }
@ -72,12 +79,12 @@ QtObject {
} }
function onJumpToZettelClicked() { 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) { if (selected) {
script.setCurrentNote(zettelSelectorDialog(true)); setNoteToZettelIdOrElse(selected, function(uid) {script.informationMessageBox("Zettel " + uid + " does not exist.")});
} else { } else {
script.setCurrentNote(getNoteByZettelId(selected)); script.setCurrentNote(zettelSelectorDialog(true));
} }
} }
@ -86,17 +93,13 @@ QtObject {
// ---------------------------- // ----------------------------
function autocompletionHook() { function autocompletionHook() {
let uid = verifyZettelId(script.noteTextEditCurrentWord(false)); let uid = verifyZettelId(script.noteTextEditCurrentWord(true), true);
if(uid == false) { if(uid == false) {
return []; return [];
} }
let zettel = getNoteByZettelId(uid); setNoteToZettelIdOrElse(uid);
script.log("Autocomplete detected, Switch to " + idMarker + uid + " - " + zettel.name)
script.setCurrentNote(zettel);
return []; return [];
} }
@ -115,7 +118,7 @@ QtObject {
} }
let selected = script.inputDialogGetItem("Zettel", "Select a Zettel", zettelArray, 0, editableTextBox); 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)); return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5));
} }
@ -123,17 +126,21 @@ QtObject {
let regex = new RegExp(idMarker + '\\d{14}\\s') let regex = new RegExp(idMarker + '\\d{14}\\s')
let markerpos = text.search(regex) + 2; let markerpos = text.search(regex) + 2;
if (markerpos == -1) { if (markerpos == -1) {
script.log("No Zettel ID Found in text");
return; return;
} }
return text.substring(markerpos, markerpos+14); return text.substring(markerpos, markerpos+14);
} }
function verifyZettelId(id) { function stripZettelId(link) {
if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) { return link.replace(/\D*/g, "");
return id.substring(2); }
} else if (id.match(/^\d{14}$/) != null) {
function verifyZettelId(id, shouldStrip) {
if (shouldStrip) id = stripZettelId(id);
if (id.match(/^\d{14}$/) != null) {
return id; return id;
} else if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) {
return id.substring(2);
} else { } else {
return false; return false;
} }
@ -142,14 +149,22 @@ QtObject {
function getNoteByZettelId(uid) { function getNoteByZettelId(uid) {
let noteId = script.fetchNoteIdsByNoteTextPart(idMarker + uid); let noteId = script.fetchNoteIdsByNoteTextPart(idMarker + uid);
if (noteId.length == 0) { if (noteId.length == 0) {
script.log("No note found for Zettel ID: " + idMarker + uid);
return; return;
} }
return script.fetchNoteById(noteId[0]); 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) { function createLinkInPlace(uid, selected) {
script.noteTextEditWrite("["+selected+"](zettel://"+uid+")"); wikiLinks ? script.noteTextEditWrite("[[" + uid + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + uid + ")");
} }
function createZettel(uid, headline) { function createZettel(uid, headline) {
@ -193,4 +208,4 @@ QtObject {
function padToLen2(text) { function padToLen2(text) {
return ("00" + text).slice(-2) return ("00" + text).slice(-2)
} }
} }