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

Images inside an admonition aren't detected #45

Closed
davidkopp opened this issue Mar 17, 2024 · 12 comments · Fixed by #57
Closed

Images inside an admonition aren't detected #45

davidkopp opened this issue Mar 17, 2024 · 12 comments · Fixed by #57
Labels
enhancement New feature or request
Milestone

Comments

@davidkopp
Copy link

Example:

```ad-info
![](_attachments/image.png)
```

The plugin wants to delete the image, because it somehow doesn't see it inside this admonition. In a Obsidian callout it works like expected. However, I need to use the old syntax of the admonition plugin and I can't switch fully to callouts.

@husjon
Copy link
Owner

husjon commented Mar 17, 2024

Hi, the reason for the cleaner seeing the images as used is because admonition isn't registering the files as references in Obsidian so things like backlinks etc are not working either. I see the plugin is in maintenance mode so I suspect that won't be fixed any time soon.

I haven't used admonition myself so unsure how it is used, is the pattern ![](_attachments/image.png) the only way to define an image reference?
If it is, I should be able to add a simple check for it.

@husjon husjon added the enhancement New feature or request label Mar 17, 2024
@davidkopp
Copy link
Author

Inside an admonition you can use all possible ways of including images. So besides the standard markdown way I used in the example, also Wikilinks could be used.
It would be great, if your plugin could support admonitions! My use case is the import of annotations from Zotero into Obsidian. Admonitions are a great way for me, to include highlights, images, etc. from Zotero in a way that looks great.

Example:

```ad-custom-image
title: <mark style="background: white;color: black">Cruz.deBekker.2023.EnergyMetricsSoftwareEngineering</mark> (Image) [(pg. 2)](zotero://open-pdf/library/items/HBL2XWHW?page=2)

![](_attachments/zotero/Cruz.deBekker.2023.EnergyMetricsSoftwareEngineering/Cruz.deBekker.2023.EnergyMetricsSoftwareEngineering-2-x37-y177.png)

Plot of power consumption over time. The area in gray is equivalent to the energy consumption between t=1st=1s and t=9st=9s.
```

Screenshot:

@husjon husjon removed the enhancement New feature or request label Mar 17, 2024
@husjon
Copy link
Owner

husjon commented Mar 17, 2024

Thanks, the issue with implementing this is that the cleaner would need to go through each file in a users vault and parse out what could be an image and/or file link.

As described in #44, this is something that is not preferential as it would increase the time it takes for a cleanup cycle drastically.
It might work fine for a small vault, but as the vault increases the search time increases by a lot.

The best solution for this issue in particular (admonition) is for the plugin to register which files are being referenced using the Obsidian API but with the maintenance mode this might take a while.

As a temporary solution that could help you is to add image links to the frontmatter.

images: "[[path/to/image.png]]"

This will register the image / file in the Obsidian resolvedLinks metadata cache which the cleaner is using to find files which should be removed or not.

@husjon
Copy link
Owner

husjon commented May 2, 2024

Hi @davidkopp I haven't been able to get to a proper conclusion regarding implementing this.
However what I'd like to ask for is to gather some data about your vault.

The reasoning for this is that, if I am going to implement this, the implementation needs to read each file in the vault and parse out the admonition blocks as mentioned in my previous comment.
This is as you can imagine an expensive operation, the bigger the vault, the longer it will take to run each cleanup cycle.

My question is for you and others that might use Admonition who'd like this implemented, to run the following command in their vault Developer Console (Ctrl+Shift+i / Cmd+Shift+i).

I've added as many comments as possible but in short, it will go iterate through the vault using Obsidians metadata API to get some stats about how many markdown files are in the vault and how many code blocks are used (code block stats will be: how many, where they are located in the file and how big they are), this data will let me get an indication of how many files I can expect in a typical vault and get some ideas as to how long the operation might take.

