Skip to content

Commit

Permalink
Fixed error when daily note was missing sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan J.A. Murphy committed Oct 21, 2021
1 parent 01fb4df commit 9af1009
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 61 deletions.
126 changes: 67 additions & 59 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,74 +118,82 @@ ${this.settings.logPrefix}${tampTime}`

// find the section to insert the log item into and set the insert position to append into it, or set the insert position to the end of the note
let sections = this.app.metadataCache.getFileCache(dailyNote).headings;
if (this.settings.targetHeader != "") { // If the user has set a target header
// need to figure out which line the _next_ section is on, if any, then use that line number in the functions below
let targetSection = sections.find( (eachSection) => (eachSection.heading === this.settings.targetHeader)); // does the heading we're looking for exist?
if (typeof(targetSection !== undefined)) { // The target section exists
let nextSection = sections.find( (eachSection) => ((eachSection.position.start.line > targetSection.position.start.line) && (eachSection.level <= targetSection.level))); // matches sections _after_ our target, with the same level or greater
console.debug(nextSection);
if (!nextSection) {
// There is no section following the target section. Look for the end of the document
// A better approach would append the item at the end of the content inside the user's target section, because it's possible that someone would put more stuff in their daily note without a following section, but that will have to be implemented later.
console.debug("No section follows the target section. Inserting the log item at the end of the target section.")
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
} else {
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) {
// 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);
} else {
console.debug(`The line before the next section has 0 length. It is: ${editor.getLine(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
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
noBlankLines = true;
lastBlankLineFound = false;
} else {
if (blankLineFinderCurrentLine.length > 0) {
lastBlankLineFound = true;
if (!sections) {
// The daily note does not have sections. Insert the log item at the bottom of the note.
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
} else {
if (this.settings.targetHeader != "") { // If the user has set a target header
// need to figure out which line the _next_ section is on, if any, then use that line number in the functions below
let targetSection = sections.find( (eachSection) => (eachSection.heading === this.settings.targetHeader)); // does the heading we're looking for exist?
if (typeof(targetSection !== undefined)) { // The target section exists
let nextSection = sections.find( (eachSection) => ((eachSection.position.start.line > targetSection.position.start.line) && (eachSection.level <= targetSection.level))); // matches sections _after_ our target, with the same level or greater
console.debug(nextSection);
if (!nextSection) {
// There is no section following the target section. Look for the end of the document
// A better approach would append the item at the end of the content inside the user's target section, because it's possible that someone would put more stuff in their daily note without a following section, but that will have to be implemented later.
console.debug("No section follows the target section. Inserting the log item at the end of the target section.")
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
} else {
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) {
// 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);
} else {
console.debug(`The line before the next section has 0 length. It is: ${editor.getLine(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
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
noBlankLines = true;
lastBlankLineFound = false;
} else {
lastLineBeforeLineBreakIteratorLineNumber = lastLineBeforeLineBreakIteratorLineNumber - 1; // Move to the next line up
if (blankLineFinderCurrentLine.length > 0) {
lastBlankLineFound = true;
} else {
lastLineBeforeLineBreakIteratorLineNumber = lastLineBeforeLineBreakIteratorLineNumber - 1; // Move to the next line up
}
}
}

if (noBlankLines) { // this means the iterator failed to find any blanks at all; insert the log item just before the next section.
console.debug("No blank lines found.");
editor.setCursor(nextSection.position.start.line - 1);
editor.replaceSelection(linePrefix);
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.replaceSelection(linePrefix);
editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber + 1);
}


}

if (noBlankLines) { // this means the iterator failed to find any blanks at all; insert the log item just before the next section.
console.debug("No blank lines found.");
editor.setCursor(nextSection.position.start.line - 1);
editor.replaceSelection(linePrefix);
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.replaceSelection(linePrefix);
editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber + 1);
}


}
}
}
} else {
// The user has not set a target header. Insert the log item at the bottom of the note.
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
}
} else {
// The user has not set a target header. Insert the log item at the bottom of the note.
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
}

}


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.1.6",
"version": "1.1.7",
"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.1.6",
"version": "1.1.7",
"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 9af1009

Please sign in to comment.