blacksmithgu / obsidian-dataview

A data index and query language over Markdown files, for https://obsidian.md/.
https://blacksmithgu.github.io/obsidian-dataview/
MIT License
6.94k stars 411 forks source link

Add option to evaluate dataview codeblocks #941

Open j-mcavoy opened 2 years ago

j-mcavoy commented 2 years ago

Is your feature request related to a problem? Please describe. I use dataviews a lot in my daily entry template for tasks such as listing files I created/modified that day, what tasks I completed or should work on that day, and add #tags based on some conditional logic.

However Obsidian doesn't see the generated markdown created by the dataview code blocks, it just sees the code, so I can't actually search for any text or tags generated by dataview or see back-links to this entry from the files which are listed / linked.

Describe the solution you'd like I would like a way for a user to optionally have their dataview code blocks evaluated, i.e. evaluate the markdown that the dataview code block generates on the fly and write it to the actual file.

For instance, the content created by some dataviews may change throughout the day, but then it will be static forever after so it would be nice to have an easy way to save this generated output to the actual markdown file to take advantage of all the Obsidian linking-searching stuff.

  1. The user writes adds dataview code to their markdown file... # Example ```dataview list where file.ctime = date(today) ```
  2. They can see the on-the-fly evaluated output in read mode (just like dataview works currently):
    # Example
    - [[File 1]]
    - [[File 2]]
  3. They hover over the dataview output, next to the </> Edit this Block icon, there edit_this_block is a ▶️ icon to evaluate the expression and write it to the file.

They click it. And the file contents changes to:

# Example
 - [[File 1]]
 - [[File 2]]

Describe alternatives you've considered Another idea I had for evaluating the dataview codeblock non-destructively would be to not delete the dataview code, but just comment it out with %:

# Example
% \`\`\`dataview
% list where file.ctime = date(today)
% \`\`\`
 - [[File 1]]
 - [[File 2]]

Additional context

AB1908 commented 2 years ago

Haha this is a common ask. I can propose two approaches, one is to use a templater script to write to the file and use DV's API to generate what you need. However, if you're looking to only generate the link structure and the text itself isn't that important to you, wrap DV in an admonition. This is a plugin by javalent.

blacksmithgu commented 2 years ago

See #42 for the bigger discussion on freezing views.

nomppy commented 2 years ago

Haha this is a common ask. I can propose two approaches, one is to use a templater script to write to the file and use DV's API to generate what you need. However, if you're looking to only generate the link structure and the text itself isn't that important to you, wrap DV in an admonition. This is a plugin by javalent.

I've been reading up on view freezing (#42). I understand why writing to file with templater works, but what would wrapping a DV query in admonition do? Also: dynamic templater commands run on file open, so the way to freeze queries -- such as for a daily log of modified files -- would be to just insert it once, right?

AB1908 commented 2 years ago

For templater, only insertion would work, dynamic commands do not since they do not create content, similar to dataview. I believe the admonition idea no longer works. I never tested it myself and merely saw on Discord that it worked.

nomppy commented 2 years ago

My mistake on the templater - I confused dynamic and execution commands.

AB1908 commented 2 years ago

I'm closing this in favor of #42 since there are many variations on the idea. If there's something I overlooked, let me know and we can split that into a separate issue.