const filesWithCodeBlocks = Object.values(this.app.vault.fileMap)
  .filter((f) => f.extension === "md") // filter out only markdown files
  .map((file) => this.app.metadataCache.getFileCache(file)) // get information about sections etc for file
  .filter((file) => file.sections) // filter out files that have sections (aka non-empty files)
  .map(
    (file) => file.sections.filter((section) => section.type === "code"), // find each section in each file that is a code-block
  )
  .filter((data) => data.length > 0); // filter out blocks files that did not contain any code-blocks

this.app.vault.create(
  "./obsidian-file-cleaner-redux-admonition.json",
  JSON.stringify(filesWithCodeBlocks),
);

This will create a file in the root of your vault named obsidian-file-cleaner-redux-admonition.json containing all the data about the vault.
Inspect it if you please 🙂

The output will be similar to the following except for the the comments and prettyfied formatting.

[
  [ // <- Single file containing one or more code blocks (this example, one file with 2 code-blocks)
    { "type": "code",
      "position": {  // Position of the code block in the markdown file
        "start": { "line": 0, "col": 0, "offset": 0 },
        "end": { "line": 2, "col": 3, "offset": 29 }
      }
    },
    { "type": "code",
      "position": {
        "start": { "line": 6, "col": 0, "offset": 42 },
        "end": { "line": 8, "col": 3, "offset": 71 }
      }
    }
  ]
]

Please create a gist with this file and add the link in a comment and I'll continue looking into this.

Thanks

@husjon husjon added the question Further information is requested label May 2, 2024
@davidkopp
Copy link
Author

Here the gist: https://gist.github.com/davidkopp/93a09b28d1fa099900d9f496040b8080

Thanks for your effort!

@husjon
Copy link
Owner

husjon commented May 25, 2024

Hi @davidkopp, I've realized a couple of weeks ago that I would need to rewrite the plugin so that extending support would be easier.
The initial restructuring work is done and is living in the branch rewrite-v1.0.0
I've also started looking into the work needed to support Admonition and the small test phase I've done so far has been very positive (branch admonition-support).

I'm hoping to have something that can be tested soon.
When I do, I would like if possible for you to clone your vault, then install the plugin using BRAT in the cloned vault, this way you can test it without causing potential havoc on your main vault. 🙂

@husjon husjon added this to the v1.0.0 milestone May 25, 2024
@husjon husjon added enhancement New feature or request and removed question Further information is requested labels May 27, 2024
@davidkopp
Copy link
Author

Great to hear 😀
Let me know when there is a version I can test with my vault.

With BRAT I need a tag or a manifest-beta.json for that, specifying a branch is not supported.

@husjon
Copy link
Owner

husjon commented May 30, 2024

Will do, I've set up a pre-release workflow in the rewrite branch so when I feel comfortable with the release candidate I'll do a v1.0.0-0 pre-release for you to test.

@husjon husjon mentioned this issue Jun 3, 2024
@husjon
Copy link
Owner

husjon commented Jun 3, 2024

I just created a pre-release which supports admonition and can be found at 1.0.0-3

There is still stuff to do for the v1.0.0 milestone that I want to add but I might release it earlier if the Admonition support works and isn't too slow :)

Looking forward to hearing the results.
The development console will contain information about how long it took to index the admonition blocks etc.
Example:
image

Oh and as mentioned in my previous comment (#45 (comment)), please make a backup / clone your vault before testing 😅

@davidkopp
Copy link
Author

davidkopp commented Jun 3, 2024

It works perfectly so far!

image

The 7 files to clean up were correct and no image inside an admonition was deleted 😀
I'm impressed. Great work!

@husjon
Copy link
Owner

husjon commented Jun 3, 2024

Awesome and thanks for testing.
I'm actually also kinda amazed at how quickly it managed to go through all the files (1065 files), I might have worried unnecessarily. 😅

I'm gonna look over the remaining issues I've planned for v1.0.0 and see if there is anything that needs to get in, if not I think I should be able to have a release ready by either end of the week or some time next week.

@husjon
Copy link
Owner

husjon commented Jun 18, 2024

I'm in the process of releasing v1.0.0 and I'm merging in the changes for supporting Admonition, the issue will be closed but don't hesitate to open it / a new one once it's released and issues should come up.

Thanks for your patience :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants