| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1d3fed5 commit f065800
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,10 +7,14 @@ | |||
| 7 | 7 | /vendor/bundle/ | |
| 8 | 8 | /node_modules/ | |
| 9 | 9 | ||
| 10 | + # bower brings in *a lot* of files | ||
| 11 | + /assets/vendor/lunr.js/* | ||
| 12 | + !/assets/vendor/lunr.js/lunr.js | ||
| 13 | + !/assets/vendor/lunr.js/lunr.min.js | ||
| 14 | + | ||
| 10 | 15 | output | |
| 11 | 16 | tmp | |
| 12 | 17 | .DS_Store | |
| 13 | 18 | .bundle | |
| 14 | 19 | crash.log | |
| 15 | 20 | npm-debug.log | |
| 16 | - static/search-index.json | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,6 @@ ruby '2.2.3' | |||
| 3 | 3 | ||
| 4 | 4 | gem 'nanoc', '~> 4.0' | |
| 5 | 5 | gem 'nanoc-conref-fs', '~> 0.5' | |
| 6 | - gem 'nokogiri', '~> 1.6.0' | ||
| 7 | 6 | ||
| 8 | 7 | # rendering | |
| 9 | 8 | gem 'nanoc-html-pipeline', '0.3.3' | |
@@ -17,6 +16,7 @@ gem 'page-toc-filter', '~> 0.0.1' | |||
| 17 | 16 | gem 'builder', '~> 3.2' | |
| 18 | 17 | ||
| 19 | 18 | group :development do | |
| 19 | + gem 'nokogiri', '~> 1.6.0' | ||
| 20 | 20 | gem 'rake', '10.3.2' | |
| 21 | 21 | gem 'awesome_print', '1.6.1' | |
| 22 | 22 | end | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,9 +5,7 @@ | |||
| 5 | 5 | # * The order of rules is important: for each item, only the first matching | |
| 6 | 6 | # rule is applied. | |
| 7 | 7 | ||
| 8 | - # Reset search-index by deleting it every time | ||
| 9 | 8 | preprocess do | |
| 10 | - File.delete("output/search-index.json") if File.exists?("output/search-index.json") | ||
| 11 | 9 | create_individual_blog_pages | |
| 12 | 10 | generate_redirects(config[:redirects]) | |
| 13 | 11 | end | |
@@ -25,7 +23,6 @@ compile '/integrations-directory/*' do | |||
| 25 | 23 | end | |
| 26 | 24 | ||
| 27 | 25 | compile '/v3{.*,/**/*}' do | |
| 28 | - filter :search | ||
| 29 | 26 | filter :erb | |
| 30 | 27 | filter :html_pipeline, @config[:pipeline_config] | |
| 31 | 28 | filter :enterprise_only_filter | |
@@ -52,6 +49,10 @@ compile '/webhooks/**/*' do | |||
| 52 | 49 | layout(item[:layout] ? "/#{item[:layout]}.*" : '/webhooks.*') | |
| 53 | 50 | end | |
| 54 | 51 | ||
| 52 | + compile '/search/search-index.json' do | ||
| 53 | + filter :erb | ||
| 54 | + end | ||
| 55 | + | ||
| 55 | 56 | compile '/**/*' do | |
| 56 | 57 | filter :erb | |
| 57 | 58 | filter :html_pipeline, @config[:pipeline_config] | |
@@ -62,6 +63,10 @@ route '/changes.atom' do | |||
| 62 | 63 | '/changes.atom' | |
| 63 | 64 | end | |
| 64 | 65 | ||
| 66 | + route '/search/search-index.json' do | ||
| 67 | + item.identifier.to_s | ||
| 68 | + end | ||
| 69 | + | ||
| 65 | 70 | route '/**/index.*' do | |
| 66 | 71 | item.identifier.without_ext + '.html' | |
| 67 | 72 | end | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | $(function() { | |
| 2 | 2 | var searchIndex, | |
| 3 | - searchHits; | ||
| 3 | + searchHits, | ||
| 4 | + searchWorker = new Worker("/assets/javascripts/search_worker.js"); | ||
| 4 | 5 | ||
| 5 | 6 | $('.help-search .search-box').focus(function(){ | |
| 6 | 7 | $(this).css('background-position','0px -25px') | |
@@ -17,15 +18,22 @@ $(function() { | |||
| 17 | 18 | if (localStorage['searchIndex']) { | |
| 18 | 19 | searchIndex = JSON.parse(localStorage['searchIndex']); | |
| 19 | 20 | ||
| 20 | - if (localStorageHasExpired()) | ||
| 21 | + if (localStorageHasExpired()) { | ||
| 21 | 22 | loadSearchIndex(); | |
| 23 | + } | ||
| 24 | + else { | ||
| 25 | + searchIndex.type = "index"; | ||
| 26 | + searchWorker.postMessage(searchIndex); | ||
| 27 | + } | ||
| 22 | 28 | } else { | |
| 23 | 29 | loadSearchIndex(); | |
| 24 | 30 | } | |
| 25 | 31 | ||
| 26 | 32 | function loadSearchIndex() { | |
| 27 | - $.getJSON('/assets/search-index.json', function(data) { | ||
| 28 | - searchIndex = data["pages"]; | ||
| 33 | + $.getJSON('/search/search-index.json', function(data) { | ||
| 34 | + data.type = { index: true }; | ||
| 35 | + searchWorker.postMessage(data); | ||
| 36 | + searchIndex = data; | ||
| 29 | 37 | localStorage['searchIndex'] = JSON.stringify(searchIndex); | |
| 30 | 38 | localStorage['updated'] = new Date().getTime(); | |
| 31 | 39 | }); | |
@@ -118,22 +126,17 @@ $(function() { | |||
| 118 | 126 | function searchForString(searchString) { | |
| 119 | 127 | searchHits = []; | |
| 120 | 128 | searchString = searchString.toLowerCase(); | |
| 129 | + searchWorker.postMessage({ query: searchString, type: "search" }) | ||
| 130 | + } | ||
| 121 | 131 | ||
| 122 | - // Search for string in all pages | ||
| 123 | - for (var i = 0; i < searchIndex.length; i++) { | ||
| 124 | - var page = searchIndex[i]; | ||
| 125 | - | ||
| 126 | - // Add the page to the array of hits if there's a match | ||
| 127 | - if (page.title.toLowerCase().indexOf(searchString) !== -1) { | ||
| 128 | - searchHits.push(page); | ||
| 129 | - } | ||
| 132 | + searchWorker.addEventListener("message", function (e) { | ||
| 133 | + if (e.data.type.search) { | ||
| 134 | + renderResultsForSearch(e.data.query, e.data.results); | ||
| 130 | 135 | } | |
| 131 | - | ||
| 132 | - renderResultsForSearch(searchString); | ||
| 133 | - } | ||
| 136 | + }); | ||
| 134 | 137 | ||
| 135 | 138 | // Update the UI representation of the search hits | |
| 136 | - function renderResultsForSearch(searchString){ | ||
| 139 | + function renderResultsForSearch(searchString, searchHits){ | ||
| 137 | 140 | $("#search-results").empty(); | |
| 138 | 141 | ||
| 139 | 142 | // Check if there are any results. If not, show placeholder and exit | |
@@ -146,7 +149,7 @@ $(function() { | |||
| 146 | 149 | for (var i = 0; i < Math.min(searchHits.length, 8); i++) { | |
| 147 | 150 | var page = searchHits[i]; | |
| 148 | 151 | ||
| 149 | - $('<li class="result"><a href="' + page.url + '"><em>' + page.title + '</em><small>' + page.section + '</small></a></li>').appendTo("#search-results"); | ||
| 152 | + $('<li class="result"><a href="' + page.url + '"><em>' + page.title + '</em></a></li>').appendTo("#search-results"); | ||
| 150 | 153 | } | |
| 151 | 154 | ||
| 152 | 155 | // Select the first alternative | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,58 @@ | |||
| 1 | + importScripts('lunr.min.js'); | ||
| 2 | + | ||
| 3 | + // create lunr.js search index specifying that we want to index the title and body fields of documents. | ||
| 4 | + var lunr_index = lunr(function() { | ||
| 5 | + this.field('title', { boost: 10 }); | ||
| 6 | + this.field('body'); | ||
| 7 | + this.ref('id'); | ||
| 8 | + }), | ||
| 9 | + entries; | ||
| 10 | + | ||
| 11 | + onmessage = function (oEvent) { | ||
| 12 | + | ||
| 13 | + populateIndex = function(data) { | ||
| 14 | + // format the raw json into a form that is simpler to work with | ||
| 15 | + this.entries = data.map(this.createEntry).filter(function(n){ return n !== undefined }); | ||
| 16 | + this.entries.forEach(function(entry) { | ||
| 17 | + if (entry !== null) | ||
| 18 | + this.lunr_index.add(entry); | ||
| 19 | + }); | ||
| 20 | + | ||
| 21 | + postMessage({type: {indexed: true}}); | ||
| 22 | + }; | ||
| 23 | + | ||
| 24 | + decodeHtmlEntity = function(str) { | ||
| 25 | + return str.replace(/&#(\d+);/g, function(match, dec) { | ||
| 26 | + return String.fromCharCode(dec); | ||
| 27 | + }); | ||
| 28 | + }; | ||
| 29 | + | ||
| 30 | + createEntry = function(entry, entry_id) { | ||
| 31 | + if (entry.title === undefined) | ||
| 32 | + return undefined; | ||
| 33 | + entry.id = entry_id + 1; | ||
| 34 | + entry.title = decodeHtmlEntity(entry.title); | ||
| 35 | + return entry; | ||
| 36 | + }; | ||
| 37 | + | ||
| 38 | + search = function(data) { | ||
| 39 | + var entries = this.entries; | ||
| 40 | + | ||
| 41 | + var results = lunr_index | ||
| 42 | + .search(data.query) | ||
| 43 | + .map(function(result) { | ||
| 44 | + return entries.filter(function(entry) { return entry.id === parseInt(result.ref, 10); })[0]; | ||
| 45 | + }) | ||
| 46 | + .filter(function (result) { | ||
| 47 | + return typeof result !== 'undefined'; | ||
| 48 | + }); | ||
| 49 | + | ||
| 50 | + postMessage({ query: data.query, results: results, type: { search: true } }); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + // if we're asked to index, index! else, search | ||
| 54 | + if (oEvent.data.type == "index") | ||
| 55 | + populateIndex(oEvent.data); | ||
| 56 | + else | ||
| 57 | + search(oEvent.data); | ||
| 58 | + }; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments