Skip to content

Commit ef50e9c

Browse files
authored
feat: Support field-specific raw content with # raw (louietan#85)
* Support raw export in anki-editor--export-string * Test anki-editor--export-string * update reademe: Add support for raw text fields * Add example of mixing raw and formatted fields - Added a new example showing how to have both raw (unprocessed) and HTML-formatted fields within the same Anki note. - Also, modified "Raw fields" example because prior version with `ANKI_FORMAT` property in `Back` subheading does not work.
1 parent 65b64b3 commit ef50e9c

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

README.org

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ If you use Doom Emacs, add the followings to ~packages.el~ and ~config.el~ respe
7575
** Usage
7676

7777
*** The Layout of Notes
78-
78+
**** Note and Fields General Information
7979
The power of this mode comes from the builtin HTML export backend
8080
provided by Org, which enables you to use almost all the Org
8181
constructs for writing Anki notes: lists, code blocks, tables,
@@ -156,6 +156,29 @@ If you use Doom Emacs, add the followings to ~packages.el~ and ~config.el~ respe
156156
[fn:1] It should be noted that Org only allows letters, numbers, =_=
157157
and ~@~ in a tag but Anki allows more, so you may have to edit you
158158
Anki tags before they can be used in Org without any surprise.
159+
**** Controlling HTML Formatting
160+
By default, anki-editor converts Org syntax to HTML when exporting to Anki.
161+
The =:ANKI_FORMAT: nil= property can be used at the *note* level to disable this conversion for the entire note.
162+
163+
If you want to use both raw text fields and HTML-converted fields within a single note, you can now use the =# raw= prefix *within a field* to indicate that the field's content should be treated as raw text, bypassing HTML conversion.
164+
Any spaces, tabs, or newlines immediately following =# raw= are ignored.
165+
166+
#+BEGIN_SRC org
167+
,* Example Note with Mixed Formatting
168+
:PROPERTIES:
169+
:ANKI_NOTE_TYPE: Basic
170+
:END:
171+
172+
,** Front
173+
This field will be converted to HTML.
174+
- This is a list.
175+
- It will be rendered as an HTML list.
176+
177+
,** Back
178+
# raw
179+
This field will be treated as RAW text.
180+
It will be sent to Anki exactly as written.
181+
#+END_SRC
159182

160183
*** Commands
161184
To see the docs for the most recent commands use M-x describe-function (for more info see [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Name-Help.html][Emacs Manual - Help Commands]])

anki-editor-tests.el

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ actual body of the test."
134134
(unwind-protect ,test
135135
(anki-editor-test--teardown))))))
136136

137+
(anki-editor-deftest test--export-string-with-raw ()
138+
:doc "Test `anki-editor--export-string` with `# raw` prefix."
139+
:in "test-files/test.org"
140+
:test
141+
(progn
142+
(should (equal (anki-editor--export-string "# raw content" t) "content"))
143+
(should (equal (anki-editor--export-string "# raw content" t) "content"))
144+
(should (equal (anki-editor--export-string "# raw\ncontent" t) "content"))
145+
(should (equal (anki-editor--export-string "# raw" t) ""))))
146+
147+
(anki-editor-deftest test--export-string-without-raw ()
148+
:doc "Test `anki-editor--export-string` without `# raw` prefix."
149+
:in "test-files/test.org"
150+
:test
151+
(progn
152+
(should (equal (anki-editor--export-string "content" t) "<p>\ncontent</p>\n"))
153+
(should (equal (anki-editor--export-string "" t) ""))))
154+
137155
(ert-deftest test--concat-fields-should-concatenate-fields-into-string ()
138156
"Test `anki-editor--concat-fields' should concatenate fields into string."
139157
(should (equal (anki-editor--concat-fields '("Front" "Back")

anki-editor.el

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -503,19 +503,22 @@ The implementation is borrowed and simplified from ox-html."
503503
(funcall oldfun link desc info)))
504504

505505
(defun anki-editor--export-string (src fmt)
506-
"Export string SRC and format it if FMT."
507-
(if fmt
508-
(or (org-export-string-as
509-
src
510-
anki-editor--ox-anki-html-backend
511-
t
512-
anki-editor--ox-export-ext-plist)
513-
;; 8.2.10 version of
514-
;; `org-export-filter-apply-functions'
515-
;; returns nil for an input of empty string,
516-
;; which will cause AnkiConnect to fail
517-
"")
518-
src))
506+
"Export string SRC and format it if FMT.
507+
If the string starts with '# raw', return the string as is."
508+
(if (and (stringp src) (string-prefix-p "# raw" src))
509+
(replace-regexp-in-string "^# raw[ \t\n]*" "" src)
510+
(if fmt
511+
(or (org-export-string-as
512+
src
513+
anki-editor--ox-anki-html-backend
514+
t
515+
anki-editor--ox-export-ext-plist)
516+
;; 8.2.10 version of
517+
;; `org-export-filter-apply-functions'
518+
;; returns nil for an input of empty string,
519+
;; which will cause AnkiConnect to fail
520+
"")
521+
src)))
519522

520523
;;; Core primitives
521524

examples.org

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@
2424
* Raw fields
2525
:PROPERTIES:
2626
:ANKI_NOTE_TYPE: Basic
27+
:ANKI_FORMAT: nil
2728
:END:
2829

2930
** Front
30-
3131
How to send the content of a field or fields to Anki as is?
3232

3333
** Back
34-
:PROPERTIES:
35-
:ANKI_FORMAT: nil
36-
:END:
37-
3834
With property <code>:ANKI_FORMAT: nil</code>, content of the
3935
field will be sent to Anki <em>unprocessed</em>. You can use
4036
whatever Anki supports, like HTML tags.
@@ -44,6 +40,17 @@
4440
set in any ancestor entries or at the top of the file with
4541
<code>#+PROPERTY: ANKI_FORMAT nil</code>, it's also possible to
4642
override an outer level nil format with <code>:ANKI_FORMAT: t</code>.
43+
* Raw and HTML-formatted fields in the same note
44+
:PROPERTIES:
45+
:ANKI_NOTE_TYPE: Basic
46+
:END:
47+
48+
** Front
49+
This field will be formatted as HTML.
50+
51+
** Back
52+
# raw
53+
If you start a field with ~# raw~, the content will be sent to Anki as is.
4754

4855
* Is there a shorter way to write notes?
4956
:PROPERTIES:

0 commit comments

Comments
 (0)