Skip to content

Commit 15d7526

Browse files
committed
fix: ast blocks in block title
1 parent 2068123 commit 15d7526

2 files changed

Lines changed: 115 additions & 14 deletions

File tree

deps/publish/src/logseq/publish/publish.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ a:hover {
383383
padding: 12px 14px;
384384
}
385385

386-
.quote-block {
386+
.quote-block, blockquote {
387387
padding-left: 1.5em;
388388
margin: 1.5em 0;
389389
border-left: 2px solid var(--quote-border);

deps/publish/src/logseq/publish/render.cljs

Lines changed: 114 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
[logseq.publish.common :as publish-common]
1010
[logseq.publish.model :as publish-model]))
1111

12-
(defonce version 1767014800432)
12+
(defonce version 1767016302995)
1313

1414
(def ref-regex
1515
(js/RegExp. "\\[\\[([0-9a-fA-F-]{36})\\]\\]|\\(\\(([0-9a-fA-F-]{36})\\)\\)" "g"))
1616

1717
(defonce inline-config
1818
(gp-mldoc/default-config :markdown))
1919

20+
(defn- block-ast
21+
[text]
22+
(when-not (string/blank? text)
23+
(->> (gp-mldoc/->edn text inline-config)
24+
(map first))))
25+
2026
(defn inline-ast [text]
2127
(gp-mldoc/inline->edn text inline-config))
2228

@@ -510,7 +516,12 @@
510516
{:keys [uuid->title name->uuid graph-uuid]} ctx]
511517
(cond
512518
(or (= "Plain" type) (= "Spaces" type))
513-
(content->nodes data uuid->title graph-uuid)
519+
(let [sub-ast (inline-ast data)
520+
simple-plain? (and (= 1 (count sub-ast))
521+
(= "Plain" (ffirst sub-ast)))]
522+
(if (and (seq sub-ast) (not simple-plain?))
523+
(mapcat #(inline->nodes ctx %) sub-ast)
524+
(content->nodes data uuid->title graph-uuid)))
514525

515526
(= "Break_Line" type)
516527
[[:br]]
@@ -551,6 +562,10 @@
551562
:else [""])
552563
href (cond
553564
page-uuid (str "/page/" graph-uuid "/" page-uuid)
565+
(= "Complex" link-type) (when (and (map? link-value)
566+
(string? (:protocol link-value))
567+
(string? (:link link-value)))
568+
(str (:protocol link-value) "://" (:link link-value)))
554569
(string? link-value) link-value
555570
:else nil)]
556571
(if href
@@ -569,6 +584,10 @@
569584
(or (macro->nodes ctx macro-data) [])
570585
(content->nodes (str data) uuid->title graph-uuid))
571586

587+
(= "Email" type)
588+
(let [email (str (:local_part data) "@" (:domain data))]
589+
[[:a {:href (str "mailto:" email)} email]])
590+
572591
(or (= "Inline_Html" type) (= "Export_Snippet" type))
573592
(if-let [node (tweet-embed-from-html data)]
574593
[node]
@@ -577,6 +596,81 @@
577596
:else
578597
(content->nodes (str data) uuid->title graph-uuid))))
579598

