feat(copy-as/export): add option: :newline-after-block

This commit is contained in:
rcmerci
2023-03-13 23:17:39 +08:00
committed by Tienson Qin
parent 2cfe943169
commit bce6174b5a
5 changed files with 137 additions and 100 deletions

View File

@@ -1,21 +1,18 @@
(ns frontend.handler.export.text
"export blocks/pages as text"
(:refer-clojure :exclude [map filter mapcat concat remove newline])
(:require
[clojure.string :as string]
[frontend.db :as db]
[frontend.extensions.zip :as zip]
[frontend.handler.export.common :as common :refer
[*state*
simple-ast-malli-schema
raw-text space newline* indent simple-asts->string]]
[frontend.state :as state]
[frontend.util :as util :refer [mapcatv concatv removev]]
[goog.dom :as gdom]
[logseq.graph-parser.mldoc :as gp-mldoc]
[malli.core :as m]
[promesa.core :as p]))
(:require [clojure.string :as string]
[frontend.db :as db]
[frontend.extensions.zip :as zip]
[frontend.handler.export.common :as common :refer
[*state* indent newline* raw-text simple-ast-malli-schema
simple-asts->string space]]
[frontend.state :as state]
[frontend.util :as util :refer [concatv mapcatv removev]]
[goog.dom :as gdom]
[logseq.graph-parser.mldoc :as gp-mldoc]
[malli.core :as m]
[promesa.core :as p]))
;;; block-ast, inline-ast -> simple-ast
@@ -41,10 +38,17 @@
size* (and size [space (raw-text (reduce str (repeat size "#")))])
marker* (and marker (raw-text marker))]
(set! *state* (assoc *state* :current-level level))
(removev nil? (concatv heading* size*
[space marker* space priority* space]
(mapcatv inline-ast->simple-ast title)
[(newline* 1)]))))
(let [simple-asts
(removev nil? (concatv
(when (and (get-in *state* [:export-options :newline-after-block])
(not (get-in *state* [:newline-after-block :current-block-is-first-heading-block?])))
[(newline* 2)])
heading* size*
[space marker* space priority* space]
(mapcatv inline-ast->simple-ast title)
[(newline* 1)]))]
(set! *state* (assoc-in *state* [:newline-after-block :current-block-is-first-heading-block?] false))
simple-asts)))
(declare block-list)
(defn- block-list-item
@@ -309,52 +313,60 @@
(m/=> block-ast->simple-ast [:=> [:cat [:sequential :any]] [:sequential simple-ast-malli-schema]])
(defn- block-ast->simple-ast
[block]
(removev
nil?
(let [[ast-type ast-content] block]
(case ast-type
"Paragraph"
(concatv (mapcatv inline-ast->simple-ast ast-content) [(newline* 1)])
"Paragraph_line"
(assert false "Paragraph_line is mldoc internal ast")
"Paragraph_Sep"
[(newline* ast-content)]
"Heading"
(block-heading ast-content)
"List"
(block-list ast-content)
("Directive" "Results" "Property_Drawer" "Export" "CommentBlock" "Custom")
nil
"Example"
(block-example ast-content)
"Src"
(block-src ast-content)
"Quote"
(block-quote ast-content)
"Latex_Fragment"
(block-latex-fragment ast-content)
"Latex_Environment"
(block-latex-env (rest block))
"Displayed_Math"
(block-displayed-math ast-content)
"Drawer"
(block-drawer (rest block))
(let [newline-after-block? (get-in *state* [:export-options :newline-after-block])]
(removev
nil?
(let [[ast-type ast-content] block]
(case ast-type
"Paragraph"
(let [{:keys [origin-ast]} (meta block)
current-block-is-first-heading-block? (get-in *state* [:newline-after-block :current-block-is-first-heading-block?])]
(set! *state* (assoc-in *state* [:newline-after-block :current-block-is-first-heading-block?] false))
(concatv
(when (and origin-ast newline-after-block? (not current-block-is-first-heading-block?))
[(newline* 2)])
(mapcatv inline-ast->simple-ast ast-content)
[(newline* 1)]))
"Paragraph_line"
(assert false "Paragraph_line is mldoc internal ast")
"Paragraph_Sep"
[(newline* ast-content)]
"Heading"
(block-heading ast-content)
"List"
(block-list ast-content)
("Directive" "Results" "Property_Drawer" "Export" "CommentBlock" "Custom")
nil
"Example"
(block-example ast-content)
"Src"
(block-src ast-content)
"Quote"
(block-quote ast-content)
"Latex_Fragment"
(block-latex-fragment ast-content)
"Latex_Environment"
(block-latex-env (rest block))
"Displayed_Math"
(block-displayed-math ast-content)
"Drawer"
(block-drawer (rest block))
;; TODO: option: toggle Property_Drawer
;; "Property_Drawer"
;; (block-property-drawer ast-content)
"Footnote_Definition"
(block-footnote-definition (rest block))
"Horizontal_Rule"
block-horizontal-rule
"Table"
(block-table ast-content)
"Comment"
(block-comment ast-content)
"Raw_Html"
(block-raw-html ast-content)
"Hiccup"
(block-hiccup ast-content)
(assert false (print-str :block-ast->simple-ast ast-type "not implemented yet"))))))
"Footnote_Definition"
(block-footnote-definition (rest block))
"Horizontal_Rule"
block-horizontal-rule
"Table"
(block-table ast-content)
"Comment"
(block-comment ast-content)
"Raw_Html"
(block-raw-html ast-content)
"Hiccup"
(block-hiccup ast-content)
(assert false (print-str :block-ast->simple-ast ast-type "not implemented yet")))))))
(defn- inline-ast->simple-ast
[inline]
@@ -410,7 +422,6 @@
;;; block-ast, inline-ast -> simple-ast (ends)
;;; export fns
(defn- export-helper
@@ -423,7 +434,8 @@
:remove-emphasis? (contains? remove-options :emphasis)
:remove-page-ref-brackets? (contains? remove-options :page-ref)
:remove-tags? (contains? remove-options :tag)
:keep-only-level<=N (:keep-only-level<=N other-options)}})]
:keep-only-level<=N (:keep-only-level<=N other-options)
:newline-after-block (:newline-after-block 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)