Skip to content

Commit

Permalink
Add breadcrumbs to JAR, group, and security pages
Browse files Browse the repository at this point in the history
Google uses breadcrumbs to present search results in a cleaner fashion.

This will probably need to be revisited once #482 is decided.
  • Loading branch information
danielcompton committed Feb 11, 2016
1 parent f448984 commit aea8653
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
7 changes: 7 additions & 0 deletions resources/security.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div class="small-section">
<script type="application/ld+json">
{"@context":"http://schema.org",
"@type":"BreadcrumbList",
"itemListElement":[{"@type":"ListItem",
"position":1,
"item":{"@id":"https://clojars.org/security","name":"Security"}}]}
</script>
<h1>Found a security flaw?</h1>


Expand Down
4 changes: 3 additions & 1 deletion src/clojars/web/browse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
[clojars.db :refer [browse-projects count-all-projects
count-projects-before]]
[hiccup.form :refer [label submit-button text-field submit-button]]
[ring.util.response :refer [redirect]]))
[ring.util.response :refer [redirect]]
[clojars.web.structured-data :as structured-data]))

(defn browse-page [db account page per-page]
(let [project-count (count-all-projects db)
total-pages (-> (/ project-count per-page) Math/ceil .intValue)
projects (browse-projects db page per-page)]
(html-doc "All projects" {:account account :description "Browse all of the projects in Clojars"}
[:div.light-article.row
(structured-data/breadcrumbs [{:name "All projects" :url "https://clojars.org/projects"}])
[:h1 "All projects"]
[:div.small-section
[:form.browse-from {:method :get :action "/projects"}
Expand Down
7 changes: 6 additions & 1 deletion src/clojars/web/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,13 @@
(str "/" (:jar_name jar))
(str "/" (:group_name jar) "/" (:jar_name jar))))

(defn group-is-name?
"Is the group of the artifact the same as its name?"
[jar]
(= (:group_name jar) (:jar_name jar)))

(defn jar-name [jar]
(if (= (:group_name jar) (:jar_name jar))
(if (group-is-name? jar)
(:jar_name jar)
(str (:group_name jar) "/" (:jar_name jar))))

Expand Down
5 changes: 4 additions & 1 deletion src/clojars/web/group.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
[clojars.auth :refer [authorized?]]
[hiccup.element :refer [unordered-list]]
[hiccup.form :refer [text-field submit-button]]
[clojars.web.safe-hiccup :refer [form-to]]))
[clojars.web.safe-hiccup :refer [form-to]]
[clojars.web.structured-data :as structured-data]))

(defn show-group [db account groupname membernames & errors]
(html-doc (str groupname " group") {:account account :description (format "Clojars projects in the %s group" groupname)}
[:div.small-section.col-md-6.col-lg-6.col-sm-6.col-xs-12
(structured-data/breadcrumbs [{:url (str "https://clojars.org/groups/" groupname)
:name groupname}])
[:h1 (str groupname " group")]
[:h2 "Projects"]
(unordered-list (map jar-link (jars-by-groupname db groupname)))
Expand Down
22 changes: 16 additions & 6 deletions src/clojars/web/jar.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns clojars.web.jar
(:require [clojars.web.common :refer [html-doc jar-link group-link
tag jar-url jar-name user-link
tag jar-url jar-name group-is-name? user-link
jar-fork? single-fork-notice
simple-date]]
hiccup.core
Expand All @@ -13,7 +13,8 @@
[clojars.stats :as stats]
[ring.util.codec :refer [url-encode]]
[cheshire.core :as json]
[clojars.web.helpers :as helpers]))
[clojars.web.helpers :as helpers]
[clojars.web.structured-data :as structured-data]))

(defn url-for [jar]
(str (jar-url jar) "/versions/" (:version jar)))
Expand Down Expand Up @@ -65,12 +66,21 @@
(:version jar))
(stats/format-stats))]
(html-doc (str (:jar_name jar) " " (:version jar)) {:account account :description (format "%s - %s" (:description jar) (:version jar))
:label1 "Downloads total/this version"
:data1 (format "%s/%s" total-downloads downloads-this-version)
:label2 "Coordinates"
:data2 (format "[%s \"%s\"]" (jar-name jar) (:version jar))}
:label1 "Downloads total/this version"
:data1 (format "%s/%s" total-downloads downloads-this-version)
:label2 "Coordinates"
:data2 (format "[%s \"%s\"]" (jar-name jar) (:version jar))}
(let [pom-map (jar-to-pom-map reporter jar)]
[:div.light-article.row
;; TODO: this could be made more semantic by attaching the metadata to #jar-title, but we're waiting on https://github.com/clojars/clojars-web/issues/482
(structured-data/breadcrumbs (if (group-is-name? jar)
[{:url (str "https://clojars.org/" (jar-name jar))
:name (:jar_name jar)}]
[{:url (str "https://clojars.org/groups/" (:group_name jar))
:name (:group_name jar)}
{:url (str "https://clojars.org/" (jar-name jar)) ;; TODO: Not sure if this is a dirty hack or a stroke of brilliance
:name (:jar_name jar)}]))

(helpers/select-text-script)
[:div#jar-title.col-sm-9.col-lg-9.col-xs-12.col-md-9
[:h1 (jar-link jar)]
Expand Down
33 changes: 31 additions & 2 deletions src/clojars/web/structured_data.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
[clojure.string :as str]))

(def common "Common ld-json attributes"
{"@context" "http://schema.org"
"url" "https://clojars.org"})
{"@context" "http://schema.org"})

(defn ld-json
"Takes a map m, converts it to JSON, and puts it inside
Expand All @@ -21,6 +20,7 @@
(def website
(ld-json
{"@type" "WebSite"
"url" "https://clojars.org"
"name" "Clojars" ;; https://developers.google.com/structured-data/site-name
"sameAs" ["https://twitter.com/clojars"] ;; https://developers.google.com/structured-data/customize/social-profiles
"potentialAction" ;; https://developers.google.com/structured-data/slsb-overview
Expand All @@ -31,6 +31,7 @@
(def organisation
(ld-json
{"@type" "Organization"
"url" "https://clojars.org"
"name" "Clojars"
"logo" "https://clojars.org/images/[email protected]"}))

Expand Down Expand Up @@ -72,3 +73,31 @@
(meta-property "og:title" (:title ctx))
(meta-property "og:description" (:description ctx))
(meta-property "og:image" (or (:image-url ctx) "https://clojars.org/images/[email protected]"))))

(defn breadcrumbs [crumbs]
(println crumbs)
(ld-json
{"@type" "BreadcrumbList"
"itemListElement" (into [] (map-indexed (fn [index crumb]
{"@type" "ListItem"
"position" (inc index)
"item" {"@id" (:url crumb)
"name" (:name crumb)}}))
crumbs)

#_(if (group-is-name? jar)
[{"@type" "ListItem"
"position" 1
"item" {"@id" (str "https://clojars.org/" (jar-name jar))
"name" (:jar_name jar)}}]

[{"@type" "ListItem"
"position" 1
"item" {"@id" (str "https://clojars.org/groups/" (:group_name jar)) ;; Waiting on #482
"name" (:group_name jar)}}
{"@type" "ListItem"
"position" 2
"item" {"@id" (str "https://clojars.org/" (jar-name jar)) ;; TODO: Not sure if this is a dirty hack or a stroke of brilliance
"name" (:jar_name jar)}}])})

)

0 comments on commit aea8653

Please sign in to comment.