obsidian-shellcommands icon indicating copy to clipboard operation
obsidian-shellcommands copied to clipboard

A new variable {{note_content}}

Open Taitava opened this issue 4 years ago • 13 comments

Discussed in https://github.com/Taitava/obsidian-shellcommands/discussions/57

Get's the current note's content without YAML frontmatter.

Taitava avatar Oct 18 '21 10:10 Taitava

I tried to implement this now, but faced difficulties:

  1. Getting file content in Obsidian's API seems to require asynchronous method processing. This is something that is new to me in JavaScript, and the variable system in SC is not built to work asynchronously. I quess I can do the kind of refactoring needed for this, but cannot promise anything yet.
  2. I was not able to find a way to reliably filter out YAML fronmatter from the file content. For this, I have asked a question in Obsidian's forum.

So, unfortunately I need to postpone this until those two problems get solved. If I get number 1 solved myself in the future, I think I could then create another variable file_content (needs a new feature issue) that would contain the whole content of the current file. It would not be a replacement for this variable, it would be a different thing, but at least something.

Taitava avatar Oct 26 '21 17:10 Taitava

I tried to implement this now, but faced difficulties:

Sorry if this FR is bring some hurdles @Taitava !

So, unfortunately I need to postpone this until those two problems get solved. If I get number 1 solved myself in the future, I think I could then create another variable file_content (needs a new feature issue) that would contain the whole content of the current file. It would not be a replacement for this variable, it would be a different thing, but at least something.

Okay, I think you are right!

Let's wait a feedback from our community friends. Maybe some other developer can help us with this issue :)

Have a great day!

FelipeRearden avatar Oct 26 '21 18:10 FelipeRearden

Sorry if this FR is bring some hurdles @Taitava !

Problems are nothing new under the sun of code 🙂 . I've been developing this plugin since mid August, so only a couple of months, and before that I had no experience of Obsidian's API and plugin development. Things tend to be harder in the beginning 🙂 .

Let's wait a feedback from our community friends. Maybe some other developer can help us with this issue :)

I hope so too.

Have a nice day, too! 🙂

Taitava avatar Oct 27 '21 12:10 Taitava

Problems are nothing new under the sun of code 🙂 . I've been developing this plugin since mid August, so only a couple of months, and before that I had no experience of Obsidian's API and plugin development. Things tend to be harder in the beginning 🙂 .

It's amazing to see how you accomplished so much in just only 2/3 months :)

I can imagine @Taitava . Code development is something so special and complex when compared to other subjects :)

You are doing an spectacular job :)

Have a great day :)

FelipeRearden avatar Oct 28 '21 09:10 FelipeRearden

@Taitava

I found this link that has a code about reading the file current content:

https://liamca.in/Obsidian/API+FAQ/filesystem/read+the+current+file

Reading the current file contents is a two-step process:

Retrieve the current view from the Workspace. Read the view's file from the Vault.

Sample Code

import type { MarkdownView } from 'obsidian';

/* ... */

const { workspace } = this.app;
const activeView = workspace.getActiveViewOfType(MarkdownView);

if (activeView) {  // The active view might not be a markdown view
  // Read the file (from either the cache or from disk)
  // Note: `cachedRead` returns stale data which should be good for most cases. 
  // However, if you plan on writing back to disk, use `vault.read` here 
  // instead to avoid data loss.
  const fileContents = vault.cachedRead(activeView.file);
}

Maybe it helps :)

FelipeRearden avatar Oct 28 '21 10:10 FelipeRearden

Thanks @FelipeRearden ! The Liam Cain's solution seems to be quite similar to another solution that I found over here. Both of these use vault.read() / vault.cachedRead() methods, which return Promise objects (sorry I'm not explaining this with non-programmin terms), and Promise objects are something that will require me to do some bigger changes in the variable system due to their asynchronous nature.

Taitava avatar Oct 28 '21 11:10 Taitava

A short explanation for Promise objects: In theory vault.read() / vault.cachedRead() could return the file's content to my program directly. That would be easy. But they do not do that. Instead they return me something that tells: "Hey, I'll promise to give you the file's content in the future." So I need to wait - probably less than a millisecond, but still. And I need to make the whole SC wait: the variable waits to get the file content that it can then give to earlier code that replaces variables with their values, and when that code finally gets the variable value, it can continue processing other variables and finally let even earlier code to continue with actually executing the shell command. This whole chain needs to be organised a bit differently to take into account that we need to wait a bit before we can execute the shell command. 👍

(I guess this explanation didn't exlain anything 😆 )

Taitava avatar Oct 28 '21 11:10 Taitava

Looks like this is the kind of thing that we have to wait for Obsidian API gives us a "map" to simple extract the content without the YAML :(

I face the same situation trying to ask for a command palette command to the right mouse click:

  • Obsidian API doesn't not have yet a command for this :)

Thanks for the explanation @Taitava !

We have to wait for some external help :)

FelipeRearden avatar Oct 28 '21 11:10 FelipeRearden

My question in the forum has been answered: https://forum.obsidian.md/t/how-to-get-current-file-content-without-yaml-frontmatter/26197/2

I haven't tried the solution yet.

Taitava avatar Nov 11 '21 04:11 Taitava

My question in the forum has been answered: https://forum.obsidian.md/t/how-to-get-current-file-content-without-yaml-frontmatter/26197/2

I haven't tried the solution yet.

Great to know @Taitava ! Let's hope that we have a good solution 🙏

FelipeRearden avatar Nov 11 '21 11:11 FelipeRearden

Thanks for this great plugin and it is very useful, just a very hacky thought , instead of using obsidian api, since we know the current file path already, can we just cat the file in bash to get the content ?

marvinwu avatar Sep 01 '22 04:09 marvinwu

Thank you @marvinwu for your positive feedback! ❤️ cat works very well as a workaround solution, if users use it in their own shell commands. It just doesn't make it easy to get the content without YAML frontmatter, in case a user doesn't want the frontmatter.

What comes to this actual {{note_content}} variable, in theory it could be implemented so that it would execute a cat command under the hood (I don't know if you meant to suggest that or not), but there's two reasons why I wouldn't do it:

  • I have a design principle in my head that the plugin should only execute shell commands a user has defined themselves. If there would be any "under the hood" shell commands, the plugin should at least show the shell command to the user and ask a permission to execute it.
  • It's not actually hard to use the Obsidian API to retrieve the file content. It just takes some time to refactor the current variable system to support asynchronous processing (using Promises). Now that this issue is almost a year old, I have become better at using Promises during these months, so the problem should not be so big anymore.

But anyway, cat is a good workaround for users until this variable finally gets implemented.

Taitava avatar Sep 01 '22 06:09 Taitava

Success

I was able to make this variable work! I did the needed variable system refactoring (issue #265). I still need to do extensive testing, because the changes were quite big, so need to ensure I didn't break anything in the variable parsing process.

I'll update this issue when the variable is ready to be released.

TODO:

  • [x] Create also {{event_note_content}} variable.

Taitava avatar Sep 01 '22 16:09 Taitava

Released now.

Taitava avatar Sep 25 '22 13:09 Taitava