From 346188e835e65c190c80d2238e87738664d15f45 Mon Sep 17 00:00:00 2001 From: Marty Date: Wed, 28 Nov 2018 19:16:19 +0000 Subject: [PATCH] Refactor into distinct anchor and ID each time the Zettel ID is referred to it is called an anchor. Whenever the internal note ID is referred to it is called an id. --- zettel.qml | 99 +++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/zettel.qml b/zettel.qml index 05a4761..d364774 100644 --- a/zettel.qml +++ b/zettel.qml @@ -4,7 +4,7 @@ import com.qownnotes.noteapi 1.0 QtObject { property bool anchorInFilename; - property string idMarker; + property string anchorMarker; property bool wikiLinks; property string template; @@ -12,14 +12,14 @@ QtObject { { "identifier": "anchorInFilename", "name": "Display Zettel anchors in note filenames", - "description": "By default, the anchor of a Zettel will appear in its body, prepended with its ID Marker selected below.\nIf enabled, this will instead put the anchor in the filename itself and disregard any anchors in Zettel bodies.", + "description": "By default, the anchor of a Zettel will appear in its body, prefixed with the anchor marker selected below.\nIf enabled, this will instead put the anchor in the filename itself and disregard any anchors in Zettel bodies.", "type": "boolean", "default": "false", }, { - "identifier": "idMarker", + "identifier": "anchorMarker", "name": "Zettelkasten anchor marker", - "description": "The in-text marker denoting that a Zettel ID is following.\nMake sure to use something which is not ambiguous, and does not appear in any other way than to actually denote the Zettel ID.\nMultiple characters are fine (as in the default '§§')\nIf 'In-Title Anchors' are selected above this option has no effect.", + "description": "The in-text marker denoting a Zettel anchor is following.\nMake sure to use something which is not ambiguous, and does not appear in any other way than to actually denote a Zettel anchor.\nMultiple characters are fine (as in the default '§§')\nIf 'In-Title Anchors' is selected above this option has no effect.", "type": "string", "default": "§§", }, @@ -64,41 +64,41 @@ QtObject { // ---------------------------- function onZettelCreateClicked() { - let uid = exactDate(); + let anchor = exactDate(); let selected = script.noteTextEditSelectedText().trim(); // Link the from the old Zettel to the new one if anything was selected if(selected !== "") { - createLinkInPlace(uid, selected); + createLinkInPlace(anchor, selected); } // Create a Zettel - createZettel(uid, selected); + createZettel(anchor, selected); } function onInsertLinkClicked() { let selected = script.noteTextEditSelectedText(); let clipboard = script.clipboard(); - let uid = null - if (verifyZettelId(clipboard)) { - uid = verifyZettelId(clipboard); + let anchor = null + if (verifyAnchor(clipboard)) { + anchor = verifyAnchor(clipboard); } else { let otherNote = zettelSelectorDialog(true); - uid = extractZettelIdFromString(otherNote.noteText); + anchor = extractAnchorFromString(otherNote.noteText); } - if (uid == null) { + if (anchor == null) { return; } - createLinkInPlace(uid, selected) + createLinkInPlace(anchor, selected) } function onJumpToZettelClicked() { - let selected = verifyZettelId(script.noteTextEditSelectedText(), true) ? verifyZettelId(script.noteTextEditSelectedText(), true) : verifyZettelId(script.noteTextEditCurrentWord(true), true); + let selected = verifyAnchor(script.noteTextEditSelectedText(), true) ? verifyAnchor(script.noteTextEditSelectedText(), true) : verifyAnchor(script.noteTextEditCurrentWord(true), true); if (selected) { - setNoteToZettelIdOrElse(selected, function(uid) {script.informationMessageBox("Zettel " + uid + " does not exist.")}); + setNoteToAnchorOrElse(selected, function(anchor) {script.informationMessageBox("Zettel " + anchor + " does not exist.")}); } else { script.setCurrentNote(zettelSelectorDialog(true)); } @@ -109,13 +109,13 @@ QtObject { // ---------------------------- function autocompletionHook() { - let uid = verifyZettelId(script.noteTextEditCurrentWord(true), true); + let anchor = verifyAnchor(script.noteTextEditCurrentWord(true), true); - if(uid == false) { + if(anchor == false) { return []; } - setNoteToZettelIdOrElse(uid); + setNoteToAnchorOrElse(anchor); return []; } @@ -142,8 +142,8 @@ QtObject { return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5)); } - function extractZettelIdFromString(text) { - let regex = new RegExp(idMarker + '\\d{14}\\s') + function extractAnchorFromString(text) { + let regex = new RegExp(anchorMarker + '\\d{14}\\s') let markerpos = text.search(regex) + 2; if (markerpos == -1) { return; @@ -151,42 +151,43 @@ QtObject { return text.substring(markerpos, markerpos+14); } - function stripZettelId(link) { + function stripAnchor(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); + function verifyAnchor(anchor, shouldStrip) { + if (shouldStrip) anchor = stripAnchor(anchor); + + if (anchor.match(/^\d{14}$/) != null) { + return anchor; + } else if (anchor.match(new RegExp(anchorMarker + '^\\d{14}$')) != null) { + return anchor.substring(2); } else { return false; } } - // Returns List of Zettel ids with the specified uID. - // If no uID provided returns all Zettel found in the current directory. - function fetchZettelIDs(uid) { - let uid = uid ? uid : ""; - let noteIds = anchorInFilename ? _getAllZettelFromTitle(uid) : _getAllZettelFromAnchor(uid); + // Returns List of Zettel ids with the specified anchor. + // If no anchor provided returns all Zettel found in the current directory. + function fetchZettelIDs(anchor) { + let anchor = anchor ? anchor : ""; + let noteIds = anchorInFilename ? _getAllZettelFromTitle(anchor) : _getAllZettelFromAnchor(anchor); return noteIds; } - // Looks for the combination of idMarker and uID (optionally) in all note texts. - function _getAllZettelFromAnchor(optionaluId) { - return script.fetchNoteIdsByNoteTextPart(idMarker + optionaluId); + // Looks for the combination of anchorMarker and anchor (optionally) in all note texts. + function _getAllZettelFromAnchor(anchor) { + return script.fetchNoteIdsByNoteTextPart(anchorMarker + anchor); } - // Looks for the uID (optionally) in all note texts. - function _getAllZettelFromTitle(optionaluID) { + // Looks for the anchor (optionally) in all note texts. + function _getAllZettelFromTitle(anchor) { let allnotes = script.fetchNoteIdsByNoteTextPart(""); let noteIds = []; - if (optionaluID != "") { + if (anchor != "") { allnotes.forEach(function(noteId) { - if (script.fetchNoteById(noteId).name.startsWith(optionaluID)) noteIds.push(noteId); + if (script.fetchNoteById(noteId).name.startsWith(anchor)) noteIds.push(noteId); }); return noteIds; } @@ -198,28 +199,28 @@ QtObject { return noteIds; } - function getNoteByZettelId(uid) { - let noteId = fetchZettelIDs(uid); + function getNoteByAnchor(anchor) { + let noteId = fetchZettelIDs(anchor); if (noteId.length == 0) { return; } return script.fetchNoteById(noteId[0]); } - function setNoteToZettelIdOrElse(uid, orElse) { - let zettel = getNoteByZettelId(uid); + function setNoteToAnchorOrElse(anchor, orElse) { + let zettel = getNoteByAnchor(anchor); if (zettel == null) { - if(orElse != null) orElse(uid); + if(orElse != null) orElse(anchor); return; } script.setCurrentNote(zettel); } - function createLinkInPlace(uid, selected) { - wikiLinks ? script.noteTextEditWrite("[[" + uid + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + uid + ")"); + function createLinkInPlace(anchor, selected) { + wikiLinks ? script.noteTextEditWrite("[[" + anchor + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + anchor + ")"); } - function createZettel(uid, headline) { + function createZettel(anchor, headline) { // Create Headline - empty if nothing selected if (headline == "") { headline = "Zettel" @@ -227,9 +228,9 @@ QtObject { let text = template; if (anchorInFilename) { - text = text.replace(/\{headline\}/, uid.toString() + " " + headline).replace(/\{anchor\}/, ""); + text = text.replace(/\{headline\}/, anchor.toString() + " " + headline).replace(/\{anchor\}/, ""); } else { - text = text.replace(/\{headline\}/, headline).replace(/\{anchor\}/, idMarker + uid.toString()); + text = text.replace(/\{headline\}/, headline).replace(/\{anchor\}/, anchorMarker + anchor.toString()); } script.createNote(text); -- 2.45.2