chhoumann / quickadd

QuickAdd for Obsidian
https://quickadd.obsidian.guide
MIT License
1.5k stars 136 forks source link

[BUG] Templater code executing twice when using tp.system.prompt to capture user input. #533

Open romeoleung opened 1 year ago

romeoleung commented 1 year ago

Describe the bug When enabled Capture format, and inputting templater tp.system.prompt to capture user input, the code will execute twice, and only the 2nd time gets registered.

To Reproduce Steps to reproduce the behavior:

  1. Go to QuickAdd Settings, add a new Capture.
  2. Enable "Capture to active file", enable "Capture format".
  3. In Capture format edit box, input templater code with tp.system.prompt function to get user input. Example:
    <%*
    let date = await tp.system.prompt('Enter Date:', default_value=tp.date.now('YYYY-MM-DD'), throw_on_cancel=true)
    let time = await tp.system.prompt('Enter Time:', default_value=tp.date.now('HH:mm'), throw_on_cancel=true)
    %> (@<% date %> <% time %>)
  4. Go back to any notes, and run this QickAdd Capture.
  5. Observe prompts open for a first time - input any valid data, e.g. "2023-09-01" and "17:00", hit Enter to continue.
  6. Observe prompts now open for a 2nd time - input other valid data, e.g. "2024-01-01" and "06:00", hit Enter to continue.
  7. Observe the note, only the 2nd entry is added to the note

Expected behavior The prompts should not display twice. They should display once and the input should be captured and put into the note.

Screenshots If applicable, add screenshots to help explain your problem. Capture 1 - Settings Capture 2 - first date prompt Capture 3 - first time prompt Capture 4 - 2nd date prompt Capture 5 - 2nd time prompt Capture 6 - issue

Desktop (please complete the following information):

FeralFlora commented 9 months ago

A similar thing happens when I capture to a non-existent daily note that contains various Templater templates.

The Quickadd capture triggers the creation of the daily note, thus triggering Templater (run Templater on new file creation enabled). Like you report, when I fill in the system prompts from Templater, they open once more.

This doesn't happen when I create the daily note outside of Quickadd, only when Quickadd creates the daily note as part of the capture.

jesperkdab commented 9 months ago

I'd like to just chime in here that I see this too.

GV-Wong commented 8 months ago

Experiencing this as well.

FeralFlora commented 7 months ago

Solving this would provide a lot of value to users, because the current situation a big impediment to a good workflow. Whenever you capture to a non-existent daily note that contains Templater prompts, thereby triggering its creation, you have to fill in the prompts twice to get valid results in the resulting note. Of course, this is very inconvenient.

GV-Wong commented 5 months ago

FYI for anyone who uses quickadd features sparingly and is looking for a quick minimal solution to this- reverting back to Version 0.17.1 fixes the duplicate prompt templater issue.

FeralFlora commented 5 months ago

FYI for anyone who uses quickadd features sparingly and is looking for a quick minimal solution to this- reverting back to Version 0.17.1 fixes the duplicate prompt templater issue.

Very interesting! Maybe that holds some clues as to the cause of this issue. I'm wondering, did you revert version by version to figure this out?

GV-Wong commented 5 months ago

FYI for anyone who uses quickadd features sparingly and is looking for a quick minimal solution to this- reverting back to Version 0.17.1 fixes the duplicate prompt templater issue.

Very interesting! Maybe that holds some clues as to the cause of this issue. I'm wondering, did you revert version by version to figure this out?

Read version releases and looked at the code with my limited typescript knowledge.

I didn't look too far into it but I believe it has to do with the templaterParseTemplate function.

My hypothesis is that when templater's own parse_template function is called within that function, it is generating its own instance of the templater object in order to interpret it for rendering. That's why the first entry goes nowhere but the second entry is appended to the current line. This bug started happening around the time of 0.18.3 which involved a fix for rendering templater syntax so it lines up with the timeline.