From a050631d2e09f2c05899272c49fda12930a14e31 Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Mon, 12 Feb 2024 19:00:14 -0600 Subject: [PATCH] Add slugify support for advanced search form js References: https://todo.code.netlandish.com/~netlandish/links/47 --- core/routes.go | 6 ++++-- static/js/advancedsearch.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/routes.go b/core/routes.go index 45b3d96..105a2b6 100644 --- a/core/routes.go +++ b/core/routes.go @@ -1171,6 +1171,7 @@ func (s *Service) PopularLinkList(c echo.Context) error { "links": links, "isPopular": true, "navFlag": "popular", + "tagFilter": "", } if search != "" { gmap["search"] = search @@ -1241,7 +1242,7 @@ func (s *Service) OrgLinksList(c echo.Context) error { } }`) - var isOrgLink bool + var isOrgLink, advancedSearch bool var navFlag string org := &models.Organization{} if c.Path() != c.Echo().Reverse(s.RouteName("recent_link_list")) { @@ -1266,6 +1267,7 @@ func (s *Service) OrgLinksList(c echo.Context) error { org = orgs[0] isOrgLink = true navFlag = "bookmarks" + advancedSearch = true } else { navFlag = "recent" } @@ -1368,7 +1370,7 @@ func (s *Service) OrgLinksList(c echo.Context) error { "tagFilter": strings.Replace(tag, ",", ", ", -1), "excludeTagFilter": strings.Replace(excludeTag, ",", ", ", -1), "navFlag": navFlag, - "advancedSearch": true, + "advancedSearch": advancedSearch, "hasUnreadFilter": hasUnreadFilter, "hasStarredFilter": hasStarredFilter, "hasAllFilter": hasAllFilter, diff --git a/static/js/advancedsearch.js b/static/js/advancedsearch.js index d752c9d..124f345 100644 --- a/static/js/advancedsearch.js +++ b/static/js/advancedsearch.js @@ -5,6 +5,16 @@ var form = document.getElementById("advanced-search"); var btn = document.getElementById("advanced-search-btn") var tagInputDiv = document.getElementById("advanced-search-tags") +function slugify(s) { + if (s === ", ") { + return "" + } + var re = /[^a-z0-9]+/g; + s = s.toLowerCase().trim(); + s = s.replace(re, "-"); + return s.replace(/^-+|-+$/g, ""); +} + function autocomplete() { tags = this.value.split(",") tag = tags[tags.length -1].trim() @@ -72,7 +82,7 @@ form.addEventListener("submit", function(e) { if (tagValue === "") { form.elements.tag.disabled = true; } else { - tagValue = tagValue.split(", ").join(","); + tagValue = tagValue.split(", ").map(slugify).join(","); if (tagValue.endsWith(",")) { tagValue = tagValue.slice(0, -1); } -- 2.45.2