Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Mar 16, 2022. It is now read-only.

A JavaScript library to add search functionality to any Jekyll blog.

License

NotificationsYou must be signed in to change notification settings

christian-fei/Simple-Jekyll-Search

Repository files navigation

Build Statusdependencies StatusdevDependencies Status

A JavaScript library to add search functionality to any Jekyll blog.

Use case

You have a blog, built with Jekyll, and want alightweight search functionality on your blog, purely client-side?

No server configurations or databases to maintain.

Just5 minutes to have afully working searchable blog.


Installation

npm

npm install simple-jekyll-search

Getting started

Createsearch.json

Place the following code in a file calledsearch.json in theroot of your Jekyll blog. (You can also get a copyfrom here)

This file will be used as a small data source to perform the searches on the client side:

---layout:none---[{% for post in site.posts %}{"title": "{{ post.title | escape }}","category": "{{ post.category }}","tags": "{{ post.tags | join: ', ' }}","url": "{{ site.baseurl }}{{ post.url }}","date": "{{ post.date }}"} {% unless forloop.last %},{% endunless %}{% endfor %}]

Preparing the plugin

Add DOM elements

SimpleJekyllSearch needs twoDOM elements to work:

  • a search input field
  • a result container to display the results

Give me the code

Here is the code you can use with the default configuration:

You need to place the following code within the layout where you want the search to appear. (See the configuration section below to customize it)

For example in_layouts/default.html:

<!-- HTML elements for search --><inputtype="text"id="search-input"placeholder="Search blog posts.."><ulid="results-container"></ul><!-- or without installing anything --><scriptsrc="https://unpkg.com/simple-jekyll-search@latest/dest/simple-jekyll-search.min.js"></script>

Usage

Customize SimpleJekyllSearch by passing in your configuration options:

varsjs=SimpleJekyllSearch({searchInput:document.getElementById('search-input'),resultsContainer:document.getElementById('results-container'),json:'/search.json'})

returns { search }

A new instance of SimpleJekyllSearch returns an object, with the only propertysearch.

search is a function used to simulate a user input and display the matching results. 

E.g.:

varsjs=SimpleJekyllSearch({ ...options})sjs.search('Hello')

💡 it can be used to filter posts by tags or categories!

Options

Here is a list of the available options, usage questions, troubleshooting & guides.

searchInput (Element) [required]

The input element on which the plugin should listen for keyboard event and trigger the searching and rendering for articles.

resultsContainer (Element) [required]

The container element in which the search results should be rendered in. Typically a<ul>.

json (String|JSON) [required]

You can either pass in an URL to thesearch.json file, or the results in form of JSON directly, to save one round trip to get the data.

searchResultTemplate (String) [optional]

The template of a single rendered search result.

The templating syntax is very simple: You just enclose the properties you want to replace with curly braces.

E.g.

The template

varsjs=SimpleJekyllSearch({searchInput:document.getElementById('search-input'),resultsContainer:document.getElementById('results-container'),json:'/search.json',searchResultTemplate:'<li><a href="{{ site.url }}{url}">{title}</a></li>'})

will render to the following

<li><ahref="/jekyll/update/2014/11/01/welcome-to-jekyll.html">Welcome to Jekyll!</a></li>

If thesearch.json contains this data

[    {"title"    :"Welcome to Jekyll!","category" :"","tags"     :"","url"      :"/jekyll/update/2014/11/01/welcome-to-jekyll.html","date"     :"2014-11-01 21:07:22 +0100"    }]

templateMiddleware (Function) [optional]

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.

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

This can be potentially useful for manipulating URLs etc.

Example:

SimpleJekyllSearch({  ...templateMiddleware:function(prop,value,template){if(prop==='bar'){returnvalue.replace(/^\//,'')}}...})

See thetests for an in-depth code example

sortMiddleware (Function) [optional]

A function that will be used to sort the filtered results.

It can be used for example to group the sections together.

Example:

SimpleJekyllSearch({  ...sortMiddleware:function(a,b){varastr=String(a.section)+"-"+String(a.caption);varbstr=String(b.section)+"-"+String(b.caption);returnastr.localeCompare(bstr)}...})

noResultsText (String) [optional]

The HTML that will be shown if the query didn't match anything.

limit (Number) [optional]

You can limit the number of posts rendered on the page.

fuzzy (Boolean) [optional]

Enable fuzzy search to allow less restrictive matching.

exclude (Array) [optional]

Pass in a list of terms you want to exclude (terms will be matched against a regex, so URLs, words are allowed).

success (Function) [optional]

A function called once the data has been loaded.

debounceTime (Number) [optional]

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 nodebounceTime (milliseconds) is provided a search will be triggered on each keystroke.


If search isn't working due to invalid JSON

  • There is a filter plugin in the _plugins folder which should remove most characters that cause invalid JSON. To use it, add the simple_search_filter.rb file to your _plugins folder, and useremove_chars as a filter.

For example: in search.json, replace

"content":"{{ page.content | strip_html | strip_newlines }}"

with

"content":"{{ page.content | strip_html | strip_newlines | remove_chars | escape }}"

If this doesn't work when using Github pages you can tryjsonify to make sure the content is json compatible:

"content":{{page.content|jsonify}}

Note: you don't need to use quotes" in this sincejsonify automatically inserts them.

Enabling full-text search

Replacesearch.json with the following code:

---layout:none---[{% for post in site.posts %}{"title": "{{ post.title | escape }}","category": "{{ post.category }}","tags": "{{ post.tags | join: ', ' }}","url": "{{ site.baseurl }}{{ post.url }}","date": "{{ post.date }}","content": "{{ post.content | strip_html | strip_newlines }}"} {% unless forloop.last %},{% endunless %}{% endfor %},{% for page in site.pages %}{{% if page.title != nil %}"title": "{{ page.title | escape }}","category": "{{ page.category }}","tags": "{{ page.tags | join: ', ' }}","url": "{{ site.baseurl }}{{ page.url }}","date": "{{ page.date }}","content": "{{ page.content | strip_html | strip_newlines }}"{% endif %}} {% unless forloop.last %},{% endunless %}{% endfor %}]

Development

  • npm install
  • npm test

Acceptance tests

cd example; jekyll serve# in another tabnpm run cypress -- run

Contributors

Thanks to allcontributors over the years! You are the best :)

@daviddarnes@XhmikosR@PeterDaveHello@mikeybeck@egladman@midzer@eduardoboucas@kremalicious@tibotiberand many others!

Stargazers over time

Stargazers over time

About

A JavaScript library to add search functionality to any Jekyll blog.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors39


[8]ページ先頭

©2009-2025 Movatter.jp