CMorooney / obsidian-game-search-plugin

Obsidian plugin that automatically creates notes by searching for video games
MIT License
36 stars 10 forks source link

Templater expressions not evaluated when regenerating metadata #45

Closed breakid closed 3 weeks ago

breakid commented 4 weeks ago

Problem

  1. Templater code that appears in the metadata section of the template (e.g., updated: <% tp.file.last_modified_date("YYYY-MM-DD HH:mm:ss") %>) is not evaluated when regenerating metadata
  2. If the template starts with a Templater expression, rather than ---, regeneratedMetadata will never be initialized and all existing metadata will be deleted from the file
    • See line 170 of main.ts
    • Note: I have not explicitly tried this with the plugin; this behavior is deduced from the code flow and testing snippets of the code in a Typescript sandbox

Obsidian Version: 1.7.4 Obsidian Installer Version: 1.5.3 Game Search Version: 0.2.12

Cause:

The regenerateAllGameNotesMetadata() function does not invoke Templater like the createNewGameNote() function does

Solution:

  1. I'm not super familiar with Templater, but, if there is an in-memory evaluation function, you could pass your regeneratedContent through that. Otherwise, you might have to write it to a temp file, run useTemplaterPluginInFile() on it, read the contents back into memory, and delete the temp file
  2. Initializing regeneratedMetadata to existingMetadata should fix this issue
    • If the conditionals are true, regeneratedMetadata should be overwritten with the new content
    • If they are false, the existing metadata should be maintained

To Reproduce

  1. Create a template file with a Templater expression in one of the properties
    created: <% tp.file.creation_date("YYYY-MM-DD HH:mm:ss") %>
    updated: <% tp.file.last_modified_date("YYYY-MM-DD HH:mm:ss") %>
  2. Configure Game Search to use the template
  3. Regenerate the metadata
  4. The Templater expressions should appear verbatim in the regenerated file
  5. Add a Templater script to the very beginning of the file (before the ---)
    <%*
    const something = await tp.system.prompt("Some Input", tp.system.clipboard()) || "";
    -%>
    ---
  6. Regenerate the metadata
  7. Any existing metadata should be deleted

Expected behavior