From 5d3c58406459ed4752a5124cee3b7e94b8625d77 Mon Sep 17 00:00:00 2001 From: Nathan Arthur Date: Mon, 28 Mar 2016 07:46:16 -0500 Subject: [PATCH 1/3] Make $searchForm only hold selector string [`.on()`](http://api.jquery.com/on/) takes selector string, not list of objects. --- scripts/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/search.js b/scripts/search.js index 8fe1d10..51ad159 100644 --- a/scripts/search.js +++ b/scripts/search.js @@ -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]"), @@ -196,4 +196,4 @@ function getParameterByName(name) { function injectContent(originalContent, injection, placeholder) { var regex = new RegExp(placeholder, 'g'); return originalContent.replace(regex, injection); -} \ No newline at end of file +} From 7d0bb14e991018c621b4470e5fa8e5e1d369a19b Mon Sep 17 00:00:00 2001 From: Nathan Arthur Date: Mon, 28 Mar 2016 08:59:28 -0500 Subject: [PATCH 2/3] Add `##AutoExcerpt##` template option Outputs excerpt if provided, generates its own if not. --- scripts/search.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/scripts/search.js b/scripts/search.js index 51ad159..0b8d572 100644 --- a/scripts/search.js +++ b/scripts/search.js @@ -151,10 +151,32 @@ 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; + console.log(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 From 58573c741a8ba9713d5544fc8612cac36c359bfa Mon Sep 17 00:00:00 2001 From: Nathan Arthur Date: Mon, 28 Mar 2016 09:02:27 -0500 Subject: [PATCH 3/3] Remove test console.log --- scripts/search.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/search.js b/scripts/search.js index 0b8d572..68f53c1 100644 --- a/scripts/search.js +++ b/scripts/search.js @@ -163,7 +163,6 @@ function populateResultContent(html, item) { */ function generateAutoExcerpt(item) { if (item.excerpt) return item.excerpt; - console.log(item.excerpt); var preparedContent = item.content.replace(/\s+/g, ' '); var index = preparedContent.toLowerCase().indexOf(q.toLowerCase());