Skip to content

Commit

Permalink
str(saerch): bring up
Browse files Browse the repository at this point in the history
  • Loading branch information
neoFelhz committed Feb 20, 2018
1 parent d9db013 commit 48b6a3a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
118 changes: 118 additions & 0 deletions layout/_pages/search.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<div class="container container-search">
<div class="input-group">
<input type="text" class="form-input input-lg" placeholder="site name">
<button class="btn btn-primary input-group-btn btn-lg">Submit</button>
</div>
</div>

<!--<script type="text/javascript" id="search-local-js-script">
var searchFunc = function(path, search_id, content_id) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.timeout = 4000;
xhr.send();
xhr.onload = function() {
if(this.status == 200||this.status == 304){
var d = document;
var s = d.createElement('script');
s.src = '//<%= theme.comment.shortname %>.disqus.com/embed.js';
s.setAttribute('data-timestamp', + new Date());
(d.head || d.body).appendChild(s);
document.querySelector(".disqus_click_btn").setAttribute("style", "display:none")
}
};
$.ajax({
url: path,
dataType: 'xml',
success: function( xmlResponse ) {
// get the contents from search data
var datas = $( 'entry', xmlResponse ).map(function() {
return {
title: $( 'title', this ).text(),
content: $('content',this).text(),
url: $( 'url' , this).text()
};
}).get();
var $input = document.getElementById(search_id);
var $resultContent = document.getElementById(content_id);
$input.addEventListener('input', function() {
var str='<ul class=\"search-result-list\">';
var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
$resultContent.innerHTML = '';
if (this.value.trim().length <= 0) {
return;
}
// perform local searching
datas.forEach(function(data) {
var isMatch = true;
var content_index = [];
var data_title = data.title.trim().toLowerCase();
var data_content = data.content.trim().replace(/<[^>]+>/g,'').toLowerCase();
var data_url = data.url;
var index_title = -1;
var index_content = -1;
var first_occur = -1;
// only match artiles with not empty titles and contents
if(data_title !== '' && data_content !== '') {
keywords.forEach(function(keyword, i) {
index_title = data_title.indexOf(keyword);
index_content = data_content.indexOf(keyword);
if( index_title < 0 && index_content < 0 ) {
isMatch = false;
} else {
if (index_content < 0) {
index_content = 0;
}
if (i === 0) {
first_occur = index_content;
}
}
});
}
// show search results
if (isMatch) {
str += '<li><a href="'+ data_url +'" class="search-result-title" target="_blank">'+ data_title;
var content = data.content.trim().replace(/<[^>]+>/g, '');
if (first_occur >= 0) {
// cut out characters
var start = first_occur - 6;
var end = first_occur + 6;
if (start < 0) {
start = 0;
}
if (start === 0) {
end = 10;
}
if (end > content.length) {
end = content.length;
}
var match_content = content.substr(start, end);
// highlight all keywords
keywords.forEach(function(keyword) {
var regS = new RegExp(keyword, 'gi');
match_content = match_content.replace(regS, '<em class="search-keyword">'+keyword+'</em>');
})
str += '<p class="search-result">' + match_content + '...</p>' +'</a>';
}
}
});
$resultContent.innerHTML = str;
});
}
});
}
</script>
<script type="text/javascript" id="search-input">
var inputArea = document.querySelector('#search');
var getSearchFile = function() {
var path = '<%= config.search.path %>';
searchFunc(path, 'search', 'local-search-result');
}
if(inputArea) {
inputArea.onfocus = function() {
getSearchFile();
}
}
</script>-->
2 changes: 1 addition & 1 deletion layout/_partial/sidebar/navigation.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- Search -->
<% if(theme.search.use) { %>
<li class="nav-item">
<a href="<%= config.root %>">
<a href="<%= theme.sidebar.search.link %>">
<% if(theme.sidebar.search.icon) { %>
<i class="icon material-icons sidebar-icons"><%= theme.sidebar.search.icon %></i>
<% } %>
Expand Down
3 changes: 3 additions & 0 deletions layout/post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<% } else if(page.layout === 'timeline') { %>
<!-- Single Timeline Page -->
<%- partial('_pages/timeline') %>
<% } else if(page.layout === 'search') { %>
<!-- Local Search Page -->
<%- partial('_pages/search') %>
<% } else { %>
<!-- Post Moudule -->
<%- partial('_pages/post') %>
Expand Down

0 comments on commit 48b6a3a

Please sign in to comment.