Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Make $searchForm only hold selector string #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions scripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
========================================================================== */

var q, jsonFeedUrl = "/feeds/feed.json",
$searchForm = $("[data-search-form]"),
$searchForm = "[data-search-form]",
$searchInput = $("[data-search-input]"),
$resultTemplate = $("#search-result"),
$resultsPlaceholder = $("[data-search-results]"),
Expand Down Expand Up @@ -151,10 +151,31 @@ function populateResultContent(html, item) {
html = injectContent(html, item.title, '##Title##');
html = injectContent(html, item.link, '##Url##');
html = injectContent(html, item.excerpt, '##Excerpt##');
html = injectContent(html, generateAutoExcerpt(item), '##AutoExcerpt##');
html = injectContent(html, item.date, '##Date##');
return html;
}

/**
* Generate excerpt
* @param {object} item
* @return {String} Generated excerpt
*/
function generateAutoExcerpt(item) {
if (item.excerpt) return item.excerpt;

var preparedContent = item.content.replace(/\s+/g, ' ');
var index = preparedContent.toLowerCase().indexOf(q.toLowerCase());
var start = (index - 150 > 0) ? index - 10 : 0;
var end = index + 150;

var excerpt = preparedContent.substring(start,end);
if (end < preparedContent.length) excerpt += '...';
if (start !== 0) excerpt = '...' + excerpt;

return excerpt;
}


/**
* Populates results string
Expand Down Expand Up @@ -196,4 +217,4 @@ function getParameterByName(name) {
function injectContent(originalContent, injection, placeholder) {
var regex = new RegExp(placeholder, 'g');
return originalContent.replace(regex, injection);
}
}