Skip to content

Commit 5301e53

Browse files
authored
Merge pull request #8 from neilboyd/highlight
add a function to highlight matched text
2 parents cd7ddd1 + 5c67afc commit 5301e53

20 files changed

+315
-217
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ cypress/screenshots/
99
.DS_Store
1010
.idea
1111
package-lock.json
12-
.jekyll-cache
12+
.jekyll-cache
13+
example/Gemfile.lock

.prettierrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"semi": false
3-
}
2+
"semi": false,
3+
"trailingComma": "none",
4+
"prettier.spaceBeforeFunctionParen": true // I want this because it matches lint, but it doesn't work
5+
}

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Christian Fei
3+
Copyright (c) 2015 Christian Fei, Neil Boyd
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ SimpleJekyllSearch({
198198
})
199199
```
200200

201-
See the [tests](https://github.com/christian-fei/Simple-Jekyll-Search/blob/master/tests/Templater.test.js) for an in-depth code example
201+
See the [tests](tests/Templater.test.js) for an in-depth code example
202202

203203
### sortMiddleware (Function) [optional]
204204

@@ -246,6 +246,24 @@ A function called once the data has been loaded.
246246

247247
Limit how many times the search function can be executed over the given time window. This is especially useful to improve the user experience when searching over a large dataset (either with rare terms or because the number of posts to display is large). If no `debounceTime` (milliseconds) is provided a search will be triggered on each keystroke.
248248

249+
## highlightMatchedText (value, query, snippetLength)
250+
251+
A helper function that might be useful in `templateMiddleware` (or anywhere else).
252+
See [default layout](example/_layouts/default.html) for an example of usage.
253+
254+
### value (String)
255+
256+
The matched text.
257+
258+
### query (String)
259+
260+
The text that was searched for.
261+
262+
### snippetLength (Number) [optional]
263+
264+
The length to trim the text to.
265+
Defaults to 200.
266+
249267
---
250268

251269
## If search isn't working due to invalid JSON

WIKI.md

Lines changed: 0 additions & 182 deletions
This file was deleted.

cypress/integration/simple-jekyll-search.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ describe('Simple Jekyll Search', function () {
3030

3131
cy.contains('No results found')
3232
})
33+
34+
it('Search Turkish highlights correct text', function () {
35+
cy.visit('http://localhost:4000')
36+
37+
cy.get('#search-input')
38+
.type('test')
39+
40+
cy.get('b').contains('test').should('have.length', 1)
41+
cy.get('b').contains('test').should('have.text', 'test')
42+
})
3343
})

dest/simple-jekyll-search.js

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
2-
* Simple-Jekyll-Search 1.12.1
3-
* Copyright 2015-2024, Christian Fei, Neil Boyd
2+
* Simple-Jekyll-Search 1.13.0
3+
* Copyright 2015-2025, Christian Fei, Neil Boyd
44
* Licensed under the MIT License.
55
*/
66

@@ -71,7 +71,7 @@ function FuzzySearchStrategy () {
7171
if (string === null) {
7272
return false
7373
}
74-
return _$fuzzysearch_1(crit.toLowerCase(), string.toLowerCase())
74+
return _$fuzzysearch_1(crit.toUpperCase(), string.toUpperCase())
7575
}
7676
}
7777

@@ -86,8 +86,8 @@ function LiteralSearchStrategy () {
8686
if (!str) {
8787
return false
8888
}
89-
str = str.trim().toLowerCase()
90-
crit = crit.trim().toLowerCase()
89+
str = str.trim().toUpperCase()
90+
crit = crit.trim().toUpperCase()
9191

9292
let critArray = []
9393
if (crit.startsWith('"') && crit.endsWith('"')) {
@@ -284,7 +284,8 @@ var _$OptionsValidator_3 = function OptionsValidator (params) {
284284

285285
var _$utils_9 = {
286286
merge: merge,
287-
isJSON: isJSON
287+
isJSON: isJSON,
288+
highlightMatchedText: highlightMatchedText
288289
}
289290

290291
function merge (defaultParams, mergeParams) {
@@ -309,6 +310,63 @@ function isJSON (json) {
309310
}
310311
}
311312

313+
function highlightMatchedText (value, query, snippetLength = 200) {
314+
// make sure it has a reasonable minimum
315+
snippetLength = Math.max(snippetLength, 20)
316+
const snippetPrefixLength = Math.round(snippetLength / 2)
317+
318+
// for exact search highlight full text, otherwise highlight each word
319+
const results =
320+
query.startsWith('"') && query.endsWith('"')
321+
? [query.substring(1, query.length - 1)]
322+
: query.split(' ')
323+
324+
// highlight each match
325+
results.forEach((result) => {
326+
let j = 0
327+
while (true) {
328+
j = value.toUpperCase().indexOf(result.toUpperCase(), j)
329+
if (j < 0) {
330+
break
331+
}
332+
const k = j + result.length
333+
value =
334+
value.substring(0, j) +
335+
'<b>' +
336+
value.substring(j, k) +
337+
'</b>' +
338+
value.substring(k)
339+
j += 4 // move past the previous match
340+
}
341+
})
342+
343+
// trim start so that match is visible
344+
let s = value.indexOf('<b>')
345+
if (s > snippetPrefixLength) {
346+
s = s - snippetPrefixLength
347+
} else {
348+
s = 0
349+
}
350+
351+
// trim start and end to snippet length
352+
value = value.substring(s, snippetLength)
353+
354+
// if end is a partial tag, or a complete opening tag, then trim it
355+
const t = value.indexOf('<', snippetLength - 3)
356+
if (t !== -1) {
357+
value = value.substring(0, t)
358+
}
359+
360+
// if last opening tag is after last closing tag, then add a new closing tag
361+
const o = value.lastIndexOf('<b>')
362+
const c = value.lastIndexOf('</b>')
363+
if (o > c) {
364+
return value + '</b>'
365+
}
366+
367+
return value
368+
}
369+
312370
var _$src_8 = {};
313371
(function (window) {
314372
'use strict'
@@ -385,6 +443,8 @@ var _$src_8 = {};
385443
return rv
386444
}
387445

446+
window.HighlightMatchedText = _$utils_9.highlightMatchedText
447+
388448
function initWithJSON (json) {
389449
_$Repository_4.put(json)
390450
registerInput()

dest/simple-jekyll-search.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)