|
9 | 9 | [logseq.publish.common :as publish-common] |
10 | 10 | [logseq.publish.model :as publish-model])) |
11 | 11 |
|
12 | | -(defonce version 1767014800432) |
| 12 | +(defonce version 1767016302995) |
13 | 13 |
|
14 | 14 | (def ref-regex |
15 | 15 | (js/RegExp. "\\[\\[([0-9a-fA-F-]{36})\\]\\]|\\(\\(([0-9a-fA-F-]{36})\\)\\)" "g")) |
16 | 16 |
|
17 | 17 | (defonce inline-config |
18 | 18 | (gp-mldoc/default-config :markdown)) |
19 | 19 |
|
| 20 | +(defn- block-ast |
| 21 | + [text] |
| 22 | + (when-not (string/blank? text) |
| 23 | + (->> (gp-mldoc/->edn text inline-config) |
| 24 | + (map first)))) |
| 25 | + |
20 | 26 | (defn inline-ast [text] |
21 | 27 | (gp-mldoc/inline->edn text inline-config)) |
22 | 28 |
|
|
510 | 516 | {:keys [uuid->title name->uuid graph-uuid]} ctx] |
511 | 517 | (cond |
512 | 518 | (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))) |
514 | 525 |
|
515 | 526 | (= "Break_Line" type) |
516 | 527 | [[:br]] |
|
551 | 562 | :else [""]) |
552 | 563 | href (cond |
553 | 564 | 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))) |
554 | 569 | (string? link-value) link-value |
555 | 570 | :else nil)] |
556 | 571 | (if href |
|
569 | 584 | (or (macro->nodes ctx macro-data) []) |
570 | 585 | (content->nodes (str data) uuid->title graph-uuid)) |
571 | 586 |
|
| 587 | + (= "Email" type) |
| 588 | + (let [email (str (:local_part data) "@" (:domain data))] |
| 589 | + [[:a {:href (str "mailto:" email)} email]]) |
| 590 | + |
572 | 591 | (or (= "Inline_Html" type) (= "Export_Snippet" type)) |
573 | 592 | (if-let [node (tweet-embed-from-html data)] |
574 | 593 | [node] |
|
577 | 596 | :else |
578 | 597 | (content->nodes (str data) uuid->title graph-uuid)))) |
579 | 598 |
|
| 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 | + |
580 | 674 | (defn- heading-level |
581 | 675 | [block depth] |
582 | 676 | (let [legacy (:block/heading-level block) |
|
601 | 695 | raw (if heading |
602 | 696 | (strip-heading-prefix raw) |
603 | 697 | 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)))))] |
610 | 706 | (let [container (cond |
611 | 707 | heading (keyword (str "h" heading ".block-text.block-heading")) |
| 708 | + block-level? :div.block-text |
612 | 709 | (some macro-embed-node? content) :div.block-text |
613 | 710 | :else :span.block-text)] |
614 | 711 | (into [container] content)))) |
|
715 | 812 |
|
716 | 813 | (defn block-content-from-ref [ref ctx] |
717 | 814 | (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))) |
723 | 824 |
|
724 | 825 | (comment |
725 | 826 | (def ^:private void-tags |
|
0 commit comments