on master help sign in
added truncate to given max chars without cutting into the last word
7f3a93cfriederjanmoerman
static/scripts.js+23−2
@@ -112,6 +112,7 @@ function makeTeaser(body, terms) {
112 var NORMAL_WORD_WEIGHT = 2;
112 var NORMAL_WORD_WEIGHT = 2;
113 var FIRST_WORD_WEIGHT = 8;
113 var FIRST_WORD_WEIGHT = 8;
114 var TEASER_MAX_WORDS = 17;
114 var TEASER_MAX_WORDS = 17;
115+ var TEASER_MAX_CHARS = 63;
115
116
116 var stemmedTerms = terms.map(function (w) {
117 var stemmedTerms = terms.map(function (w) {
117 return elasticlunr.stemmer(w.toLowerCase());
118 return elasticlunr.stemmer(w.toLowerCase());
@@ -153,7 +154,16 @@ function makeTeaser(body, terms) {
153 }
154 }
154
155
155 var windowWeights = [];
156 var windowWeights = [];
156− var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS);
157+ var windowSize = 0;
158+
159+ var sumChars = 0;
160+ for (var i = 0; i < weighted.length; i++) {
161+ sumChars += weighted[i][0].length;
162+ if (sumChars <= TEASER_MAX_CHARS) {
163+ windowSize++;
164+ }
165+ }
166+
157 // We add a window with all the weights first
167 // We add a window with all the weights first
158 var curSum = 0;
168 var curSum = 0;
159 for (var i = 0; i < windowSize; i++) {
169 for (var i = 0; i < windowSize; i++) {
@@ -205,10 +215,21 @@ function makeTeaser(body, terms) {
205 return teaser.join("");
215 return teaser.join("");
206}
216}
207
217
218+/*function trimTeaser(teaser) {
219+ var maxLength = 63; // maximum number of characters to extract
220+ var string = teaser;
221+ var trimmedString = teaser.substr(0, maxLength); //trim the string to the maximum length
222+ trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" "))) //re-trim if we are in the middle of a word
223+}*/
224+
225+function trimTeaserTitle() {
226+ //
227+}
228+
208function formatSearchResultItem(item, terms) {
229function formatSearchResultItem(item, terms) {
209 var li = document.createElement("li");
230 var li = document.createElement("li");
210 li.classList.add("search-results__item");
231 li.classList.add("search-results__item");
211− li.innerHTML = `<a href="${item.ref}">${item.doc.title}</a>`;
232+ li.innerHTML = `<a href="${item.ref}">${item.ref}</a>`;
212 li.innerHTML += `<div class="search-results__teaser">${makeTeaser(item.doc.body, terms)}</div>`;
233 li.innerHTML += `<div class="search-results__teaser">${makeTeaser(item.doc.body, terms)}</div>`;
213 return li;
234 return li;
214}
235}