Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
temp/
index.html
.vscode
.vscode
.DS_Store
9 changes: 8 additions & 1 deletion static/canjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ $ = require("jquery");
var debounce = require("lodash/debounce");
require("./canjs.less!");
var SearchControl = require('./search');
var highlightKeywords = require('./highlight-keywords');

// state
var $articleContainer,
Expand Down Expand Up @@ -176,8 +177,14 @@ function navigate(href) {
// Google Analytics
ga('send', 'pageview', window.location.pathname);

var body = content.match(/<body>(\r|\n|.)+<\/body>/g)[0];

if(searchControl.currentSearch){
body = highlightKeywords(body, searchControl.currentSearch.split(' '));
}

// set new content
var $content = $(content.match(/<body>(\r|\n|.)+<\/body>/g)[0]);
var $content = $(body);
if (!$content.length) {
window.location.reload();
}
Expand Down
18 changes: 18 additions & 0 deletions static/highlight-keywords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function highlightKeywords(text, keywords, className){
if(typeof text !== 'string'){
throw 'The first argument must be a string!';
}
if(!Array.isArray(keywords)){
throw 'The second argument must be an array!';
}
// Single quotes needed because the transpiler removes escaping :(
var replaceText = ">$1<span class='"+(className || 'highlighted')+"'>$2</span>$3<";
var textCopy = text;
for(var i = 0; i < keywords.length; i++){
var regex = new RegExp('>([^<]*)?('+keywords[i]+')([^>]*)?<','gmi');
textCopy = textCopy.replace(regex, replaceText);
}
return textCopy;
}

module.exports = highlightKeywords;
14 changes: 12 additions & 2 deletions static/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var $ = require("jquery");
var Control = require("can-control");
var searchResultsRenderer = require("./templates/search-results.stache!steal-stache");
var joinURIs = require("can-util/js/join-uris/");
var highlightKeywords = require('./highlight-keywords');

//https://lunrjs.com/guides/getting_started.html
var searchEngine = require("lunr");
Expand Down Expand Up @@ -369,6 +370,7 @@ var Search = Control.extend({
// esc exits search results
// any other key triggers search
searchTerm: "",
currentSearch: "",
".search keyup": function(el, ev){
var value = ev.target.value;

Expand All @@ -390,6 +392,7 @@ var Search = Control.extend({
default:
if(value !== this.searchTerm){
this.searchTerm = value;
this.currentSearch = value;
this.search(value);
this.showResults();
}
Expand Down Expand Up @@ -493,7 +496,7 @@ var Search = Control.extend({

// function search
// replaces the content in the results element
// with stache rendered data based on given falue
// with stache rendered data based on given value
searchDebounceHandle: 0,
search: function(value){
clearTimeout(this.searchDebounceHandle);
Expand Down Expand Up @@ -522,7 +525,14 @@ var Search = Control.extend({
});

self.$resultsWrap.empty();
self.$resultsWrap[0].appendChild(resultsFrag);

var resultsWrap = self.$resultsWrap[0].appendChild(resultsFrag);

// Inject highlighting spans for search keywords
var $searchResults = self.$resultsWrap.find('.search-results > ul');
$searchResults.html(
highlightKeywords($searchResults.html(), value.split(' '))
);

//refresh necessary dom
self.$resultsList = null;
Expand Down
16 changes: 15 additions & 1 deletion static/search.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//Search
.search-bar {
display:none;
background: @code-color;
padding: @gutter @gutter*2;
height: 48px;
Expand Down Expand Up @@ -151,3 +150,18 @@
position:relative;
}
}

/* Highlight search results */
.highlighted {
position: relative;
display: inline-block;
&:after {
content:"";
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
background-color: hsla(32, 96%, 49%, 0.61);
}
}