only copy-as/export level<N blocks for text export

This commit is contained in:
rcmerci
2023-03-13 16:54:36 +08:00
committed by Tienson Qin
parent 2ca70373af
commit 5efbd125de
5 changed files with 68 additions and 7 deletions

View File

@@ -44,7 +44,8 @@
:indent-style "dashes"
:remove-page-ref-brackets? false
:remove-emphasis? false
:remove-tags? false}})
:remove-tags? false
:keep-only-level<=N :all}})
;;; internal utils
(defn- get-blocks-contents
@@ -528,6 +529,29 @@
["Paragraph" inline-coll])
heading-ast)))
(defn keep-only-level<=n
[block-ast-coll n]
(-> (reduce
(fn [{:keys [result-ast-tcoll accepted-heading] :as r} ast]
(let [[heading-type {level :level}] ast
is-heading? (= heading-type "Heading")]
(cond
(and (not is-heading?) accepted-heading)
{:result-ast-tcoll (conj! result-ast-tcoll ast) :accepted-heading accepted-heading}
(and (not is-heading?) (not accepted-heading))
r
(and is-heading? (<= level n))
{:result-ast-tcoll (conj! result-ast-tcoll ast) :accepted-heading true}
(and is-heading? (> level n))
{:result-ast-tcoll result-ast-tcoll :accepted-heading false})))
{:result-ast-tcoll (transient []) :accepted-heading false}
block-ast-coll)
:result-ast-tcoll
persistent!))
;;; inline transformers
(defn remove-emphasis

View File

@@ -415,16 +415,22 @@
(defn- export-helper
[content format options]
(let [remove-options (set (:remove-options options))]
(let [remove-options (set (:remove-options options))
other-options (:other-options options)]
(binding [*state* (merge *state*
{:export-options
{:indent-style (or (:indent-style options) "dashes")
:remove-emphasis? (contains? remove-options :emphasis)
:remove-page-ref-brackets? (contains? remove-options :page-ref)
:remove-tags? (contains? remove-options :tag)}})]
:remove-tags? (contains? remove-options :tag)
:keep-only-level<=N (:keep-only-level<=N other-options)}})]
(let [ast (gp-mldoc/->edn content (gp-mldoc/default-config format))
ast (mapv common/remove-block-ast-pos ast)
ast (removev common/Properties-block-ast? ast)
keep-level<=n (get-in *state* [:export-options :keep-only-level<=N])
ast (if (= :all keep-level<=n)
ast
(common/keep-only-level<=n ast keep-level<=n))
ast* (common/replace-block&page-reference&embed ast)
ast** (if (= "no-indent" (get-in *state* [:export-options :indent-style]))
(mapv common/replace-Heading-with-Paragraph ast*)