Skip to content

Commit d24d47c

Browse files
committed
Backport search from v4 and adapt it for v3.4.0.
1 parent ebe7459 commit d24d47c

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

docs/_includes/footer.html

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<script src="{{ site.baseurl }}/dist/js/bootstrap.js"></script>
3030
{% endif %}
3131

32+
{% if page.layout == "default" %}
33+
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
34+
{% endif %}
35+
3236
{% if site.github %}
3337
<script src="{{ site.baseurl }}/assets/js/docs.min.js"></script>
3438
{% else %}
@@ -37,24 +41,6 @@
3741
{% endfor %}
3842
{% endif %}
3943

40-
{% if page.slug == "getting-started" or page.slug == "css" or page.slug == "components" or page.slug == "js" %}
41-
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
42-
<script>
43-
var docsearch = docsearch({
44-
apiKey: 'c6aa63796ef2f94c7e7e7f0b93b4a223',
45-
indexName: 'bootstrap-v3',
46-
inputSelector: '#search-input',
47-
transformData: function (hits) {
48-
return hits.map(function (hit) {
49-
hit.url = hit.url.replace('https://getbootstrap.com/', '');
50-
return hit;
51-
});
52-
},
53-
debug: false // Set debug to true if you want to inspect the dropdown
54-
});
55-
</script>
56-
{% endif %}
57-
5844
{% if page.slug == "customize" %}
5945
<script>var __configBridge = {{ site.data.configBridge.config | jsonify }}</script>
6046
{% if site.github %}

docs/_includes/header.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{% endif %}
2929
{% endif %}
3030

31-
{% if page.slug == "getting-started" or page.slug == "css" or page.slug == "components" or page.slug == "js" %}
31+
{% if page.layout == "default" %}
3232
<link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css">
3333
{% endif %}
3434

docs/assets/js/src/search.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
2+
// IT'S ALL JUST JUNK FOR OUR DOCS!
3+
// ++++++++++++++++++++++++++++++++++++++++++
4+
5+
(function () {
6+
'use strict'
7+
8+
var inputElement = document.getElementById('search-input')
9+
10+
if (!window.docsearch || !inputElement) {
11+
return
12+
}
13+
14+
function getOrigin() {
15+
var location = window.location
16+
var origin = location.origin
17+
18+
if (!origin) {
19+
var port = location.port ? ':' + location.port : ''
20+
21+
origin = location.protocol + '//' + location.hostname + port
22+
}
23+
24+
return origin
25+
}
26+
27+
window.docsearch({
28+
apiKey: 'c8948afa20e6437a6e829f7e87b9ac11',
29+
indexName: 'bootstrap-v3',
30+
inputSelector: '#search-input',
31+
handleSelected: function (input, event, suggestion) {
32+
var url = suggestion.url
33+
url = suggestion.isLvl1 ? url.split('#')[0] : url
34+
// If it's a title we remove the anchor so it does not jump.
35+
window.location.href = url
36+
},
37+
transformData: function (hits) {
38+
return hits.map(function (hit) {
39+
var siteurl = getOrigin()
40+
var urlRE = /^https?:\/\/getbootstrap\.com/
41+
42+
// When in production, return the result as is,
43+
// otherwise remove our url from it.
44+
hit.url = siteurl.match(urlRE) ? hit.url : hit.url.replace(urlRE, '')
45+
46+
return hit
47+
})
48+
},
49+
debug: false // Set debug to true if you want to inspect the dropdown
50+
})
51+
}())

grunt/configBridge.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"/docs/3.4/assets/js/vendor/holder.min.js",
1515
"/docs/3.4/assets/js/vendor/clipboard.min.js",
1616
"/docs/3.4/assets/js/vendor/anchor.min.js",
17-
"/docs/3.4/assets/js/src/application.js"
17+
"/docs/3.4/assets/js/src/application.js",
18+
"/docs/3.4/assets/js/src/search.js"
1819
]
1920
},
2021
"config": {

0 commit comments

Comments
 (0)