599+
(defn- inline-coll->nodes
600+
[ctx inline-coll]
601+
(mapcat #(inline->nodes ctx %) (or inline-coll [])))
602+
603+
(declare block-ast->nodes)
604+
(defn- block-ast-coll->nodes
605+
[ctx content]
606+
(mapcat #(block-ast->nodes ctx %) (or content [])))
607+
608+
(defn- list-items->node
609+
[ctx items]
610+
(into
611+
[:ul]
612+
(map (fn [item]
613+
(let [content (let [content (:content item)]
614+
(if (and (sequential? content)
615+
(every? #(and (vector? %) (string? (first %))) content))
616+
(block-ast-coll->nodes ctx content)
617+
(inline-coll->nodes ctx content)))
618+
nested (when (seq (:items item))
619+
[(list-items->node ctx (:items item))])
620+
children (concat content nested)]
621+
(into [:li] children)))
622+
items)))
623+
624+
(defn- block-ast->nodes
625+
[ctx block-ast]
626+
(let [[type data] block-ast]
627+
(case type
628+
"Paragraph"
629+
(let [children (inline-coll->nodes ctx data)]
630+
(when (seq children)
631+
[(into [:p] children)]))
632+
633+
"Heading"
634+
(let [children (inline-coll->nodes ctx (:title data))]
635+
(when (seq children)
636+
[(into [:p] children)]))
637+
638+
"List"
639+
(when (seq data)
640+
[(list-items->node ctx data)])
641+
642+
"Quote"
643+
(when (seq data)
644+
[(into [:blockquote] (mapcat #(block-ast->nodes ctx %) data))])
645+
646+
"Example"
647+
(when (seq data)
648+
[[:pre (string/join "\n" data)]])
649+
650+
"Src"
651+
(let [lines (:lines data)
652+
code (if (sequential? lines) (string/join "\n" lines) (str lines))]
653+
[[:pre [:code code]]])
654+
655+
"Paragraph_Sep"
656+
[[:br]]
657+
658+
"Horizontal_Rule"
659+
[[:hr]]
660+
661+
(let [fallback (content->nodes (str data) (:uuid->title ctx) (:graph-uuid ctx))]
662+
(when (seq fallback)
663+
[(into [:p] fallback)])))))
664+
665+
(defn- block-ast-complex?
666+
[block-asts]
667+
(let [block-asts (seq block-asts)]
668+
(and block-asts
669+
(or (> (count block-asts) 1)
670+
(some (fn [[type _]]
671+
(not (contains? #{"Paragraph" "Heading"} type)))
672+
block-asts)))))
673+
580674
(defn- heading-level
581675
[block depth]
582676
(let [legacy (:block/heading-level block)
@@ -601,14 +695,17 @@
601695
raw (if heading
602696
(strip-heading-prefix raw)
603697
raw)
604-
format :markdown
605-
ctx (assoc ctx :format format)
606-
ast (inline-ast raw)
607-
content (if (seq ast)
608-
(mapcat #(inline->nodes ctx %) ast)
609-
(content->nodes raw (:uuid->title ctx) (:graph-uuid ctx)))]
698+
block-asts (when-not heading (block-ast raw))
699+
block-level? (and (not heading) (block-ast-complex? block-asts))
700+
content (if block-level?
701+
(mapcat #(block-ast->nodes ctx %) block-asts)
702+
(let [ast (inline-ast raw)]
703+
(if (seq ast)
704+
(mapcat #(inline->nodes ctx %) ast)
705+
(content->nodes raw (:uuid->title ctx) (:graph-uuid ctx)))))]
610706
(let [container (cond
611707
heading (keyword (str "h" heading ".block-text.block-heading"))
708+
block-level? :div.block-text
612709
(some macro-embed-node? content) :div.block-text
613710
:else :span.block-text)]
614711
(into [container] content))))
@@ -715,11 +812,15 @@
715812

716813
(defn block-content-from-ref [ref ctx]
717814
(let [raw (or (get ref "source_block_content") "")
718-
ast (inline-ast raw)
719-
content (if (seq ast)
720-
(mapcat #(inline->nodes ctx %) ast)
721-
(content->nodes raw (:uuid->title ctx) (:graph-uuid ctx)))]
722-
(into [:span.block-text] content)))
815+
block-asts (block-ast raw)
816+
block-level? (block-ast-complex? block-asts)
817+
content (if block-level?
818+
(mapcat #(block-ast->nodes ctx %) block-asts)
819+
(let [ast (inline-ast raw)]
820+
(if (seq ast)
821+
(mapcat #(inline->nodes ctx %) ast)
822+
(content->nodes raw (:uuid->title ctx) (:graph-uuid ctx)))))]
823+
(into [(if block-level? :div.block-text :span.block-text)] content)))
723824

724825
(comment
725826
(def ^:private void-tags

0 commit comments

Comments
 (0)