Skip to content

Commit

Permalink
Fixed bugs with the section finder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan J.A. Murphy committed Oct 22, 2021
1 parent fa50c21 commit 1418da5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
33 changes: 19 additions & 14 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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);
}


Expand Down Expand Up @@ -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?`);
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 1418da5

Please sign in to comment.