From 30cb1a2715fc1f920fd225ce5f7452e77fc819f8 Mon Sep 17 00:00:00 2001
From: XhmikosR <xhmikosr@gmail.com>
Date: Mon, 9 Apr 2018 23:53:45 +0300
Subject: [PATCH] Pass docs version to search form and switch to the new index.

Also, move the search code to a separate file.
---
 README.md                    |  2 +-
 _includes/docs-sidebar.html  |  2 +-
 _includes/scripts.html       |  1 +
 assets/js/src/application.js | 28 --------------------------
 assets/js/src/search.js      | 38 ++++++++++++++++++++++++++++++++++++
 5 files changed, 41 insertions(+), 30 deletions(-)
 create mode 100644 assets/js/src/search.js

diff --git a/README.md b/README.md
index c7f6024cf6..2bd9336899 100644
--- a/README.md
+++ b/README.md
@@ -105,7 +105,7 @@ Have a bug or a feature request? Please first read the [issue guidelines](https:
 
 Bootstrap's documentation, included in this repo in the root directory, is built with [Jekyll](https://jekyllrb.com/) and publicly hosted on GitHub Pages at <https://getbootstrap.com/>. The docs may also be run locally.
 
-Documentation search is powered by [Algolia's DocSearch](https://community.algolia.com/docsearch/). Working on our search? Be sure to set `debug: true` in `assets/js/application.js` file.
+Documentation search is powered by [Algolia's DocSearch](https://community.algolia.com/docsearch/). Working on our search? Be sure to set `debug: true` in `assets/js/src/search.js` file.
 
 ### Running documentation locally
 
diff --git a/_includes/docs-sidebar.html b/_includes/docs-sidebar.html
index c0a0982bcc..61edd8ec11 100644
--- a/_includes/docs-sidebar.html
+++ b/_includes/docs-sidebar.html
@@ -1,5 +1,5 @@
 <form class="bd-search d-flex align-items-center">
-  <input type="search" class="form-control" id="search-input" placeholder="Search..." aria-label="Search for..." autocomplete="off" data-siteurl="{{ site.url }}">
+  <input type="search" class="form-control" id="search-input" placeholder="Search..." aria-label="Search for..." autocomplete="off" data-siteurl="{{ site.url }}" data-docs-version="{{ site.docs_version }}">
   <button class="btn btn-link bd-search-docs-toggle d-md-none p-0 ml-3" type="button" data-toggle="collapse" data-target="#bd-docs-nav" aria-controls="bd-docs-nav" aria-expanded="false" aria-label="Toggle docs navigation">
     {%- include icons/menu.svg width="30" height="30" -%}
   </button>
diff --git a/_includes/scripts.html b/_includes/scripts.html
index 7c262149f2..37c04f9833 100644
--- a/_includes/scripts.html
+++ b/_includes/scripts.html
@@ -20,6 +20,7 @@
   <script src="{{ site.baseurl }}/assets/js/vendor/clipboard.min.js"></script>
   <script src="{{ site.baseurl }}/assets/js/vendor/holder.min.js"></script>
   <script src="{{ site.baseurl }}/assets/js/src/application.js"></script>
+  <script src="{{ site.baseurl }}/assets/js/src/search.js"></script>
   <script src="{{ site.baseurl }}/assets/js/src/ie-emulation-modes-warning.js"></script>
   <script src="{{ site.baseurl }}/assets/js/src/pwa.js"></script>
 {%- endif -%}
diff --git a/assets/js/src/application.js b/assets/js/src/application.js
index 1f1af444c9..7666da065d 100644
--- a/assets/js/src/application.js
+++ b/assets/js/src/application.js
@@ -101,34 +101,6 @@
     anchors.add('.bd-content > h2, .bd-content > h3, .bd-content > h4, .bd-content > h5')
     $('.bd-content > h2, .bd-content > h3, .bd-content > h4, .bd-content > h5').wrapInner('<div></div>')
 
-    // Search
-    if (window.docsearch) {
-      window.docsearch({
-        apiKey: '48cb48b22351bc71ea5f12f4d1ede198',
-        indexName: 'bootstrap-v4',
-        inputSelector: '#search-input',
-        handleSelected: function (input, event, suggestion) {
-          var url = suggestion.url
-          url = suggestion.isLvl1 ? url.split('#')[0] : url
-          // If it's a title we remove the anchor so it does not jump.
-          window.location.href = url
-        },
-        transformData: function (hits) {
-          return hits.map(function (hit) {
-            // When in production, return the result as is,
-            // otherwise remove our url from it.
-            var siteurl = document.getElementById('search-input').getAttribute('data-siteurl')
-            var urlRE = /^https?:\/\/getbootstrap\.com/
-
-            hit.url = siteurl.match(urlRE) ? hit.url : hit.url.replace(urlRE, '')
-
-            return hit
-          })
-        },
-        debug: false // Set debug to true if you want to inspect the dropdown
-      })
-    }
-
     // Holder
     Holder.addTheme('gray', {
       bg: '#777',
diff --git a/assets/js/src/search.js b/assets/js/src/search.js
new file mode 100644
index 0000000000..3acd71c6f7
--- /dev/null
+++ b/assets/js/src/search.js
@@ -0,0 +1,38 @@
+(function () {
+  'use strict'
+
+  if (!window.docsearch) {
+    return
+  }
+
+  var inputElement = document.getElementById('search-input')
+  var siteDocsVersion = inputElement.getAttribute('data-docs-version')
+
+  window.docsearch({
+    apiKey: '5990ad008512000bba2cf951ccf0332f',
+    indexName: 'bootstrap',
+    inputSelector: '#search-input',
+    algoliaOptions: {
+      facetFilters: ['version:' + siteDocsVersion]
+    },
+    handleSelected: function (input, event, suggestion) {
+      var url = suggestion.url
+      url = suggestion.isLvl1 ? url.split('#')[0] : url
+      // If it's a title we remove the anchor so it does not jump.
+      window.location.href = url
+    },
+    transformData: function (hits) {
+      return hits.map(function (hit) {
+        // When in production, return the result as is,
+        // otherwise remove our url from it.
+        var siteurl = inputElement.getAttribute('data-siteurl')
+        var urlRE = /^https?:\/\/getbootstrap\.com/
+
+        hit.url = siteurl.match(urlRE) ? hit.url : hit.url.replace(urlRE, '')
+
+        return hit
+      })
+    },
+    debug: false // Set debug to true if you want to inspect the dropdown
+  })
+}())
-- 
GitLab