Merge pull request #8654 from ksqsf/feat/cloze-cue

feat(srs): support cloze cues
pull/8682/head
rcmerci 2023-02-21 21:34:56 +08:00 committed by GitHub
commit 7236ff0313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 3 deletions

View File

@ -534,6 +534,17 @@
;;; register cloze macro
(def ^:private cloze-cue-separator "\\\\")
(defn- cloze-parse
"Parse the cloze content, and return [answer cue]."
[content]
(let [parts (string/split content cloze-cue-separator -1)]
(if (<= (count parts) 1)
[content nil]
(let [cue (string/trim (last parts))]
;; If there are more than one separator, only the last component is considered the cue.
[(string/trimr (string/join cloze-cue-separator (drop-last parts))) cue]))))
(rum/defcs cloze-macro-show < rum/reactive
{:init (fn [state]
@ -543,12 +554,15 @@
[state config options]
(let [shown?* (:shown? state)
shown? (rum/react shown?*)
toggle! #(swap! shown?* not)]
toggle! #(swap! shown?* not)
[answer cue] (cloze-parse (string/join ", " (:arguments options)))]
(if (or shown? (:show-cloze? config))
[:a.cloze-revealed {:on-click toggle!}
(util/format "[%s]" (string/join ", " (:arguments options)))]
(util/format "[%s]" answer)]
[:a.cloze {:on-click toggle!}
"[...]"])))
(if (string/blank? cue)
"[...]"
(str "(" cue ")"))])))
(component-macro/register cloze-macro-name cloze-macro-show)