From 1418da59dcc259b5823140b5a3452bd6ba997b71 Mon Sep 17 00:00:00 2001 From: "Ryan J.A. Murphy" Date: Fri, 22 Oct 2021 10:03:20 -0600 Subject: [PATCH] Fixed bugs with the section finder --- main.ts | 33 +++++++++++++++++++-------------- manifest.json | 2 +- package.json | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/main.ts b/main.ts index ca2abf0..b8482a1 100644 --- a/main.ts +++ b/main.ts @@ -143,28 +143,33 @@ ${this.settings.logPrefix}${tampTime}` if (typeof(nextSection) !== undefined) { // The search for a following section did not return undefined, therefore it exists // Find out if there is a preceding blank line before the next section. E.g., does the user use linebreaks to separate content in edit mode? If so, inserting the next item after that line break will look messy. - if (editor.getLine(nextSection.position.start.line - 1).length > 0) { + if (editor.getLine(nextSection.position.start.line - 1) === editor.getLine(targetSection.position.start.line)) { + // There are no lines between the next section and the target section. Insert the log item immediately after the target section. + editor.setCursor(nextSection.position.start.line - 1); + editor.replaceSelection(linePrefix); + editor.setCursor(nextSection.position.start.line); + } else if (editor.getLine(nextSection.position.start.line - 1).length > 0) { // The line just before the next section header is not blank. Insert the log item just before the next section, without a line break. - console.debug(`The line before the next section header is ${editor.getLine(nextSection.position.start.line - 1).toString()}`); console.debug("No blank lines found between the target section and the next section."); - editor.setCursor(nextSection.position.start.line - 1); - editor.replaceSelection(linePrefix); - editor.setCursor(nextSection.position.start.line); + editor.setCursor(nextSection.position.start.line - 2); + editor.replaceSelection(linePrefix); + editor.setCursor(nextSection.position.start.line); } else { - console.debug(`The line before the next section has 0 length. It is: ${editor.getLine(nextSection.position.start.line - 1)}`) + console.debug(`The line before the next section has 0 length. It is line number: ${nextSection.position.start.line - 1}`) // The line just before the next section header is blank. It's likely that the user uses line breaks to clean up their note in edit mode. // The approach here is to iterate over the lines preceding the next section header until a non-blank line is reached, then insert the log item at (iterator.position.start.line + 1)... let lastBlankLineFound = false; let noBlankLines = false; - let lastLineBeforeLineBreakIteratorLineNumber = nextSection.position.start.line - 2; // `lastLineBeforeLineBreakIteratorNumber: this wordy variable represents the number of the last-line-before-line-break iterator's current line + let lastLineBeforeLineBreakIteratorLineNumber = nextSection.position.start.line - 1; // `lastLineBeforeLineBreakIteratorNumber: this wordy variable represents the number of the last-line-before-line-break iterator's current line while (lastBlankLineFound == false) { - let blankLineFinderCurrentLine = editor.getLine(lastLineBeforeLineBreakIteratorLineNumber); - if (editor.getLine(0) === blankLineFinderCurrentLine) { // This condition would mean the iterator found the start of the document + if (lastLineBeforeLineBreakIteratorLineNumber == 0) { // This condition would mean the iterator found the start of the document noBlankLines = true; - lastBlankLineFound = false; + lastBlankLineFound = true; } else { - if (blankLineFinderCurrentLine.length > 0) { + let blankLineFinderCurrentLine = editor.getLine(lastLineBeforeLineBreakIteratorLineNumber); + if (blankLineFinderCurrentLine.toString() === "") { lastBlankLineFound = true; + console.debug("found the last line"); } else { lastLineBeforeLineBreakIteratorLineNumber = lastLineBeforeLineBreakIteratorLineNumber - 1; // Move to the next line up } @@ -178,9 +183,9 @@ ${this.settings.logPrefix}${tampTime}` editor.setCursor(nextSection.position.start.line - 1); } else { // There were an arbitrary number of blank lines before the next section header. Insert the log item _after_ the last (length > 0) line before the next section header. console.debug(`Iterator stopped at line ${lastLineBeforeLineBreakIteratorLineNumber}, with text ${editor.getLine(lastLineBeforeLineBreakIteratorLineNumber)}`); - editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber); + editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber - 1); editor.replaceSelection(linePrefix); - editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber + 1); + editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber); } @@ -225,7 +230,7 @@ ${this.settings.logPrefix}${tampTime}` let zkUUIDNoteName = moment().format(this.settings.newDraftFilenameTemplate); - console.log(`${this.settings.inboxFilePath}`); + console.debug(`${this.settings.inboxFilePath}`); if (!(this.app.vault.getAbstractFileByPath(`${this.settings.inboxFilePath}`))) { // In the future, handle folder creation as necessary. For now, error and tell the user if the inbox folder does not exist. new Notice(`Error. Lumberjack couldn't create the draft. Does the inbox folder you've set in Preferences -> Lumberjack 🪓🪵 exist?`); diff --git a/manifest.json b/manifest.json index 20653f6..230102c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "lumberjack-obsidian", "name": "Lumberjack 🪓 🪵", - "version": "1.2.0", + "version": "1.2.1", "minAppVersion": "0.12.0", "description": "Log your thoughts! Lumberjack adds URL commands to help you axe inefficiency and get right to writing.", "author": "ryanjamurphy", diff --git a/package.json b/package.json index d3968cc..80bda63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Lumberjack 🪓🪵", - "version": "1.2.0", + "version": "1.2.1", "description": "Log your thoughts! Lumberjack adds URL commands to help you axe inefficiency and get right to writing.", "main": "main.js", "scripts": {