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.
This commit is contained in:
Marty 2018-11-28 19:16:19 +00:00
parent 7611441252
commit 346188e835

View file

@ -4,7 +4,7 @@ import com.qownnotes.noteapi 1.0
QtObject { QtObject {
property bool anchorInFilename; property bool anchorInFilename;
property string idMarker; property string anchorMarker;
property bool wikiLinks; property bool wikiLinks;
property string template; property string template;
@ -12,14 +12,14 @@ QtObject {
{ {
"identifier": "anchorInFilename", "identifier": "anchorInFilename",
"name": "Display Zettel anchors in note filenames", "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", "type": "boolean",
"default": "false", "default": "false",
}, },
{ {
"identifier": "idMarker", "identifier": "anchorMarker",
"name": "Zettelkasten anchor marker", "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", "type": "string",
"default": "§§", "default": "§§",
}, },
@ -64,41 +64,41 @@ QtObject {
// ---------------------------- // ----------------------------
function onZettelCreateClicked() { function onZettelCreateClicked() {
let uid = exactDate(); let anchor = exactDate();
let selected = script.noteTextEditSelectedText().trim(); let selected = script.noteTextEditSelectedText().trim();
// 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 !== "") {
createLinkInPlace(uid, selected); createLinkInPlace(anchor, selected);
} }
// Create a Zettel // Create a Zettel
createZettel(uid, selected); createZettel(anchor, selected);
} }
function onInsertLinkClicked() { function onInsertLinkClicked() {
let selected = script.noteTextEditSelectedText(); let selected = script.noteTextEditSelectedText();
let clipboard = script.clipboard(); let clipboard = script.clipboard();
let uid = null let anchor = null
if (verifyZettelId(clipboard)) { if (verifyAnchor(clipboard)) {
uid = verifyZettelId(clipboard); anchor = verifyAnchor(clipboard);
} else { } else {
let otherNote = zettelSelectorDialog(true); let otherNote = zettelSelectorDialog(true);
uid = extractZettelIdFromString(otherNote.noteText); anchor = extractAnchorFromString(otherNote.noteText);
} }
if (uid == null) { if (anchor == null) {
return; return;
} }
createLinkInPlace(uid, selected) createLinkInPlace(anchor, selected)
} }
function onJumpToZettelClicked() { 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) { 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 { } else {
script.setCurrentNote(zettelSelectorDialog(true)); script.setCurrentNote(zettelSelectorDialog(true));
} }
@ -109,13 +109,13 @@ QtObject {
// ---------------------------- // ----------------------------
function autocompletionHook() { function autocompletionHook() {
let uid = verifyZettelId(script.noteTextEditCurrentWord(true), true); let anchor = verifyAnchor(script.noteTextEditCurrentWord(true), true);
if(uid == false) { if(anchor == false) {
return []; return [];
} }
setNoteToZettelIdOrElse(uid); setNoteToAnchorOrElse(anchor);
return []; return [];
} }
@ -142,8 +142,8 @@ QtObject {
return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5)); return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5));
} }
function extractZettelIdFromString(text) { function extractAnchorFromString(text) {
let regex = new RegExp(idMarker + '\\d{14}\\s') let regex = new RegExp(anchorMarker + '\\d{14}\\s')
let markerpos = text.search(regex) + 2; let markerpos = text.search(regex) + 2;
if (markerpos == -1) { if (markerpos == -1) {
return; return;
@ -151,42 +151,43 @@ QtObject {
return text.substring(markerpos, markerpos+14); return text.substring(markerpos, markerpos+14);
} }
function stripZettelId(link) { function stripAnchor(link) {
return link.replace(/\D*/g, ""); return link.replace(/\D*/g, "");
} }
function verifyZettelId(id, shouldStrip) { function verifyAnchor(anchor, shouldStrip) {
if (shouldStrip) id = stripZettelId(id); if (shouldStrip) anchor = stripAnchor(anchor);
if (id.match(/^\d{14}$/) != null) {
return id; if (anchor.match(/^\d{14}$/) != null) {
} else if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) { return anchor;
return id.substring(2); } else if (anchor.match(new RegExp(anchorMarker + '^\\d{14}$')) != null) {
return anchor.substring(2);
} else { } else {
return false; return false;
} }
} }
// Returns List of Zettel ids with the specified uID. // Returns List of Zettel ids with the specified anchor.
// If no uID provided returns all Zettel found in the current directory. // If no anchor provided returns all Zettel found in the current directory.
function fetchZettelIDs(uid) { function fetchZettelIDs(anchor) {
let uid = uid ? uid : ""; let anchor = anchor ? anchor : "";
let noteIds = anchorInFilename ? _getAllZettelFromTitle(uid) : _getAllZettelFromAnchor(uid); let noteIds = anchorInFilename ? _getAllZettelFromTitle(anchor) : _getAllZettelFromAnchor(anchor);
return noteIds; return noteIds;
} }
// Looks for the combination of idMarker and uID (optionally) in all note texts. // Looks for the combination of anchorMarker and anchor (optionally) in all note texts.
function _getAllZettelFromAnchor(optionaluId) { function _getAllZettelFromAnchor(anchor) {
return script.fetchNoteIdsByNoteTextPart(idMarker + optionaluId); return script.fetchNoteIdsByNoteTextPart(anchorMarker + anchor);
} }
// Looks for the uID (optionally) in all note texts. // Looks for the anchor (optionally) in all note texts.
function _getAllZettelFromTitle(optionaluID) { function _getAllZettelFromTitle(anchor) {
let allnotes = script.fetchNoteIdsByNoteTextPart(""); let allnotes = script.fetchNoteIdsByNoteTextPart("");
let noteIds = []; let noteIds = [];
if (optionaluID != "") { if (anchor != "") {
allnotes.forEach(function(noteId) { 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; return noteIds;
} }
@ -198,28 +199,28 @@ QtObject {
return noteIds; return noteIds;
} }
function getNoteByZettelId(uid) { function getNoteByAnchor(anchor) {
let noteId = fetchZettelIDs(uid); let noteId = fetchZettelIDs(anchor);
if (noteId.length == 0) { if (noteId.length == 0) {
return; return;
} }
return script.fetchNoteById(noteId[0]); return script.fetchNoteById(noteId[0]);
} }
function setNoteToZettelIdOrElse(uid, orElse) { function setNoteToAnchorOrElse(anchor, orElse) {
let zettel = getNoteByZettelId(uid); let zettel = getNoteByAnchor(anchor);
if (zettel == null) { if (zettel == null) {
if(orElse != null) orElse(uid); if(orElse != null) orElse(anchor);
return; return;
} }
script.setCurrentNote(zettel); script.setCurrentNote(zettel);
} }
function createLinkInPlace(uid, selected) { function createLinkInPlace(anchor, selected) {
wikiLinks ? script.noteTextEditWrite("[[" + uid + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + uid + ")"); wikiLinks ? script.noteTextEditWrite("[[" + anchor + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + anchor + ")");
} }
function createZettel(uid, headline) { function createZettel(anchor, headline) {
// Create Headline - empty if nothing selected // Create Headline - empty if nothing selected
if (headline == "") { if (headline == "") {
headline = "Zettel" headline = "Zettel"
@ -227,9 +228,9 @@ QtObject {
let text = template; let text = template;
if (anchorInFilename) { if (anchorInFilename) {
text = text.replace(/\{headline\}/, uid.toString() + " " + headline).replace(/\{anchor\}/, ""); text = text.replace(/\{headline\}/, anchor.toString() + " " + headline).replace(/\{anchor\}/, "");
} else { } else {
text = text.replace(/\{headline\}/, headline).replace(/\{anchor\}/, idMarker + uid.toString()); text = text.replace(/\{headline\}/, headline).replace(/\{anchor\}/, anchorMarker + anchor.toString());
} }
script.createNote(text); script.createNote(text);