ivan-lednev / obsidian-task-archiver

Archive completed tasks in your Obsidian vault (plus other org-mode-like features)
MIT License
109 stars 12 forks source link

Plugin crash when headers jump more than one level #7

Closed adamhl8 closed 2 years ago

adamhl8 commented 2 years ago

If you have a note setup like the following, the plugin crashes:

# h1

### h3

- [x] task

# h1

As far as I can tell, it's because it's jumping from an h1 to an h3. If I make that second header an h2, the plugin runs fine. I think there's an issue with how the plugin finds parent headers/sections, or it's an issue with how the plugin calculates the internal level value.

https://github.com/ivan-lednev/obsidian-task-archiver/blob/f06d7bc5a5d5e201f3fd383c8eb89b3b4f26aa3b/src/parser/SectionParser.ts#L59

In buildTree, as it steps through flatSections, context.level is 3 and section.level is 1. Then, stepsUpToParent is 3. When this is passed to getNthAncestor, that function returns a parent that doesn't exist.

VM628:260 Uncaught (in promise) TypeError: Cannot read property 'append' of undefined
    at SectionParser.buildTree (eval at <anonymous> (app.js:1), <anonymous>:260:21)
    at SectionParser.parse (eval at <anonymous> (app.js:1), <anonymous>:226:14)
    at Archiver.archiveTasksToSameFile (eval at <anonymous> (app.js:1), <anonymous>:286:43)
    at ObsidianTaskArchiver.eval (eval at <anonymous> (app.js:1), <anonymous>:469:48)
    at Generator.next (<anonymous>)
    at fulfilled (eval at <anonymous> (app.js:1), <anonymous>:28:58)
ivan-lednev commented 2 years ago

Hello, @adamhl8,

thank you for your input! I addressed the issue in 0.4.0.