Selaa lähdekoodia

enhance: markdown use fence code when <src

Tienson Qin 4 vuotta sitten
vanhempi
sitoutus
762f5eff85
2 muutettua tiedostoa jossa 186 lisäystä ja 160 poistoa
  1. 9 3
      src/main/frontend/commands.cljs
  2. 177 157
      src/main/frontend/components/block.cljs

+ 9 - 3
src/main/frontend/commands.cljs

@@ -192,9 +192,15 @@
   ([type]
    (->block type nil))
   ([type optional]
-   (let [left (util/format "#+BEGIN_%s"
-                           (string/upper-case type))
-         right (util/format "\n#+END_%s" (string/upper-case type))
+   (let [format (get state/get-edit-block :block/format :markdown)
+         org? (= format :org)
+         left (if org?
+                (util/format "#+BEGIN_%s"
+                             (string/upper-case type))
+                (str "```" (let [t (string/lower-case type)] (if (= t "src") "" t))))
+         right (if org?
+                 (util/format "\n#+END_%s" (string/upper-case type))
+                 (str "\n```"))
          template (str
                    left
                    (if optional (str " " optional) "")

+ 177 - 157
src/main/frontend/components/block.cljs

@@ -1994,168 +1994,188 @@
      [:div.ml-4.text-lg
       (markup-elements-cp config result)]]))
 
+;; TODO: move to mldoc
+;; (defn- convert-md-src-to-custom-block
+;;   [item]
+;;   (let [{:keys [language options lines] :as options} (second item)
+;;         lang (string/lower-case (or language ""))]
+;;     (cond
+;;       (= lang "quote")
+;;       (let [content (string/trim (string/join "\n" lines))]
+;;         ["Quote" (first (mldoc/->edn content (mldoc/default-config :markdown)))])
+
+;;       (contains? #{"query" "note" "tip" "important" "caution" "warning" "pinned"} lang)
+;;       (let [content (string/trim (string/join "\n" lines))]
+;;         ["Custom" lang nil (first (mldoc/->edn content (mldoc/default-config :markdown))) content])
+
+;;       :else
+;;       ["Src" options])))
+
 (defn markup-element-cp
   [{:keys [html-export?] :as config} item]
-  (try
-    (match item
-      ["Properties" m]
-      [:div.properties
-       (let [format (:block/format config)]
-         (for [[k v] (dissoc m :roam_alias :roam_tags)]
-           (when (and (not (and (= k :macros) (empty? v))) ; empty macros
-                      (not (= k :title))
-                      (not (= k :filters)))
-             [:div.property
-              [:span.font-medium.mr-1 (str (name k) ": ")]
-              (if (coll? v)
-                (let [vals (for [item v]
-                             (if (coll? v)
-                               (let [config (if (= k :alias)
-                                              (assoc config :block/alias? true))]
-                                 (page-cp config {:block/name item}))
-                               (inline-text format item)))]
-                  (interpose [:span ", "] vals))
-                (inline-text format v))])))]
-
-      ["Paragraph" l]
-      ;; TODO: speedup
-      (if (re-find #"\"Export_Snippet\" \"embed\"" (str l))
-        (->elem :div (map-inline config l))
-        (->elem :div.is-paragraph (map-inline config l)))
-
-      ["Horizontal_Rule"]
-      (when-not (:slide? config)
-        [:hr])
-      ["Heading" h]
-      (block-container config h)
-      ["List" l]
-      (let [lists (divide-lists l)]
-        (if (= 1 (count lists))
-          (let [l (first lists)]
-            (->elem
-             (list-element l)
-             (map #(list-item config %) l)))
-          [:div.list-group
-           (for [l lists]
+  (let [format (or (:block/format config)
+                   :markdown)]
+    (try
+     (match item
+       ["Properties" m]
+       [:div.properties
+        (for [[k v] (dissoc m :roam_alias :roam_tags)]
+          (when (and (not (and (= k :macros) (empty? v))) ; empty macros
+                     (not (= k :title))
+                     (not (= k :filters)))
+            [:div.property
+             [:span.font-medium.mr-1 (str (name k) ": ")]
+             (if (coll? v)
+               (let [vals (for [item v]
+                            (if (coll? v)
+                              (let [config (if (= k :alias)
+                                             (assoc config :block/alias? true))]
+                                (page-cp config {:block/name item}))
+                              (inline-text format item)))]
+                 (interpose [:span ", "] vals))
+               (inline-text format v))]))]
+
+       ["Paragraph" l]
+       ;; TODO: speedup
+       (if (re-find #"\"Export_Snippet\" \"embed\"" (str l))
+         (->elem :div (map-inline config l))
+         (->elem :div.is-paragraph (map-inline config l)))
+
+       ["Horizontal_Rule"]
+       (when-not (:slide? config)
+         [:hr])
+       ["Heading" h]
+       (block-container config h)
+       ["List" l]
+       (let [lists (divide-lists l)]
+         (if (= 1 (count lists))
+           (let [l (first lists)]
              (->elem
               (list-element l)
-              (map #(list-item config %) l)))]))
-      ["Table" t]
-      (table config t)
-      ["Math" s]
-      (if html-export?
-        (latex/html-export s true true)
-        (latex/latex (str (dc/squuid)) s true true))
-      ["Example" l]
-      [:pre.pre-wrap-white-space
-       (join-lines l)]
-      ["Src" options]
-      (let [{:keys [language options lines pos_meta]} options
-            attr (if language
-                   {:data-lang language})
-            code (join-lines lines)]
-        (cond
-          html-export?
-          (highlight/html-export attr code)
-
-          :else
-          (let [language (if (contains? #{"edn" "clj" "cljc" "cljs" "clojure"} language) "text/x-clojure" language)]
-            (if (:slide? config)
-              (highlight/highlight (str (medley/random-uuid)) {:data-lang language} code)
-              [:div
-               (lazy-editor/editor config (str (dc/squuid)) attr code pos_meta)
-               (when (and (= language "text/x-clojure") (contains? (set options) ":results"))
-                 (sci/eval-result code))]))))
-      ["Quote" l]
-      (->elem
-       :blockquote
-       (markup-elements-cp config l))
-      ["Raw_Html" content]
-      (when (not html-export?)
-        [:div.raw_html {:dangerouslySetInnerHTML
-                        {:__html content}}])
-      ["Export" "html" options content]
-      (when (not html-export?)
-        [:div.export_html {:dangerouslySetInnerHTML
-                           {:__html content}}])
-      ["Hiccup" content]
-      (ui/catch-error
-       [:div.warning {:title "Invalid hiccup"}
-        content]
-       (-> (safe-read-string content)
-           (security/remove-javascript-links-in-href)))
-
-      ["Export" "latex" options content]
-      (if html-export?
-        (latex/html-export content true false)
-        (latex/latex (str (dc/squuid)) content true false))
-
-      ["Custom" "query" _options result content]
-      (try
-        (let [query (reader/read-string content)]
-          (custom-query config query))
-        (catch js/Error e
-          (println "read-string error:")
-          (js/console.error e)
-          [:div.warning {:title "Invalid query"}
-           content]))
-
-      ["Custom" "note" options result content]
-      (admonition config "note" options result)
-
-      ["Custom" "tip" options result content]
-      (admonition config "tip" options result)
-
-      ["Custom" "important" options result content]
-      (admonition config "important" options result)
-
-      ["Custom" "caution" options result content]
-      (admonition config "caution" options result)
-
-      ["Custom" "warning" options result content]
-      (admonition config "warning" options result)
-
-      ["Custom" "pinned" options result content]
-      (admonition config "pinned" options result)
-
-      ["Custom" name options l content]
-      (->elem
-       :div
-       {:class name}
-       (markup-elements-cp config l))
-
-      ["Latex_Fragment" l]
-      [:p.latex-fragment
-       (inline config ["Latex_Fragment" l])]
-
-      ["Latex_Environment" name option content]
-      (let [content (latex-environment-content name option content)]
-        (if html-export?
-          (latex/html-export content true true)
-          (latex/latex (str (dc/squuid)) content true true)))
-
-      ["Displayed_Math" content]
-      (if html-export?
-        (latex/html-export content true true)
-        (latex/latex (str (dc/squuid)) content true true))
-
-      ["Footnote_Definition" name definition]
-      (let [id (util/url-encode name)]
-        [:div.footdef
-         [:div.footpara
-          (conj
-           (markup-element-cp config ["Paragraph" definition])
-           [:a.ml-1 {:id (str "fn." id)
-                     :style {:font-size 14}
-                     :class "footnum"
-                     :on-click #(route-handler/jump-to-anchor! (str "fnr." id))}
-            [:sup.fn (str name "↩︎")]])]])
+              (map #(list-item config %) l)))
+           [:div.list-group
+            (for [l lists]
+              (->elem
+               (list-element l)
+               (map #(list-item config %) l)))]))
+       ["Table" t]
+       (table config t)
+       ["Math" s]
+       (if html-export?
+         (latex/html-export s true true)
+         (latex/latex (str (dc/squuid)) s true true))
+       ["Example" l]
+       [:pre.pre-wrap-white-space
+        (join-lines l)]
+       ["Quote" l]
+       (->elem
+        :blockquote
+        (markup-elements-cp config l))
+       ["Raw_Html" content]
+       (when (not html-export?)
+         [:div.raw_html {:dangerouslySetInnerHTML
+                         {:__html content}}])
+       ["Export" "html" options content]
+       (when (not html-export?)
+         [:div.export_html {:dangerouslySetInnerHTML
+                            {:__html content}}])
+       ["Hiccup" content]
+       (ui/catch-error
+        [:div.warning {:title "Invalid hiccup"}
+         content]
+        (-> (safe-read-string content)
+            (security/remove-javascript-links-in-href)))
+
+       ["Export" "latex" options content]
+       (if html-export?
+         (latex/html-export content true false)
+         (latex/latex (str (dc/squuid)) content true false))
+
+       ["Custom" "query" _options result content]
+       (try
+         (let [query (reader/read-string content)]
+           (custom-query config query))
+         (catch js/Error e
+           (println "read-string error:")
+           (js/console.error e)
+           [:div.warning {:title "Invalid query"}
+            content]))
+
+       ["Custom" "note" options result content]
+       (admonition config "note" options result)
+
+       ["Custom" "tip" options result content]
+       (admonition config "tip" options result)
+
+       ["Custom" "important" options result content]
+       (admonition config "important" options result)
+
+       ["Custom" "caution" options result content]
+       (admonition config "caution" options result)
+
+       ["Custom" "warning" options result content]
+       (admonition config "warning" options result)
+
+       ["Custom" "pinned" options result content]
+       (admonition config "pinned" options result)
+
+       ["Custom" name options l content]
+       (->elem
+        :div
+        {:class name}
+        (markup-elements-cp config l))
+
+       ["Latex_Fragment" l]
+       [:p.latex-fragment
+        (inline config ["Latex_Fragment" l])]
+
+       ["Latex_Environment" name option content]
+       (let [content (latex-environment-content name option content)]
+         (if html-export?
+           (latex/html-export content true true)
+           (latex/latex (str (dc/squuid)) content true true)))
+
+       ["Displayed_Math" content]
+       (if html-export?
+         (latex/html-export content true true)
+         (latex/latex (str (dc/squuid)) content true true))
+
+       ["Footnote_Definition" name definition]
+       (let [id (util/url-encode name)]
+         [:div.footdef
+          [:div.footpara
+           (conj
+            (markup-element-cp config ["Paragraph" definition])
+            [:a.ml-1 {:id (str "fn." id)
+                      :style {:font-size 14}
+                      :class "footnum"
+                      :on-click #(route-handler/jump-to-anchor! (str "fnr." id))}
+             [:sup.fn (str name "↩︎")]])]])
+
+       ["Src" options]
+       (when options
+         (let [{:keys [language options lines pos_meta]} options
+               attr (if language
+                      {:data-lang language})
+               code (join-lines lines)]
+           (cond
+             html-export?
+             (highlight/html-export attr code)
 
-      :else
-      "")
-    (catch js/Error e
-      (println "Convert to html failed, error: " e)
-      "")))
+             :else
+             (let [language (if (contains? #{"edn" "clj" "cljc" "cljs" "clojure"} language) "text/x-clojure" language)]
+               (if (:slide? config)
+                 (highlight/highlight (str (medley/random-uuid)) {:data-lang language} code)
+                 [:div
+                  (lazy-editor/editor config (str (dc/squuid)) attr code pos_meta)
+                  (when (and (= language "text/x-clojure") (contains? (set options) ":results"))
+                    (sci/eval-result code))])))))
+
+       :else
+       "")
+     (catch js/Error e
+       (println "Convert to html failed, error: " e)
+       ""))))
 
 (defn markup-elements-cp
   [config col]