Bläddra i källkod

Fix #4088 by reverting plain link regex

The link tests weren't being run because test namespaces need to end with '-test'.
Since these tests weren't being run, they didn't catch when the
plain-link regex was borked in #3520
Gabriel Horner 3 år sedan
förälder
incheckning
203c39828f

+ 9 - 2
src/main/frontend/handler/link.cljs

@@ -1,7 +1,8 @@
 (ns frontend.handler.link
   (:require [frontend.util :as util]))
 
-(def plain-link "(\bhttps?://)?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]")
+(def plain-link "(?:http://www\\.|https://www\\.|http://|https://){1}[a-z0-9]+(?:[\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(?:.*)*")
+;; Based on https://orgmode.org/manual/Link-Format.html#Link-Format
 (def org-link-re-1 (re-pattern (util/format "\\[\\[(%s)\\]\\[(.+)\\]\\]" plain-link)))
 (def org-link-re-2 (re-pattern (util/format "\\[\\[(%s)\\]\\]" plain-link)))
 (def markdown-link-re (re-pattern (util/format "^\\[(.+)\\]\\((%s)\\)" plain-link)))
@@ -21,8 +22,14 @@
      :url (nth matches 2)
      :label (second matches)}))
 
+(defn- plain-link?
+  [link]
+  (when (util/url? link)
+    {:type "plain-link"
+     :url link}))
+
 (defn link?
   [link]
-  (or (util/url? link)
+  (or (plain-link? link)
       (org-link? link)
       (markdown-link? link)))

+ 4 - 6
src/test/frontend/handler/test_link.cljs → src/test/frontend/handler/link_test.cljs

@@ -1,4 +1,4 @@
-(ns frontend.handler.test-link
+(ns frontend.handler.link-test
   (:require [cljs.test :refer [are deftest testing]]
             [frontend.handler.link :as link]))
 
@@ -9,7 +9,7 @@
       "[[google.com][google]]" nil
       "[[google.com]]" nil
       "[google](google.com)" nil))
-  
+
   (testing "plain links"
     (are [x y] (= (link/link? x) y)
       "http://www.google.com"
@@ -18,7 +18,7 @@
       "http://google.com"
       {:type "plain-link" :url "http://google.com"}))
 
-  (testing "org links"
+  (testing "org links with labels"
     (are [x y] (= (link/link? x) y)
       "[[http://www.google.com][google]]"
       {:type "org-link" :url "http://www.google.com" :label "google"}
@@ -32,7 +32,7 @@
       "[[https://google.com][google]]"
       {:type "org-link" :url "https://google.com" :label "google"}))
 
-  (testing "org links"
+  (testing "org links without labels"
     (are [x y] (= (link/link? x) y)
       "[[http://www.google.com]]"
       {:type "org-link" :url "http://www.google.com" :label nil}
@@ -47,5 +47,3 @@
 
       "[google](https://www.google.com)"
       {:type "markdown-link" :url "https://www.google.com" :label "google"})))
-
-#_(cljs.test/test-ns 'frontend.test-link)

+ 1 - 1
src/test/frontend/text_test.cljs

@@ -143,7 +143,7 @@
       :tags "foo" "foo"
       :tags "foo, bar" #{"foo" "bar"}
       :tags "foo,bar" #{"foo" "bar"}
-      :tags "[[foo]]" "[[foo]]"
+      :tags "[[foo]]" #{"foo"}
       :tags "[[foo]] [[bar]]" #{"foo" "bar"}
       :tags "[[foo]], [[bar]]" #{"foo" "bar"}
       :tags "[[foo]], [[bar]], #baz" #{"foo" "bar" "baz"}