Skip to content

Commit

Permalink
refactor: add markdown unordered list as blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Apr 7, 2021
1 parent fda3f67 commit 10ab134
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"ignore": "^5.1.8",
"is-svg": "4.2.2",
"jszip": "^3.5.0",
"mldoc": "0.6.6",
"mldoc": "0.6.7",
"mousetrap": "^1.6.5",
"path": "^0.12.7",
"react": "^17.0.1",
Expand Down
4 changes: 1 addition & 3 deletions src/main/frontend/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@
(case format
:org
"*"
:markdown
"#"

"")))
"-")))

(defn get-hr
[format]
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/format/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@
(->
{:uuid (db/new-block-id)
:content content
:level 2
:level 1
:meta {:start-pos 0
:end-pos (or first-block-start-pos
(utf8/length encoded-content))}
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
file-path (str "/" path)
default-content (case (name format)
"org"
"** What's **Contents**?\n*** It's a normal page called [[Contents]], you can use it for:\n**** 1. table of content/index/MOC\n**** 2. pinning/bookmarking favorites pages/blocks (e.g. [[Logseq]])\n**** 3. You can also put many different things, depending on your personal workflow."
"* What's **Contents**?\n** It's a normal page called [[Contents]], you can use it for:\n*** 1. table of content/index/MOC\n*** 2. pinning/bookmarking favorites pages/blocks (e.g. [[Logseq]])\n*** 3. You can also put many different things, depending on your personal workflow."
"markdown"
"## What's **Contents**?\n### It's a normal page called [[Contents]], you can use it for:\n#### 1. table of content/index/MOC\n#### 2. pinning/bookmarking favorites pages/blocks (e.g. [[Logseq]])\n#### 3. You can also put many different things, depending on your personal workflow."
"")]
Expand Down
35 changes: 19 additions & 16 deletions src/main/frontend/modules/file/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
[frontend.modules.outliner.tree :as tree]
[promesa.core :as p]))

(defn clip-content
[content]
(->
(string/replace content #"^\n+" "")
(string/replace #"^#+" "")
(string/replace #"\n+$" "")))

(defn transform-content
[pre-block? content level]
[{:block/keys [format pre-block? content unordered]} level]
(let [content (or content "")]
(if pre-block?
(clip-content content)
(let [prefix (->>
(repeat level "#")
(apply str))
new-content (clip-content content)]
(str prefix " " new-content)))))
(string/trim content)
(let [prefix (cond
(= format :org)
(->>
(repeat level "*")
(apply str))

(and (= format :markdown) (not unordered)) ; heading
""

:else
(str (->>
(repeat (dec level) " ")
(apply str))
"-")) ; TODO:
new-content (string/trim content)]
(str prefix " " new-content)))))

(defn tree->file-content
[tree init-level]
Expand All @@ -40,8 +44,7 @@
level init-level]
(if (nil? f)
(string/join "\n" block-contents)
(let [content (transform-content
(:block/pre-block? f) (:block/content f) level)
(let [content (transform-content f level)
new-content
(if-let [children (seq (:block/children f))]
[content (tree->file-content children (inc level))]
Expand Down
13 changes: 10 additions & 3 deletions src/main/frontend/text.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,21 @@
([text format]
(remove-level-spaces text format false))
([text format space?]
(if-not (string/blank? text)
(cond
(string/blank? text)
""

(and (= "markdown" (name format))
(string/starts-with? text "---"))
text

:else
(let [pattern (util/format
(if space?
"^[%s]+\\s+"
"^[%s]+\\s?")
(config/get-block-pattern format))]
(string/replace-first text (re-pattern pattern) ""))
"")))
(string/replace-first (string/triml text) (re-pattern pattern) "")))))

(defn append-newline-after-level-spaces
[text format]
Expand Down
7 changes: 2 additions & 5 deletions src/main/frontend/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1025,12 +1025,9 @@
"")
new-block (case (name text-format)
"org"
"** "
"* "

"markdown"
"## "

"")]
"- ")]
(if contents?
new-block
(str properties "\n\n" (if new-block? new-block))))))
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3853,10 +3853,10 @@ mkdirp@^0.5.4, mkdirp@~0.5.1:
dependencies:
minimist "^1.2.5"

[email protected].6:
version "0.6.6"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.6.6.tgz#b38b64f83424514a2cb29e3a1f2cfc8cffaaca2c"
integrity sha512-oktdGC4KRDn5nSeAJY9Io55az+YlZg+k7om0Tuo0YeO3LU0hsxnKRB0h5dsy/lufDQn7Ay2EcSPlNs+oPBzi3A==
[email protected].7:
version "0.6.7"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.6.7.tgz#7a6860202032146ae8bc14ea9850ae4ed1ff675f"
integrity sha512-jpcSytk28O8ushb3WNHApxw8SJCk0FndCWEIAUE7M1RigLGaI61wZX4tGQuFGLheT1iZS5n592KK1f0G0KKWIQ==
dependencies:
yargs "^12.0.2"

Expand Down

0 comments on commit 10ab134

Please sign in to comment.