Giter Site home page Giter Site logo

Comments (6)

wedens avatar wedens commented on June 5, 2024 1

I agree that it'd be great to be able to use heading as "front" and content under the heading as "back".

I have a lot of .org files that I can't use in either anki-editor or org-drill because they require structural changes :/

from anki-editor.

leoc avatar leoc commented on June 5, 2024

Yesterday I have played around a bit:

(setq anki-editor-quick-field-mapping '(("Basic" . ("Front" . nil))))

(defun anki-editor-get-quick-fields ()
  ""
  (interactive)
  (let* (quick-fields
         (note-heading (org-element-at-point))
         (note-title (org-element-property :title note-heading))
         (note-type (org-element-property :ANKI_NOTE_TYPE note-heading))
         (quick-field-mapping (alist-get note-type anki-editor-quick-field-mapping 'equal))
         (title-field (car quick-field-mapping))
         (contents-field (cdr quick-field-mapping))
         (contents-begin (org-element-property :contents-begin note-heading))
         (contents-end (org-element-property :contents-end note-heading))
	 (point-of-first-child (- (save-excursion
				    (if (org-goto-first-child)
					(point)
				      (point-max)))
				  1)))
    (when title-field
      (map-put quick-fields
	       title-field
	       (org-export-string-as note-title
                                     anki-editor--ox-anki-html-backend
                                     t
                                     anki-editor--ox-export-ext-plist)
	       'equal))
    (when contents-field
      (map-put quick-fields
	       contents-field
	       (cond
                ((and contents-begin contents-end)
                 (org-export-string-as
                  (buffer-substring
                   contents-begin
                   ;; in case the buffer is narrowed,
                   ;; e.g. by `org-map-entries' when
                   ;; scope is `tree'
                   (min (point-max) contents-end point-of-first-child))
                  anki-editor--ox-anki-html-backend
                  t
                  anki-editor--ox-export-ext-plist))
                (t ""))
	       'equal))
    quick-fields))

(defun anki-editor--build-fields ()
  "Build a list of fields from subheadings of current heading, each element of which is a cons cell, the car of which is field name and the cdr of which is field content."
  (save-excursion
    (let ((fields (anki-editor-get-quick-fields))
          (point-of-last-child (point)))
      (when (org-goto-first-child)
        (while (/= point-of-last-child (point))
          (setq point-of-last-child (point))
          (let* ((inhibit-message t)  ;; suppress echo message from `org-babel-exp-src-block'
                 (field-heading (org-element-at-point))
                 (field-name (substring-no-properties
                              (org-element-property
                               :raw-value
                               field-heading)))
                 (contents-begin (org-element-property :contents-begin field-heading))
                 (contents-end (org-element-property :contents-end field-heading)))

            (map-put fields
		     field-name
		     (cond
                      ((and contents-begin contents-end) (or (org-export-string-as
                                                              (buffer-substring
                                                               contents-begin
                                                               ;; in case the buffer is narrowed,
                                                               ;; e.g. by `org-map-entries' when
                                                               ;; scope is `tree'
                                                               (min (point-max) contents-end))
                                                              anki-editor--ox-anki-html-backend
                                                              t
                                                              anki-editor--ox-export-ext-plist)

                                                             ;; 8.2.10 version of
                                                             ;; `org-export-filter-apply-functions'
                                                             ;; returns nil for an input of empty string,
                                                             ;; which will cause AnkiConnect to fail
                                                             ""))
                      (t ""))
		     'equal)
            (org-forward-heading-same-level nil t))))
      (reverse fields))))

I tried to allow the contents of the heading to be associated with the Back field via:

(setq anki-editor-quick-field-mapping '(("Basic" . ("Front" . "Back"))))

But I do not know yet, how to export the contents of the heading without the properties drawer. Maybe you have an idea.

Maybe you have some hints. I will create a pull request after I played around a bit.

Edit: Nice! I found that

(setq anki-editor--ox-export-ext-plist '(:with-toc nil :anki-editor-mode t :with-drawers nil))

Hides the drawers correctly. I will prepare a pull request as soon as I can.

from anki-editor.

matiya avatar matiya commented on June 5, 2024

Yes please! I would also use something like this.

from anki-editor.

louietan avatar louietan commented on June 5, 2024

Sorry for the delay. I was focusing on my personal stuff last few months.

I would say this is really a good idea. I always find it's tricky to come up with a name for every note and sometimes I have to write the same content in heading and front field, so I was also thinking about a solution to this.

Your experimentation looks good, but it requires configuration for mapping between fields and note types. An alternative I prefer is just comparing the number of expected fields with the actual number of subtrees under the note to find out the missing field and use note heading as that field. I have implemented the idea in the develop branch, go check it out to see if that works for you.

from anki-editor.

leoc avatar leoc commented on June 5, 2024

Thanks @louietan

This means that I would still have to keep a Back heading for each card?
How would anki-editor know which is Front and which is Back?

I think your solution is okay for cards with more than two fields, because then we can check the missing field name. For two fields we still need configurability, no?

My solution allows to write:

* My question for the Front field
My content for the Back field

from anki-editor.

memeplex avatar memeplex commented on June 5, 2024

Why not just:

* Front
Back

from anki-editor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.