Skip to content

Commit

Permalink
Merge pull request #4 from neilboyd/add-query-to-middleware
Browse files Browse the repository at this point in the history
add query to middleware
  • Loading branch information
neilboyd authored Jul 24, 2024
2 parents f9bc704 + 380b101 commit 9746038
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ If the `search.json` contains this data

A function that will be called whenever a match in the template is found.

It gets passed the current property name, property value, and the template.
It gets passed the current property name, property value, the template, and the search query.

If the function returns a non-undefined value, it gets replaced in the template.

Expand All @@ -178,7 +178,7 @@ Example:
```js
SimpleJekyllSearch({
...
templateMiddleware: function(prop, value, template) {
templateMiddleware: function(prop, value, template, query) {
if (prop === 'bar') {
return value.replace(/^\//, '')
}
Expand Down
4 changes: 2 additions & 2 deletions WIKI.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ If the `search.json` contains this data

A function that will be called whenever a match in the template is found.

It gets passed the current property name, property value, and the template.
It gets passed the current property name, property value, the template, and the search query.

If the function returns a non-undefined value, it gets replaced in the template.

Expand All @@ -68,7 +68,7 @@ Example:
```js
SimpleJekyllSearch({
...
templateMiddleware: function(prop, value, template) {
templateMiddleware: function(prop, value, template, query) {
if (prop === 'bar') {
return value.replace(/^\//, '')
}
Expand Down
6 changes: 3 additions & 3 deletions dest/simple-jekyll-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function setOptions (_options) {

function compile (data) {
return options.template.replace(options.pattern, function (match, prop) {
const value = options.middleware(prop, data[prop], options.template)
const value = options.middleware(prop, data[prop], options.template, data.query)
if (typeof value !== 'undefined') {
return value
}
Expand Down Expand Up @@ -90,7 +90,7 @@ function LiteralSearchStrategy () {
}
if (crit.startsWith('"') && crit.endsWith('"')) {
exact = true
crit = crit.substring(1, crit.length - 2)
crit = crit.substring(1, crit.length - 1)
}
crit = crit.toLowerCase()
crit = exact ? [crit] : crit.split(' ')
Expand Down Expand Up @@ -192,7 +192,7 @@ function findMatches (data, crit, strategy, opt) {

function findMatchesInObject (obj, crit, strategy, opt) {
for (const key in obj) {
if (!isExcluded(obj[key], opt.exclude) && strategy.matches(obj[key], crit)) {
if (key !== 'query' && !isExcluded(obj[key], opt.exclude) && strategy.matches(obj[key], crit)) {
return obj
}
}
Expand Down
2 changes: 1 addition & 1 deletion dest/simple-jekyll-search.min.js

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

6 changes: 3 additions & 3 deletions example/js/simple-jekyll-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function setOptions (_options) {

function compile (data) {
return options.template.replace(options.pattern, function (match, prop) {
const value = options.middleware(prop, data[prop], options.template)
const value = options.middleware(prop, data[prop], options.template, data.query)
if (typeof value !== 'undefined') {
return value
}
Expand Down Expand Up @@ -90,7 +90,7 @@ function LiteralSearchStrategy () {
}
if (crit.startsWith('"') && crit.endsWith('"')) {
exact = true
crit = crit.substring(1, crit.length - 2)
crit = crit.substring(1, crit.length - 1)
}
crit = crit.toLowerCase()
crit = exact ? [crit] : crit.split(' ')
Expand Down Expand Up @@ -192,7 +192,7 @@ function findMatches (data, crit, strategy, opt) {

function findMatchesInObject (obj, crit, strategy, opt) {
for (const key in obj) {
if (!isExcluded(obj[key], opt.exclude) && strategy.matches(obj[key], crit)) {
if (key !== 'query' && !isExcluded(obj[key], opt.exclude) && strategy.matches(obj[key], crit)) {
return obj
}
}
Expand Down
Loading

0 comments on commit 9746038

Please sign in to comment.