mirror of
https://github.com/logseq/logseq.git
synced 2026-05-24 12:44:22 +00:00
refactor(mldoc): enhance inline parsing to preserve cell content on failure
This commit is contained in:
@@ -319,12 +319,18 @@
|
||||
(or (string/includes? content "^{")
|
||||
(string/includes? content "_{"))))
|
||||
|
||||
(declare inline->edn)
|
||||
|
||||
(defn- markdown-config?
|
||||
[config]
|
||||
(and (string? config)
|
||||
(boolean (re-find #"\"format\"\s*:\s*\"Markdown\"" config))))
|
||||
(defn inline->edn
|
||||
[text config]
|
||||
(try
|
||||
(if (string/blank? text)
|
||||
{}
|
||||
(-> text
|
||||
(inline-parse-json config)
|
||||
(common-util/json->clj)
|
||||
(cond-> (macro-with-script-markup? text)
|
||||
(normalize-macro-asts))))
|
||||
(catch :default _e
|
||||
[])))
|
||||
|
||||
(defn- backtick-run-length
|
||||
[s idx]
|
||||
@@ -413,11 +419,10 @@
|
||||
:else
|
||||
(recur (inc idx) start code-end cells)))
|
||||
(let [cells' (conj cells (subs s start))]
|
||||
(mapv string/trim
|
||||
(if (and trailing-pipe?
|
||||
(string/blank? (peek cells')))
|
||||
(pop cells')
|
||||
cells')))))))
|
||||
(if (and trailing-pipe?
|
||||
(string/blank? (peek cells')))
|
||||
(pop cells')
|
||||
cells'))))))
|
||||
|
||||
(defn- markdown-table-separator-line?
|
||||
[line]
|
||||
@@ -460,7 +465,10 @@
|
||||
(let [cell' (unescape-markdown-table-pipes (string/trim cell))]
|
||||
(if (string/blank? cell')
|
||||
[["Plain" ""]]
|
||||
(inline->edn cell' config))))
|
||||
(let [ast (inline->edn cell' config)]
|
||||
(if (seq ast)
|
||||
ast
|
||||
[["Plain" cell']])))))
|
||||
|
||||
(defn- table-row->inline-asts
|
||||
[line config]
|
||||
@@ -505,13 +513,18 @@
|
||||
:col_groups (table-col-groups fallback-table col-count)))
|
||||
fallback-table)))
|
||||
|
||||
(defn- table-ast-item?
|
||||
[[block _pos-meta]]
|
||||
(and (vector? block)
|
||||
(= "Table" (first block))))
|
||||
|
||||
(defn- normalize-markdown-table-asts
|
||||
[ast content config]
|
||||
(if (markdown-config? config)
|
||||
(if-not (some table-ast-item? ast)
|
||||
ast
|
||||
(let [payload (utf8/encode content)]
|
||||
(mapv (fn [[block pos-meta :as item]]
|
||||
(if (and (vector? block)
|
||||
(= "Table" (first block)))
|
||||
(if (table-ast-item? item)
|
||||
(let [{:keys [start_pos end_pos]} pos-meta
|
||||
source (utf8/substring payload start_pos end_pos)]
|
||||
(if (markdown-table-source-needs-normalization? source)
|
||||
@@ -519,8 +532,7 @@
|
||||
pos-meta]
|
||||
item))
|
||||
item))
|
||||
ast))
|
||||
ast))
|
||||
ast))))
|
||||
|
||||
(defn collect-page-properties
|
||||
[ast config]
|
||||
@@ -579,19 +591,6 @@
|
||||
[content format]
|
||||
(->edn "logseq_db_repo_stub" content format))
|
||||
|
||||
(defn inline->edn
|
||||
[text config]
|
||||
(try
|
||||
(if (string/blank? text)
|
||||
{}
|
||||
(-> text
|
||||
(inline-parse-json config)
|
||||
(common-util/json->clj)
|
||||
(cond-> (macro-with-script-markup? text)
|
||||
(normalize-macro-asts))))
|
||||
(catch :default _e
|
||||
[])))
|
||||
|
||||
(defn ast-link?
|
||||
[[type link]]
|
||||
(let [[ref-type ref-value] (:url link)]
|
||||
|
||||
@@ -189,18 +189,6 @@
|
||||
[]]
|
||||
[header groups col_groups]))))
|
||||
|
||||
(testing "non-markdown configs are not normalized"
|
||||
(let [ast [[["Table" {:header [[["Plain" "A"]]
|
||||
[["Plain" "B"]]]
|
||||
:groups []
|
||||
:col_groups [1 1]}]
|
||||
{:start_pos 0 :end_pos 14}]]]
|
||||
(is (= ast
|
||||
(normalize-markdown-table-asts
|
||||
ast
|
||||
"|A \\||`B | C`|"
|
||||
(gp-mldoc/default-config :org))))))
|
||||
|
||||
(testing "fallback table column groups are preserved"
|
||||
(let [ast [[["Table" {:header [[["Plain" "A"]]
|
||||
[["Plain" "B"]]]
|
||||
@@ -214,6 +202,21 @@
|
||||
md-config)]
|
||||
(is (= [1 1] (:col_groups table)))))
|
||||
|
||||
(testing "cell content is preserved when inline parsing fails"
|
||||
(let [ast [[["Table" {:header [[["Plain" "A"]]]
|
||||
:groups []
|
||||
:col_groups [1]}]
|
||||
{:start_pos 0 :end_pos 6}]]
|
||||
[[[_ table] _pos-meta]]
|
||||
(normalize-markdown-table-asts
|
||||
ast
|
||||
"|A \\||"
|
||||
"invalid config")]
|
||||
(is (= [[[["Plain" "A |"]]]
|
||||
[]
|
||||
[1]]
|
||||
[(:header table) (:groups table) (:col_groups table)]))))
|
||||
|
||||
(testing "unclosed code spans do not hide cell boundaries"
|
||||
(let [{:keys [header groups col_groups]}
|
||||
(markdown-table
|
||||
|
||||
Reference in New Issue
Block a user