0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-17 09:52:29 +01:00

docs search: take into account the current URL.

This commit is contained in:
XhmikosR 2018-10-19 19:43:12 +03:00
parent a16ffc7ba1
commit 282b77ee9c
2 changed files with 17 additions and 4 deletions

View File

@ -1,5 +1,5 @@
<form class="bd-search d-flex align-items-center"> <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 }}" data-docs-version="{{ site.docs_version }}"> <input type="search" class="form-control" id="search-input" placeholder="Search..." aria-label="Search for..." autocomplete="off" 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"> <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" -%} {%- include icons/menu.svg width="30" height="30" -%}
</button> </button>

View File

@ -12,6 +12,19 @@
var inputElement = document.getElementById('search-input') var inputElement = document.getElementById('search-input')
var siteDocsVersion = inputElement.getAttribute('data-docs-version') var siteDocsVersion = inputElement.getAttribute('data-docs-version')
function getOrigin() {
var location = window.location
var origin = location.origin
if (!origin) {
var port = location.port ? ':' + location.port : ''
origin = location.protocol + '//' + location.hostname + port
}
return origin
}
window.docsearch({ window.docsearch({
apiKey: '5990ad008512000bba2cf951ccf0332f', apiKey: '5990ad008512000bba2cf951ccf0332f',
indexName: 'bootstrap', indexName: 'bootstrap',
@ -27,11 +40,11 @@
}, },
transformData: function (hits) { transformData: function (hits) {
return hits.map(function (hit) { return hits.map(function (hit) {
// When in production, return the result as is, var siteurl = getOrigin()
// otherwise remove our url from it.
var siteurl = inputElement.getAttribute('data-siteurl')
var urlRE = /^https?:\/\/getbootstrap\.com/ var urlRE = /^https?:\/\/getbootstrap\.com/
// When in production, return the result as is,
// otherwise remove our url from it.
hit.url = siteurl.match(urlRE) ? hit.url : hit.url.replace(urlRE, '') hit.url = siteurl.match(urlRE) ? hit.url : hit.url.replace(urlRE, '')
return hit return hit