Skip to content

Commit

Permalink
Merge pull request #7 from liamcain/fix/missing-heading-causes-data-loss
Browse files Browse the repository at this point in the history
Fix: missing heading causes data loss
  • Loading branch information
liamcain authored Mar 18, 2021
2 parents 44a9e9e + 51eb7de commit 4adaa06
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "things-logbook",
"name": "Things Logbook",
"description": "Sync your Things.app Logbook with Daily Notes",
"version": "0.1.7",
"version": "0.1.8",
"author": "Liam Cain",
"authorUrl": "https://github.com/liamcain/",
"isDesktopOnly": true,
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": "obsidian-things-logbook-plugin",
"version": "0.1.7",
"version": "0.1.8",
"description": "Sync Things.app Logbook with Obsidian",
"author": "liamcain",
"main": "main.js",
Expand Down
24 changes: 16 additions & 8 deletions src/textUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,22 @@ export async function updateSection(

const editor = getEditorForFile(app, file);
if (editor) {
const from = { line: logbookSectionLineNum, ch: 0 };
const to =
nextSectionLineNum !== -1
? { line: nextSectionLineNum - 1, ch: 0 }
: { line: fileLines.length, ch: 0 };

editor.replaceRange(`${sectionContents}\n`, from, to);
return;
// if the "## Logbook" header exists, we just replace the
// section. If it doesn't, we need to append it to the end
// if the file and add `\n` for separation.
if (logbookSectionLineNum !== -1) {
const from = { line: logbookSectionLineNum, ch: 0 };
const to =
nextSectionLineNum !== -1
? { line: nextSectionLineNum - 1, ch: 0 }
: { line: fileLines.length, ch: 0 };
editor.replaceRange(`${sectionContents}\n`, from, to);
return;
} else {
const pos = { line: fileLines.length, ch: 0 };
editor.replaceRange(`\n\n${sectionContents}`, pos, pos);
return;
}
}

// Editor is not open, modify the file on disk...
Expand Down

0 comments on commit 4adaa06

Please sign in to comment.