From d3397e86d13c9137503adc6bcade5f4f49e45711 Mon Sep 17 00:00:00 2001 From: Marty Date: Wed, 28 Nov 2018 18:31:21 +0000 Subject: [PATCH] Add Title Anchor Option Script can now be switched to look for Zettel anchors in note names, or in the complete note bodies, prefixed with the selected tag marker. --- zettel.qml | 65 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/zettel.qml b/zettel.qml index 0c7c2fd..00d4c9c 100644 --- a/zettel.qml +++ b/zettel.qml @@ -3,21 +3,29 @@ import QOwnNotesTypes 1.0 import com.qownnotes.noteapi 1.0 QtObject { + property bool anchorInFilename; property string idMarker; property bool wikiLinks; property variant settingsVariables: [ + { + "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.", + "type": "boolean", + "default": "false", + }, { "identifier": "idMarker", - "name": "Zettelkasten ID Marker", - "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 '§§')", + "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.", "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.", + "description": "By default, links will take the form of standard markdown links.\nThis will make automatically created links between Zettel take the form of wiki-links instead.\nThe 'go to link' function understands both types, regardless of this choice.", "type": "boolean", "default": "false", }, @@ -49,7 +57,7 @@ QtObject { function onZettelCreateClicked() { let uid = exactDate(); - let selected = script.noteTextEditSelectedText(); + let selected = script.noteTextEditSelectedText().trim(); // Link the from the old Zettel to the new one if anything was selected if(selected !== "") { @@ -111,12 +119,16 @@ QtObject { function zettelSelectorDialog(editableTextBox, zettelArray) { if (zettelArray == null) { zettelArray = []; - script.fetchNoteIdsByNoteTextPart(idMarker).forEach(function (noteId){ + fetchZettelIDs().forEach(function (noteId){ let note = script.fetchNoteById(noteId); zettelArray.push(note.name + " --id:"+note.id); }); } + if (zettelArray.length == 0) { + script.informationMessageBox("No valid Zettel found."); + } + 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)); @@ -146,8 +158,41 @@ QtObject { } } + // 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); + 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 uID (optionally) in all note texts. + // If no uID passed in, simply return all notes which conform to the Zettel-Title template. + function _getAllZettelFromTitle(optionaluID) { + let allnotes = script.fetchNoteIdsByNoteTextPart(""); + let noteIds = []; + + if (optionaluID != "") { + allnotes.forEach(function(noteId) { + if (script.fetchNoteById(noteId).name.startsWith(optionaluID)) noteIds.push(noteId); + }); + return noteIds; + } + + allnotes.forEach(function(noteId) { + if (script.fetchNoteById(noteId).name.search(/^\d{14}/) != -1) noteIds.push(noteId); + }); + + return noteIds; + } + function getNoteByZettelId(uid) { - let noteId = script.fetchNoteIdsByNoteTextPart(idMarker + uid); + let noteId = fetchZettelIDs(uid); if (noteId.length == 0) { return; } @@ -172,13 +217,17 @@ QtObject { if (headline == "") { headline = "Zettel" } - let text = headline.toString() + "\n"; + let text = ""; + + if (anchorInFilename) text += uid.toString() + " "; + + text += headline.toString() + "\n"; // Create Headline Separator text += "===============\n"; // Create uid tag - text += idMarker + uid.toString() + "\n\n"; + if (!anchorInFilename) text += idMarker + uid.toString() + "\n\n"; // Create content unit (i.e. space) text += "\n\n\n\n\n"; -- 2.45.2