From 83a8174dab81ec018fb45615aef1851e473cd238 Mon Sep 17 00:00:00 2001 From: Takuya Noguchi Date: Mon, 18 Jul 2022 07:12:57 +0000 Subject: [PATCH] Replace middleman-search with lunr with Typesense DocSearch Signed-off-by: Takuya Noguchi --- Gemfile | 1 - Gemfile.lock | 16 --- assets/javascripts/application.js | 4 +- assets/javascripts/search.js | 146 ----------------------- assets/javascripts/search_arrows.js | 93 --------------- assets/javascripts/typesenseDocsearch.js | 11 ++ assets/stylesheets/_search.scss | 42 ------- assets/stylesheets/application.css.scss | 1 - config.rb | 12 -- source/layouts/base.haml | 4 +- 10 files changed, 16 insertions(+), 314 deletions(-) delete mode 100644 assets/javascripts/search.js delete mode 100644 assets/javascripts/search_arrows.js create mode 100644 assets/javascripts/typesenseDocsearch.js diff --git a/Gemfile b/Gemfile index da5c8d2ce60..c1b482da5d7 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,6 @@ gem 'middleman', '~> 4.4' gem 'middleman-syntax' gem 'middleman-blog' gem 'puma', '~> 5.6' -gem 'middleman-search', github: 'deivid-rodriguez/middleman-search', branch: 'workarea-commerce-master' gem 'rake' gem 'ronn' gem 'kramdown' diff --git a/Gemfile.lock b/Gemfile.lock index 0b04f0fa22c..f611e7b568d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,3 @@ -GIT - remote: https://github.com/deivid-rodriguez/middleman-search.git - revision: 50465e1c1580e282a45247b77036da3c2719870d - branch: workarea-commerce-master - specs: - middleman-search (0.10.0) - middleman-core (>= 3.2) - mini_racer (~> 0.5) - nokogiri (~> 1.6) - GEM remote: https://rubygems.org/ specs: @@ -52,9 +42,6 @@ GEM json (2.6.2) kramdown (2.4.0) rexml - libv8-node (16.10.0.0) - libv8-node (16.10.0.0-arm64-darwin) - libv8-node (16.10.0.0-x86_64-linux) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -101,8 +88,6 @@ GEM middleman-core (>= 3.2) rouge (~> 3.2) mini_portile2 (2.8.0) - mini_racer (0.5.0) - libv8-node (~> 16.10.0.0) minitest (5.14.4) mustache (1.0.5) nio4r (2.5.8) @@ -193,7 +178,6 @@ DEPENDENCIES kramdown middleman (~> 4.4) middleman-blog - middleman-search! middleman-syntax nokogiri (~> 1.13) octokit (~> 4.25) diff --git a/assets/javascripts/application.js b/assets/javascripts/application.js index 4e9a2a0524d..1d06ddefbd7 100644 --- a/assets/javascripts/application.js +++ b/assets/javascripts/application.js @@ -1,7 +1,7 @@ -import './search' -import './search_arrows' import AnchorJS from 'anchor-js'; +import "./typesenseDocsearch"; + const anchors = new AnchorJS(); anchors.options = { diff --git a/assets/javascripts/search.js b/assets/javascripts/search.js deleted file mode 100644 index 071aedaddfb..00000000000 --- a/assets/javascripts/search.js +++ /dev/null @@ -1,146 +0,0 @@ -import { Popover } from 'bootstrap'; -import lunr from 'lunr' - -var lunrIndex = null; -var lunrData = null; -var search = null; - -document.addEventListener('DOMContentLoaded', function() { - var oReq = new XMLHttpRequest(); - oReq.open("GET", "/search/lunr-index.json"); - oReq.addEventListener("load", function () { - lunrData = JSON.parse(oReq.response); - lunrIndex = lunr.Index.load(lunrData.index); - search = new Search(); - search.init(); - }); - oReq.send(); -}); - -(function() { - var Search = window.Search = function() { - this.SEARCH_INPUT_ID = '#input-search'; - this.POPOVER_CLASS = '.popover'; - this.POPOVER_OPTIONS = { - html: true, - container: 'body', - placement: 'bottom', - trigger: 'manual', - content: (function() { return search.getPopoverContent() }) - }; - this.LIMIT_RESULTS = 10; - this.popoverContent = ''; - this.popoverHandler = null; - - this.processText = function(text) { - if (!text || text === '') return this.hidePopover(); - - this.showPopover(text); - }; - - this.initializePopover = function() { - this.popover = new Popover(this.searchInput, this.POPOVER_OPTIONS); - }; - - this.hidePopover = function() { - const popover = Popover.getInstance(this.searchInput); - popover && popover.hide(); - this.searchArrows.destroy(); - }; - - this.showPopover = function(text) { - this.popoverContent = this.generatePopoverContent(text); - const popover = Popover.getInstance(this.searchInput); - popover.show(); - document.querySelector(".popover-body").innerHTML = this.popoverContent; - }; - - this.generatePopoverContent = function(text) { - var results = lunrIndex.search(text); - if (!results.length) return 'No results found'; - - results = results.slice(0, this.LIMIT_RESULTS); - var uniqueResults = this.uniqueResults(results); - - var generated = ''; - }; - - this.uniqueResults = function(results) { - var uniqueResults = []; - var titles = []; - [].forEach.call(results, function (el) { - if(titles.indexOf(lunrData.docs[el.ref].title) === -1) { - titles.push(lunrData.docs[el.ref].title); - uniqueResults.push(el); - } - }); - return uniqueResults; - }; - - this.initializePopoverEvents = function() { - var self = this; - - this.searchInput.addEventListener('keydown', function(e) { - if (e.which == 27) return self.hidePopover(); // esc key - }); - this.searchInput.addEventListener('input', function(e) { - var text = this.value; - self.processText(text); - }); - this.searchInput.addEventListener('focus', function(e) { - var text = this.value; - if (text === '') return; - - self.showPopover(text); - }); - this.searchInput.addEventListener('shown.bs.popover', function() { - self.popoverHandler = document.querySelector(self.POPOVER_CLASS); - self.popoverHandler.addEventListener("click", function(e) { e.stopPropagation() }); - self.searchArrows.init(); - }); - this.searchInput.addEventListener("click", function(e) { e.stopPropagation() }); - window.addEventListener("click", self.hidePopover.bind(self)); - }; - }; - - Search.prototype.init = function() { - this.searchInput = document.querySelector(this.SEARCH_INPUT_ID); - this.searchArrows = new SearchArrows(); - this.initializePopoverEvents(); - this.initializePopover(); - }; - - Search.prototype.getPopoverContent = function() { - return this.popoverContent; - }; -})(); diff --git a/assets/javascripts/search_arrows.js b/assets/javascripts/search_arrows.js deleted file mode 100644 index 0284ae6a275..00000000000 --- a/assets/javascripts/search_arrows.js +++ /dev/null @@ -1,93 +0,0 @@ -(function() { - var SearchArrows = window.SearchArrows = function() { - this.LI_SELECTOR = '.search-list-li'; - this.ACTIVE_CLASS = 'active'; - this.active = false; - this.currentSelection = null; - this.selectedLi = null; - - this.onKeydown = function(e) { - if (e.which === 40) { - e.preventDefault(); - this.onDownArrow(); - } else if (e.which === 38) { - e.preventDefault(); - this.onUpArrow(); - } else if (e.which === 13) { - this.onEnter(e); - } - }; - this.onKeydownCb = this.onKeydown.bind(this); - - this.onUpArrow = function() { - this.removeActiveClass(); - - if (this.currentSelection === 0 || this.currentSelection == null) this.markLastItem(); - else { - if (this.currentSelection === 0) this.currentSelection = this.listLength - 1; - else this.currentSelection -= 1; - - this.selectedLi = this.list[this.currentSelection]; - this.selectedLi.classList.add(this.ACTIVE_CLASS); - } - }; - - this.onDownArrow = function() { - this.removeActiveClass(); - - if (this.currentSelection === this.listLength - 1 || this.currentSelection == null) this.markFirstItem(); - else { - if (this.currentSelection === this.listLength - 1) this.currentSelection = 0; - else this.currentSelection += 1; - - this.selectedLi = this.list[this.currentSelection]; - this.selectedLi.classList.add(this.ACTIVE_CLASS); - } - }; - - this.onEnter = function() { - window.location.href = this.selectedLi.querySelector('a').getAttribute('href'); - }; - - this.removeActiveClass = function() { - if (this.selectedLi) this.selectedLi.classList.remove(this.ACTIVE_CLASS); - }; - - this.markFirstItem = function() { - this.selectedLi = this.list[0]; - this.selectedLi.classList.add(this.ACTIVE_CLASS); - this.currentSelection = 0; - }; - - this.markLastItem = function() { - this.selectedLi = this.list[this.list.length - 1]; - this.selectedLi.classList.add(this.ACTIVE_CLASS); - this.currentSelection = this.listLength - 1; - }; - }; - - SearchArrows.prototype.init = function() { - if (this.isActive()) this.destroy(); - - this.list = document.querySelectorAll(this.LI_SELECTOR); - this.listLength = this.list.length; - if (this.listLength === 0) return; - - window.addEventListener('keydown', this.onKeydownCb); - this.active = true; - }; - - SearchArrows.prototype.destroy = function() { - window.removeEventListener('keydown', this.onKeydownCb); - this.active = false; - this.currentSelection = null; - }; - - SearchArrows.prototype.isActive = function() { - return this.active; - }; - - SearchArrows.prototype.isOneOfKeys = function(keyCode) { - return ([40, 38, 13].indexOf(keyCode) !== -1); - } -})(); diff --git a/assets/javascripts/typesenseDocsearch.js b/assets/javascripts/typesenseDocsearch.js new file mode 100644 index 00000000000..b840f78b91d --- /dev/null +++ b/assets/javascripts/typesenseDocsearch.js @@ -0,0 +1,11 @@ +docsearch({ + inputSelector: "#input-search", + typesenseCollectionName: "bundler", + typesenseServerConfig: { + nodes: [{ + host: "ijmv1s078hbwfl9zp-1.a1.typesense.net", + protocol: "https", + }], + apiKey: "p7DMHdxzgPnJD27zDaY42ByNn9IQQxCv", + }, +}); diff --git a/assets/stylesheets/_search.scss b/assets/stylesheets/_search.scss index 75aa7907d32..f952dec4322 100644 --- a/assets/stylesheets/_search.scss +++ b/assets/stylesheets/_search.scss @@ -31,45 +31,3 @@ .input-search:focus { outline: 0; } - -.search-list-ul { - list-style-type: none; - padding-left: 0; - - hr { - margin-top: 0; - margin-bottom: 0; - } - - li:last-child hr { - height: 0; - border-top-color: #ffffff; - } - - h4 { - padding-top: 10px; - margin-bottom: 0; - } - - .active { - border: 2px solid #64C9EF !important; - - transition-property: border-color; - transition-duration: 0.3s; - -webkit-transition-property: border-color; - -webkit-transition-duration: 0.3s; - -o-transition-property: border-color; - -o-transition-duration: 0.3s; - -moz-transition-property: border-color; - -moz-transition-duration: 0.3s; - } - - .search-list-li { - border: 2px solid transparent; - padding: 2px; - } -} - -.popover{ - max-width: 100%; /* Max Width of the popover (depending on the container!) */ -} diff --git a/assets/stylesheets/application.css.scss b/assets/stylesheets/application.css.scss index 17918394d76..2e316403f83 100644 --- a/assets/stylesheets/application.css.scss +++ b/assets/stylesheets/application.css.scss @@ -48,7 +48,6 @@ $icon-font-path: '~bootstrap/assets/fonts/bootstrap/'; @import "~bootstrap/scss/nav"; @import "~bootstrap/scss/navbar"; @import "~bootstrap/scss/card"; -@import "~bootstrap/scss/popover"; // Helpers @import "~bootstrap/scss/helpers"; diff --git a/config.rb b/config.rb index c8a1e7c120b..5b00accf946 100644 --- a/config.rb +++ b/config.rb @@ -5,18 +5,6 @@ activate :syntax activate :i18n -activate :search do |search| - search.resources = ['index.html', 'guides/', "#{config[:current_version]}/", 'compatibility.html', 'conduct.html', 'contributors.html'] - - search.index_path = 'search/lunr-index.json' - - search.fields = { - title: {boost: 100, store: true, required: true}, - content: {boost: 50}, - url: {index: false, store: true}, - description: {index: false, store: true}, - } -end set :markdown_engine, :kramdown diff --git a/source/layouts/base.haml b/source/layouts/base.haml index 8630b7723c9..23f074a7deb 100644 --- a/source/layouts/base.haml +++ b/source/layouts/base.haml @@ -15,8 +15,8 @@ - if content_for?(:canonical) %link(rel="canonical" href="#{yield_content(:canonical)}") - = javascript_include_tag "application.min" = stylesheet_link_tag "application" + = stylesheet_link_tag "https://cdn.jsdelivr.net/npm/typesense-docsearch.js@1.0.0/dist/cdn/docsearch.min.css" = partial 'layouts/favicon' = feed_tag :atom, "#{blog.options.prefix.to_s}/feed.xml", title: "Atom Feed" = yield_content :head @@ -30,3 +30,5 @@ .footer = partial 'layouts/footer' + = javascript_include_tag "https://cdn.jsdelivr.net/npm/typesense-docsearch.js@1.0.0/dist/cdn/docsearch.min.js" + = javascript_include_tag "application.min"