louietan / anki-editor

Emacs minor mode for making Anki cards with Org
699 stars 87 forks source link

Adding a fetch function #57

Open bibekpokharel opened 4 years ago

bibekpokharel commented 4 years ago

It would be nice to not just push notes but also to be able to fetch the notes that have been pushed. This way any changes made in Anki would remain in sync. As of now my org files are only used once - to make the cards. If fetching was possible, the org files that create the anki cards could be maintained and used repeatedly. These org files would also be easier to read and review - which is harder in the inbuilt anki editor.

louietan commented 4 years ago

It is a nice addition to have and is trivial to implement for plain text fields I think.

The chanllenge is that not only do we need a converter to translate Anki HTML back to Org, but we also requires the converter to be able to produce the exact same Org elements as the original one.

Org has so many constructs, for some of them, when converted to HTML, lots of information is lost, for which reason I don't even think it's possible to produce the identical Org contents to the original one from HTML, examples I could think of right now:

  1. Ordered lists. How do we know if a <ol> was 1. or 1)?
  2. Unordered lists. How do we know if a <li> was + or * or -?
  3. Code and verbatim. How do we know if a <code> was ~ or =?
  4. Embeded HTML. How do we know if an HTML fragment was embeded in Org with #+BEGIN_EXPORT html?

In additional, there're elements in HTML that are not available in Org, e.g. styling (colors, alignments), these would be lost when converted back to Org and we are not able to detect and warn the user of the lost.

bibekpokharel commented 4 years ago

I agree. Some information would definitely be lost if the original information was in HTML form. However, the first step would be to apply this to the cards created via anki-editor-mode, for which there should exist a reversible transformation. We also pick a convention and inform the users that for certain information, where there are multiple options, one option will be chosen by default - for example code will always be surrounded by =.

nanjigen commented 4 years ago

As an alternative what about utilizing the :ANKI_NOTE_ID: field, and something like this anki addon and some more code to trigger opening to the position of the anki card subheader in its org-file for editing when pressing the edit key in Anki itself?

We still won't gain editing in ankidroid, but at least this way we can continue managing cards in org-mode and make use of Emacs/evil keys for editing - potentially making the editing process less painful.

okomestudio commented 4 years ago

The fetch function would be a very welcome addition. Implementing some sort of reversible transformation as mentioned above would certainly be nice for notes created by anki-editor , but falling back to fetching HTML content as is or something equivalent would still be very useful to "import" notes into an org files, which we can treat as the master to sync subsequent edits. It is unfortunate that Anki mutates input for presentation and doesn't appear to provide a way to work on the source...

adelin-b commented 4 years ago

For now I think using an html-to-org like pandoc on each text field would be good enough for a first version. Im looking up this package for a year now and I cannot use it as I need double sync / importing from anki to an orgfile

For later I think it should be keeping the original html tags and non-org styles in hidens property that would get their text content replaced by template . Or write the raw html in orgmode in a html code block

adelin-b commented 3 years ago

I did my own dirty version of the fetch function.

# in shell
deno run --allow-net anki.ts "*"
# or
deno run --allow-net anki.ts "Deckname::subdeckname"

# in emacs evil mode
:read !deno run --allow-net anki.ts "*"

https://gist.github.com/adelin-b/362a04fb29dd7ee618952fe2fe5e2417

However it seems that anki-editor is auto escaping html elements so they end up visible when the cards are pushed back into anki. How can we avoid this ?

orgtre commented 2 years ago

I've also had (partial) success with writing a fetch function, this was originally intended for merging into anki-editor, but is now finally published here: https://github.com/orgtre/ankiorg. Also org-anki recently added the function org-anki-import-deck which might be a useful alternative.