From 55c89e7737c29d7c532933514256c5b17e193065 Mon Sep 17 00:00:00 2001 From: Haoji Xu Date: Fri, 31 Jul 2020 10:03:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(blocks,=20parser):=20adds=20correct=20pars?= =?UTF-8?q?ing=20for=20nested=20page=20links=20in=20h=E2=80=A6=20(#311)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(blocks, parser): adds correct parsing for nested page links in hashtags; add auto link for hashtags and page links * style: fix testing code style --- src/cljc/athens/parser.cljc | 2 +- src/cljs/athens/parse_renderer.cljs | 7 ++++--- src/cljs/athens/views/blocks.cljs | 14 ++++++++------ test/athens/parser_test.clj | 3 +++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/cljc/athens/parser.cljc b/src/cljc/athens/parser.cljc index e3a34fc6d6..933195e573 100644 --- a/src/cljc/athens/parser.cljc +++ b/src/cljc/athens/parser.cljc @@ -44,7 +44,7 @@ hashtag = hashtag-bare | hashtag-delimited = <'#'> #'[^\\ \\+\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\?\\\"\\;\\:\\]\\[]+' (* Unicode: L = letters, M = combining marks, N = numbers *) - = <'#'> <'[['> #'[^\\]]+' <']]'> + = <'#'> <'[['> page-link-content <']]'> url-image = <'!'> url-link-text url-link-url diff --git a/src/cljs/athens/parse_renderer.cljs b/src/cljs/athens/parse_renderer.cljs index 1669446840..2b2b55fe42 100644 --- a/src/cljs/athens/parse_renderer.cljs +++ b/src/cljs/athens/parse_renderer.cljs @@ -96,12 +96,13 @@ (let [block (pull db/dsdb '[*] [:block/uid uid])] [:span (use-style block-ref {:class "block-ref"}) [:span {:class "contents" :on-click #(navigate-uid uid)} (parse-and-render (:block/string @block) uid)]])) - :hashtag (fn [tag-name] - (let [node (pull db/dsdb '[*] [:node/title tag-name])] + :hashtag (fn [& tag-name] + (let [parsed-name (concat tag-name) + node (pull db/dsdb '[*] [:node/title parsed-name])] [:span (use-style hashtag {:class "hashtag" :on-click #(navigate-uid (:block/uid @node))}) [:span {:class "formatting"} "#"] - [:span {:class "contents"} tag-name]])) + [:span {:class "contents"} parsed-name]])) :url-image (fn [{url :url alt :alt}] [:img (use-style image {:class "url-image" :alt alt diff --git a/src/cljs/athens/views/blocks.cljs b/src/cljs/athens/views/blocks.cljs index 097faf9383..7b8ec101c5 100644 --- a/src/cljs/athens/views/blocks.cljs +++ b/src/cljs/athens/views/blocks.cljs @@ -346,12 +346,14 @@ uid (gen-block-uid)] (db-fn inner-title now uid))) (str "[[" inner-title "]]"))) - :hashtag (fn [title] - (when (and (string? title) (link-fn title)) - (let [now (now-ts) - uid (gen-block-uid)] - (db-fn title now uid))) - (str "#" title))} (parser/parse-to-ast source-str))) + :hashtag (fn [& title] + (let [inner-title (apply + title)] + (when (and (string? inner-title) + (link-fn inner-title)) + (let [now (now-ts) + uid (gen-block-uid)] + (db-fn inner-title now uid))) + (str "#" inner-title)))} (parser/parse-to-ast source-str))) (defn on-change diff --git a/test/athens/parser_test.clj b/test/athens/parser_test.clj index 577fc4d285..74ae07380d 100644 --- a/test/athens/parser_test.clj +++ b/test/athens/parser_test.clj @@ -54,6 +54,9 @@ [:block "that’s " [:hashtag "very cool"] ", yeah"] "that’s #[[very cool]], yeah" + [:block "also here's " [:hashtag "nested " [:page-link "links"]] " in hashtags!"] + "also here's #[[nested [[links]]]] in hashtags!" + [:block "Ends after " [:hashtag "words_are_over"] "!"] "Ends after #words_are_over!"