Skip to content

Commit

Permalink
Added section targeting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan J.A. Murphy committed Oct 14, 2021
1 parent a60fec7 commit 56c0f09
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
92 changes: 72 additions & 20 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DEFAULT_SETTINGS: LumberjackSettings = {
alwaysOpenInNewLeaf: false,
inboxFilePath: "/",
newDraftFilenameTemplate: "YYYYMMDDHHmm",
targetHeader: "## Journal"
targetHeader: "Journal"
}

const editModeState = {
Expand All @@ -35,7 +35,7 @@ export default class LumberjackPlugin extends Plugin {
settings: LumberjackSettings;

async onload() {
console.log('Loading the Lumberjack plugin. 𖥧');
console.debug('Loading the Lumberjack plugin. 𖥧');

await this.loadSettings();

Expand Down Expand Up @@ -112,30 +112,82 @@ export default class LumberjackPlugin extends Plugin {
let linePrefix = `
${this.settings.logPrefix}${tampTime}`

// Make sure the editor has focus
editor.focus();

// Assume the cursor will be placed at the end of the note
let sectionFound = false;

// Inserting the cursor
// The goal is to set the cursor either at the end of the user's target section, if set, or at the end of the note

// 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 dailyNoteText = this.app.vault.read(dailyNote);
if ((await dailyNoteText).contains(this.settings.targetHeader)) {
let sections = this.app.metadataCache.getFileCache(dailyNote).headings;
let dailyNoteText = await this.app.vault.read(dailyNote);
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;
} 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);
}


}
}
}
}
}

// Make sure the editor has focus and set the cursor either in the found section or at the end of the note
editor.focus();
if (obsidianMobileFlag) {
if (!sectionFound)
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
} else {
const initialLines = editor.lineCount();
editor.setCursor({ line: initialLines, ch: 0 });
editor.replaceSelection(linePrefix);
const finalLines = editor.lineCount();
editor.setCursor({ ch: 0, line: finalLines });
}

}

Expand Down Expand Up @@ -183,7 +235,7 @@ ${this.settings.logPrefix}${tampTime}${someData}`
}

onunload() {
console.log('Unloading the Lumberjack plugin. Watch out for splinters.');
console.debug('Unloading the Lumberjack plugin. Watch out for splinters.');
}

async loadSettings() {
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.0.2",
"version": "1.1.0",
"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.0.2",
"version": "1.1.0",
"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 56c0f09

Please sign in to comment.