org-anki icon indicating copy to clipboard operation
org-anki copied to clipboard

Images draft

Open theottm opened this issue 2 years ago • 8 comments

Hi!

I hacked a bit on a solution to integrate images from org into anki notes and vice-versa. It still has a few rough edges and I didn't try it out much yet. But it reached a point where at least for me it seems to work fine. I wanted to share it so other can try it out and comment the code and usage.

The general idea is the following. Media are stored in a special folder under the Anki dir. To sync with AnkiWeb the media need to be there, it seems not possible to use links. So for the direction org->anki we need to copy the images to the media dir of anki. Also we need to strip of the local path before the image name in the org links. For the anki->org direction we can just directly point to the media dir of anki. This is used when importing a deck that already had images.

Concretely I added the following:

  • a custom variable org-anki-media-dir which points to where anki stores media.
  • anki->org: in org-anki--html-to-org I wrapped the output string to add org-anki-media-dir as a prefix to all links. This relies on org-ml for now, not sure if you want to add this dependency.
  • org->anki: in org-anki--string-to-html I wrapped the string to remove the folder from all links. Also relies on org-ml
  • org-anki: I added a function org-anki-copy-images which copies all the images from links under the subtree at point (this could be changed) to anki media dir. This also relies on org-ml.

What do you think about adding org-ml as a new dependency? I feel more comfortable using this than direct string insertion.

WDYT?

theottm avatar Jun 06 '22 21:06 theottm

Thanks! I'm short on time this week, but will definitely look into this!

eyeinsky avatar Jun 10 '22 08:06 eyeinsky

It would be better to use link abbreviations to make it much less painful to change paths.

Also, org-anki-copy-images will filter out any non-file: links, including attachment:. Talking about it, maybe something like simple integration with org-attach will do the trick? You just use DIR property and point it to the media storage and org-attach does the rest.

ceed0 avatar Jun 30 '22 16:06 ceed0

I looked at the code and have following feedback:

  • make default media path nil and skip adding/removing links when it's nil, as media path is different for different OS's (e.g mine is ~/.local/share/Anki2/Markus/collection.media)
    • don't run the add/remove-links-code-path at all when media path is nil
  • having org-ml as dependency looks good (as it aims to provide a nicer API for org-made then it can very well become useful in other places too!)
  • when converting to html, will the links in org-mode ([[..]]) become html images? (I.e <img src='..' and not links like <a href='..'>)
    • perhaps the mentioned link abbreviations? For example [[anki-img:path/to/img.jpg]] would be turned to <img> tags, other links would be kept as <a> elements. (Not saying it should be like this, simply thinking out loud)

Linking related issue #17

eyeinsky avatar Jul 04 '22 21:07 eyeinsky

Thanks for your feedback both of you. I will have more time in a few weeks to look into this.

theottm avatar Jul 18 '22 22:07 theottm

No problem, its still very much work in progress.

On Fri, Jun 10, 2022, 10:36 Markus Läll @.***> wrote:

Thanks! I'm short on time this week, but will definitely look into this!

— Reply to this email directly, view it on GitHub https://github.com/eyeinsky/org-anki/pull/43#issuecomment-1152119244, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHKQJUIV24SDETSV656QEMDVOL5CLANCNFSM5YA2KBFQ . You are receiving this because you authored the thread.Message ID: @.***>

theottm avatar Oct 11 '22 06:10 theottm

Is this still being worked on? would be really neat to have image support

threddast avatar Jan 25 '23 08:01 threddast

This PR is valid, but the org-anki-media-path in "Files Changed" should be changed to org-anki-media-dir. At first, I didn't know that org-anki-copy-images needed to be called separately, and I pondered over it for a long time 😂. I used advice-add to override it, which is very convenient. Thank you.

LuciusChen avatar Sep 01 '23 06:09 LuciusChen

There is a slight issue with the org-ml-match-map in org-ml. When the text starts with bold formatting, the *text* will be recognized as an org heading and ignored. As a result, any text starting with * cannot be synchronized to Anki.

LuciusChen avatar Sep 01 '23 08:09 LuciusChen