Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix of automatic calendar note imports #267

Merged
merged 2 commits into from
Sep 2, 2024

Conversation

dabalroman
Copy link
Contributor

@dabalroman dabalroman commented Jul 29, 2024

The problem

The plugin is unable to create the notes following the template and folder set in settings under Default template and Default folder whenever the Auto create Event Notes Marker is set to any non-empty value and the description does not contain full, 3-part :directory-marker-template notation. Instead, depending on what's provided it creates a note in root directory or/and without using template.

Example

Calendar setup

I've prepared four events in my calendar:

Name Description
Nothing :obsidian:
Only directory :directory-obsidian:
Only template :obsidian-template:
Directory and location :directory-obsidian-template:

image

Obisidian setup

Empty vault with Templates, Templater and Google Calendar plugins installed.
image

Plugin setup

{
  "autoCreateEventKeepOpen": true,
  "importStartOffset": 1,
  "importEndOffset": 1,
  "optionalNotePrefix": "",
  "eventNoteNameFormat": "{{prefix}}{{event-title}}",
  "autoCreateEventNotes": true,
  "autoCreateEventNotesMarker": "obsidian",
  "useDefaultTemplate": true,
  "defaultTemplate": "templates/default.md",
  "defaultFolder": "events"
}

Expected result

  • "Nothing" is imported to the events directory using default.md template.
  • "Only directory" is imported to the directory directory using default.md template.
  • "Only template" is imported to the events directory using template.md template.
  • "Directory and location" is imported to the directory directory using template.md template.

Expectations were set by documentation of Auto create EventNotes

Actual result

  • 🔴 "Nothing" is imported to the root directory without using template.
  • 🔴 "Only directory" is imported to the directory directory without using template.
  • 🔴 "Only template" is imported to the root directory using template.md template.
  • ✅ "Directory and location" is imported to the directory directory using template.md template.

image

The cause

Whenever the regex :([^:]*-)?obsidian-?([^:]*)?: matches a string that does not contains anything that can be put into first or second capturing group the value passed to the createNoteFromEvent is set to undefined.

Value match[1] match[2]
:folder-marker-template: folder template
:marker-template: undefined template
:folder-template: folder undefined
:marker: undefined undefined

When undefined is passed to the createNoteFromEvent weird things happen - the note does not know what template to use or where it belongs. All other 6 calls to the createNoteFromEvent in project use syntax that uses values from config like createNoteFromEvent(event, this.settings.defaultFolder, this.settings.defaultTemplate) or very close to this notation.

The solution

Fallback values from settings should be used whenever the parsed value is undefined.

await createNoteFromEvent(events[i], match[1] ?? plugin.settings.defaultFolder, match[2] ?? plugin.settings.defaultTemplate, true)

The proof

Given exactly the same setup test yielded:

  • ✅ "Nothing" is imported to the events directory using default.md template.
  • ✅ "Only directory" is imported to the directory directory using default.md template.
  • ✅ "Only template" is imported to the events directory using template.md template.
  • ✅ "Directory and location" is imported to the directory directory using template.md template.

image

@YukiGasai
Copy link
Owner

Thank you for your work on this bug fix.
This looks good to me.

@YukiGasai YukiGasai merged commit 2ee88ec into YukiGasai:master Sep 2, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants