Skip to content

Commit

Permalink
fix(editor): bad format copy from html
Browse files Browse the repository at this point in the history
- should keep space between tags
- should avoid raw data-url
  • Loading branch information
andelf authored and tiensonqin committed Jul 4, 2022
1 parent cc643f8 commit e3b749e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/main/frontend/extensions/html_parser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
:else
nil)
children' (map-join children)]
(when-not (string/blank? children')
(when (not-empty children')
(str (if (string? pattern) pattern (apply str pattern))
children'
(if (string? pattern) pattern (apply str (reverse pattern)))))))
Expand Down Expand Up @@ -122,11 +122,15 @@
:org (util/format "[[%s][%s]]" href label)
nil))))
:img (let [src (:src attrs)
alt (or (:alt attrs) "")]
(case format
:markdown (util/format "![%s](%s)" alt src)
:org (util/format "[[%s][%s]]" src alt)
nil))
alt (or (:alt attrs) "")
;; reject url-encoded and utf8-encoded(svg)
unsafe-data-url? (and (string/starts-with? src "data:")
(not (re-find #"^data:.*?;base64," src)))]
(when-not unsafe-data-url?
(case format
:markdown (util/format "![%s](%s)" alt src)
:org (util/format "[[%s][%s]]" src alt)
nil)))
:p (util/format "%s"
(map-join children))

Expand Down Expand Up @@ -218,17 +222,16 @@
(for [x hiccup]
(single-hiccup-transform x))
(single-hiccup-transform hiccup))]
(string/replace (apply str result) #"\n\n+" "\n\n")))
(apply str result)))

(defn hiccup->doc
[format hiccup]
(let [s (hiccup->doc-inner format hiccup {})]
(if (string/blank? s)
""
(-> s
(string/trim)
(string/replace "\n\n\n\n" "\n\n")
(string/replace "\n\n\n" "\n\n")))))
string/trim
(string/replace #"\n\n+" "\n\n")))))

(defn html-decode-hiccup
[hiccup]
Expand Down

0 comments on commit e3b749e

Please sign in to comment.