Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ cypress/screenshots/
.DS_Store
.idea
package-lock.json
.jekyll-cache
.jekyll-cache
example/Gemfile.lock
6 changes: 4 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"semi": false
}
"semi": false,
"trailingComma": "none",
"prettier.spaceBeforeFunctionParen": true // I want this because it matches lint, but it doesn't work
}
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Christian Fei
Copyright (c) 2015 Christian Fei, Neil Boyd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ SimpleJekyllSearch({
})
```

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

### sortMiddleware (Function) [optional]

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

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.

## highlightMatchedText (value, query, snippetLength)

A helper function that might be useful in `templateMiddleware` (or anywhere else).
See [default layout](example/_layouts/default.html) for an example of usage.

### value (String)

The matched text.

### query (String)

The text that was searched for.

### snippetLength (Number) [optional]

The length to trim the text to.
Defaults to 200.

---

## If search isn't working due to invalid JSON
Expand Down
182 changes: 0 additions & 182 deletions WIKI.md

This file was deleted.

10 changes: 10 additions & 0 deletions cypress/integration/simple-jekyll-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ describe('Simple Jekyll Search', function () {

cy.contains('No results found')
})

it('Search Turkish highlights correct text', function () {
cy.visit('http://localhost:4000')

cy.get('#search-input')
.type('test')

cy.get('b').contains('test').should('have.length', 1)
cy.get('b').contains('test').should('have.text', 'test')
})
})
72 changes: 66 additions & 6 deletions dest/simple-jekyll-search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Simple-Jekyll-Search 1.12.1
* Copyright 2015-2024, Christian Fei, Neil Boyd
* Simple-Jekyll-Search 1.13.0
* Copyright 2015-2025, Christian Fei, Neil Boyd
* Licensed under the MIT License.
*/

Expand Down Expand Up @@ -71,7 +71,7 @@ function FuzzySearchStrategy () {
if (string === null) {
return false
}
return _$fuzzysearch_1(crit.toLowerCase(), string.toLowerCase())
return _$fuzzysearch_1(crit.toUpperCase(), string.toUpperCase())
}
}

Expand All @@ -86,8 +86,8 @@ function LiteralSearchStrategy () {
if (!str) {
return false
}
str = str.trim().toLowerCase()
crit = crit.trim().toLowerCase()
str = str.trim().toUpperCase()
crit = crit.trim().toUpperCase()

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

var _$utils_9 = {
merge: merge,
isJSON: isJSON
isJSON: isJSON,
highlightMatchedText: highlightMatchedText
}

function merge (defaultParams, mergeParams) {
Expand All @@ -309,6 +310,63 @@ function isJSON (json) {
}
}

function highlightMatchedText (value, query, snippetLength = 200) {
// make sure it has a reasonable minimum
snippetLength = Math.max(snippetLength, 20)
const snippetPrefixLength = Math.round(snippetLength / 2)

// for exact search highlight full text, otherwise highlight each word
const results =
query.startsWith('"') && query.endsWith('"')
? [query.substring(1, query.length - 1)]
: query.split(' ')

// highlight each match
results.forEach((result) => {
let j = 0
while (true) {
j = value.toUpperCase().indexOf(result.toUpperCase(), j)
if (j < 0) {
break
}
const k = j + result.length
value =
value.substring(0, j) +
'<b>' +
value.substring(j, k) +
'</b>' +
value.substring(k)
j += 4 // move past the previous match
}
})

// trim start so that match is visible
let s = value.indexOf('<b>')
if (s > snippetPrefixLength) {
s = s - snippetPrefixLength
} else {
s = 0
}

// trim start and end to snippet length
value = value.substring(s, snippetLength)

// if end is a partial tag, or a complete opening tag, then trim it
const t = value.indexOf('<', snippetLength - 3)
if (t !== -1) {
value = value.substring(0, t)
}

// if last opening tag is after last closing tag, then add a new closing tag
const o = value.lastIndexOf('<b>')
const c = value.lastIndexOf('</b>')
if (o > c) {
return value + '</b>'
}

return value
}

var _$src_8 = {};
(function (window) {
'use strict'
Expand Down Expand Up @@ -385,6 +443,8 @@ var _$src_8 = {};
return rv
}

window.HighlightMatchedText = _$utils_9.highlightMatchedText

function initWithJSON (json) {
_$Repository_4.put(json)
registerInput()
Expand Down
6 changes: 3 additions & 3 deletions dest/simple-jekyll-search.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading