From fcc03c347499a1321aec1a4b39ebe22b51f35e5e Mon Sep 17 00:00:00 2001 From: Math Costa Date: Thu, 16 Aug 2018 15:47:07 -0300 Subject: [PATCH] Bing search for China users (#9845) * Updates readme's site building instructions * Alters dockerfile to use extended hugo build This is necessary for the pipes used w/ TOCSS since node-sass was dropped * WIP for bing search implementation * Implements Bing search instead of google * Makes bing the default result provider and uses google and it's available * Adds pagination to bing and replaces the $.get call with a $.ajax call * front adjust * bing limit pagination * Adds break if there are less than 10 pages worth of results on bing search * Adds proper google script ref and proper config id * Removes console.log call --- assets/sass/_base.sass | 34 +++++++++++++++++++ layouts/docs/search.html | 21 +++--------- static/js/search.js | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 static/js/search.js diff --git a/assets/sass/_base.sass b/assets/sass/_base.sass index 3829ee38b1..8dd57375d0 100644 --- a/assets/sass/_base.sass +++ b/assets/sass/_base.sass @@ -1775,3 +1775,37 @@ $feature-box-div-margin-bottom: 40px .gsc-above-wrapper-area border-bottom: 0; + + + +/* Bing Search */ +#bing-results-container + margin-top: 30px + margin-left: 20px + +.bing-result + margin-bottom: 20px + +.bing-result-name a + font-size: 16px + color: #0000CC + + +.bing-result-url + color: #008000 + font-size: 13px + +.bing-result-snippet + color: #000 + font-size: 11px + +#bing-pagination-container + margin: 10px + margin-left: 20px + +.bing-page-anchor + text-decoration: none!important + cursor: pointer + color: #0000CC + margin-right: 8px + diff --git a/layouts/docs/search.html b/layouts/docs/search.html index 162e9ee1f5..15e59a795b 100644 --- a/layouts/docs/search.html +++ b/layouts/docs/search.html @@ -1,23 +1,12 @@ {{ define "content" }}

{{ .Title }}

- - + {{ end }} diff --git a/static/js/search.js b/static/js/search.js new file mode 100644 index 0000000000..760dcfe363 --- /dev/null +++ b/static/js/search.js @@ -0,0 +1,73 @@ + window.renderGoogleSearchResults = () => { + var cx = '013288817511911618469:elfqqbqldzg'; + var gcse = document.createElement('script'); + gcse.type = 'text/javascript'; + gcse.async = true; + gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//cse.google.com/cse.js?cx=' + cx; + var s = document.getElementsByTagName('script')[0]; + s.parentNode.insertBefore(gcse, s); + // document.querySelector('html').classList.add('search'); + } + + window.getPaginationAnchors = (pages) => { + var pageAnchors = '', searchTerm = window.location.search.split("=")[1].split("&")[0]; + var currentPage = window.location.search.split("=")[2]; + currentPage = (!currentPage) ? 1 : currentPage.split("&")[0]; + + for(var i = 1; i <= 10; i++){ + if(i > pages) break; + pageAnchors += ''; + pageAnchors += (currentPage == i) ? ''+i+'' : i; + pageAnchors += ''; + } + return pageAnchors; + } + + window.getResultMarkupString = (ob) => { + return '
' + + '' + + '
'+ob.displayUrl+'
' + + '
'+ob.snippet+'
' + +'
'; + } + + window.renderBingSearchResults = () => { + var searchTerm = window.location.search.split("=")[1].split("&")[0], + page = window.location.search.split("=")[2], + q = "site:kubernetes.io " + searchTerm; + + page = (!page) ? 1 : page.split("&")[0]; + + var results = '', pagination = '', offset = (page - 1) * 10, ajaxConf = {}; + + ajaxConf.url = 'https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search'; + ajaxConf.data = { q: q, offset: offset, customConfig: '320659264' }; + ajaxConf.type = "GET"; + ajaxConf.beforeSend = function(xhr){ xhr.setRequestHeader('Ocp-Apim-Subscription-Key', '51efd23677624e04b4abe921225ea7ec'); }; + + $.ajax(ajaxConf).done(function(res) { + var paginationAnchors = window.getPaginationAnchors(Math.ceil(res.webPages.totalEstimatedMatches / 10)); + res.webPages.value.map(ob => { results += window.getResultMarkupString(ob); }) + + if($('#bing-results-container').length > 0) $('#bing-results-container').html(results); + if($('#bing-pagination-container').length > 0) $('#bing-pagination-container').html(paginationAnchors); + }); + document.querySelector('html').classList.add('search'); + } + + window.renderBingSearchResults(); + + $.ajax({ + url: "https://ipinfo.io", + dataType: "jsonp", + success: function (response) { + if (response.country != 'CN') window.renderGoogleSearchResults(); + }, + error: function () { + console.log('sorry...') + }, + timeout: 5000 + }); + + +