mirror of
https://github.com/kaka111222333/kaka111222333.github.io.git
synced 2025-12-16 23:09:53 +08:00
add Simple-Jekyll-Search
This commit is contained in:
@@ -3,13 +3,16 @@ layout: default
|
||||
---
|
||||
|
||||
<div id="search-container">
|
||||
<input type="text" id="search-input" placeholder="search...">
|
||||
<input type="text" id="search-input" placeholder="search blog posts...">
|
||||
<ul id="results-container"></ul>
|
||||
</div>
|
||||
|
||||
<script src="https://unpkg.com/simple-jekyll-search/dest/simple-jekyll-search.min.js"></script>
|
||||
|
||||
<!--script src="https://unpkg.com/simple-jekyll-search/dest/simple-jekyll-search.min.js"></script-->
|
||||
<script src="{{ site.baseurl }}/js/simple-jekyll-search.min.js"></script>
|
||||
|
||||
<script>
|
||||
window.simpleJekyllSearch = new SimpleJekyllSearch({
|
||||
window.simpleJekyllSearch = new SimpleJekyllSearch({
|
||||
searchInput: document.getElementById('search-input'),
|
||||
resultsContainer: document.getElementById('results-container'),
|
||||
json: '{{ site.baseurl }}/search.json',
|
||||
|
||||
419
js/simple-jekyll-search.js
Normal file
419
js/simple-jekyll-search.js
Normal file
@@ -0,0 +1,419 @@
|
||||
/*!
|
||||
* Simple-Jekyll-Search v1.7.2 (https://github.com/christian-fei/Simple-Jekyll-Search)
|
||||
* Copyright 2015-2018, Christian Fei
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
(function(){
|
||||
/* globals ActiveXObject:false */
|
||||
|
||||
'use strict'
|
||||
|
||||
var _$JSONLoader_2 = {
|
||||
load: load
|
||||
}
|
||||
|
||||
function load (location, callback) {
|
||||
var xhr = getXHR()
|
||||
xhr.open('GET', location, true)
|
||||
xhr.onreadystatechange = createStateChangeListener(xhr, callback)
|
||||
xhr.send()
|
||||
}
|
||||
|
||||
function createStateChangeListener (xhr, callback) {
|
||||
return function () {
|
||||
if (xhr.readyState === 4 && xhr.status === 200) {
|
||||
try {
|
||||
callback(null, JSON.parse(xhr.responseText))
|
||||
} catch (err) {
|
||||
callback(err, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getXHR () {
|
||||
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')
|
||||
}
|
||||
|
||||
'use strict'
|
||||
|
||||
var _$OptionsValidator_3 = function OptionsValidator (params) {
|
||||
if (!validateParams(params)) {
|
||||
throw new Error('-- OptionsValidator: required options missing')
|
||||
}
|
||||
|
||||
if (!(this instanceof OptionsValidator)) {
|
||||
return new OptionsValidator(params)
|
||||
}
|
||||
|
||||
var requiredOptions = params.required
|
||||
|
||||
this.getRequiredOptions = function () {
|
||||
return requiredOptions
|
||||
}
|
||||
|
||||
this.validate = function (parameters) {
|
||||
var errors = []
|
||||
requiredOptions.forEach(function (requiredOptionName) {
|
||||
if (typeof parameters[requiredOptionName] === 'undefined') {
|
||||
errors.push(requiredOptionName)
|
||||
}
|
||||
})
|
||||
return errors
|
||||
}
|
||||
|
||||
function validateParams (params) {
|
||||
if (!params) {
|
||||
return false
|
||||
}
|
||||
return typeof params.required !== 'undefined' && params.required instanceof Array
|
||||
}
|
||||
}
|
||||
|
||||
'use strict';
|
||||
|
||||
function fuzzysearch (needle, haystack) {
|
||||
var tlen = haystack.length;
|
||||
var qlen = needle.length;
|
||||
if (qlen > tlen) {
|
||||
return false;
|
||||
}
|
||||
if (qlen === tlen) {
|
||||
return needle === haystack;
|
||||
}
|
||||
outer: for (var i = 0, j = 0; i < qlen; i++) {
|
||||
var nch = needle.charCodeAt(i);
|
||||
while (j < tlen) {
|
||||
if (haystack.charCodeAt(j++) === nch) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
var _$fuzzysearch_1 = fuzzysearch;
|
||||
|
||||
'use strict'
|
||||
|
||||
/* removed: var _$fuzzysearch_1 = require('fuzzysearch') */;
|
||||
|
||||
var _$FuzzySearchStrategy_5 = new FuzzySearchStrategy()
|
||||
|
||||
function FuzzySearchStrategy () {
|
||||
this.matches = function (string, crit) {
|
||||
return _$fuzzysearch_1(crit.toLowerCase(), string.toLowerCase())
|
||||
}
|
||||
}
|
||||
|
||||
'use strict'
|
||||
|
||||
var _$LiteralSearchStrategy_6 = new LiteralSearchStrategy()
|
||||
|
||||
function LiteralSearchStrategy () {
|
||||
this.matches = function (str, crit) {
|
||||
if (!str) return false
|
||||
|
||||
str = str.trim().toLowerCase()
|
||||
crit = crit.trim().toLowerCase()
|
||||
|
||||
return crit.split(' ').filter(function (word) {
|
||||
return str.indexOf(word) >= 0
|
||||
}).length === crit.split(' ').length
|
||||
}
|
||||
}
|
||||
|
||||
'use strict'
|
||||
|
||||
var _$Repository_4 = {
|
||||
put: put,
|
||||
clear: clear,
|
||||
search: search,
|
||||
setOptions: setOptions
|
||||
}
|
||||
|
||||
/* removed: var _$FuzzySearchStrategy_5 = require('./SearchStrategies/FuzzySearchStrategy') */;
|
||||
/* removed: var _$LiteralSearchStrategy_6 = require('./SearchStrategies/LiteralSearchStrategy') */;
|
||||
|
||||
function NoSort () {
|
||||
return 0
|
||||
}
|
||||
|
||||
var data = []
|
||||
var opt = {}
|
||||
|
||||
opt.fuzzy = false
|
||||
opt.limit = 10
|
||||
opt.searchStrategy = opt.fuzzy ? _$FuzzySearchStrategy_5 : _$LiteralSearchStrategy_6
|
||||
opt.sort = NoSort
|
||||
|
||||
function put (data) {
|
||||
if (isObject(data)) {
|
||||
return addObject(data)
|
||||
}
|
||||
if (isArray(data)) {
|
||||
return addArray(data)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
function clear () {
|
||||
data.length = 0
|
||||
return data
|
||||
}
|
||||
|
||||
function isObject (obj) {
|
||||
return Boolean(obj) && Object.prototype.toString.call(obj) === '[object Object]'
|
||||
}
|
||||
|
||||
function isArray (obj) {
|
||||
return Boolean(obj) && Object.prototype.toString.call(obj) === '[object Array]'
|
||||
}
|
||||
|
||||
function addObject (_data) {
|
||||
data.push(_data)
|
||||
return data
|
||||
}
|
||||
|
||||
function addArray (_data) {
|
||||
var added = []
|
||||
clear()
|
||||
for (var i = 0, len = _data.length; i < len; i++) {
|
||||
if (isObject(_data[i])) {
|
||||
added.push(addObject(_data[i]))
|
||||
}
|
||||
}
|
||||
return added
|
||||
}
|
||||
|
||||
function search (crit) {
|
||||
if (!crit) {
|
||||
return []
|
||||
}
|
||||
return findMatches(data, crit, opt.searchStrategy, opt).sort(opt.sort)
|
||||
}
|
||||
|
||||
function setOptions (_opt) {
|
||||
opt = _opt || {}
|
||||
|
||||
opt.fuzzy = _opt.fuzzy || false
|
||||
opt.limit = _opt.limit || 10
|
||||
opt.searchStrategy = _opt.fuzzy ? _$FuzzySearchStrategy_5 : _$LiteralSearchStrategy_6
|
||||
opt.sort = _opt.sort || NoSort
|
||||
}
|
||||
|
||||
function findMatches (data, crit, strategy, opt) {
|
||||
var matches = []
|
||||
for (var i = 0; i < data.length && matches.length < opt.limit; i++) {
|
||||
var match = findMatchesInObject(data[i], crit, strategy, opt)
|
||||
if (match) {
|
||||
matches.push(match)
|
||||
}
|
||||
}
|
||||
return matches
|
||||
}
|
||||
|
||||
function findMatchesInObject (obj, crit, strategy, opt) {
|
||||
for (var key in obj) {
|
||||
if (!isExcluded(obj[key], opt.exclude) && strategy.matches(obj[key], crit)) {
|
||||
return obj
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isExcluded (term, excludedTerms) {
|
||||
var excluded = false
|
||||
excludedTerms = excludedTerms || []
|
||||
for (var i = 0, len = excludedTerms.length; i < len; i++) {
|
||||
var excludedTerm = excludedTerms[i]
|
||||
if (!excluded && new RegExp(term).test(excludedTerm)) {
|
||||
excluded = true
|
||||
}
|
||||
}
|
||||
return excluded
|
||||
}
|
||||
|
||||
'use strict'
|
||||
|
||||
var _$Templater_7 = {
|
||||
compile: compile,
|
||||
setOptions: __setOptions_7
|
||||
}
|
||||
|
||||
var options = {}
|
||||
options.pattern = /\{(.*?)\}/g
|
||||
options.template = ''
|
||||
options.middleware = function () {}
|
||||
|
||||
function __setOptions_7 (_options) {
|
||||
options.pattern = _options.pattern || options.pattern
|
||||
options.template = _options.template || options.template
|
||||
if (typeof _options.middleware === 'function') {
|
||||
options.middleware = _options.middleware
|
||||
}
|
||||
}
|
||||
|
||||
function compile (data) {
|
||||
return options.template.replace(options.pattern, function (match, prop) {
|
||||
var value = options.middleware(prop, data[prop], options.template)
|
||||
if (typeof value !== 'undefined') {
|
||||
return value
|
||||
}
|
||||
return data[prop] || match
|
||||
})
|
||||
}
|
||||
|
||||
'use strict'
|
||||
|
||||
var _$utils_9 = {
|
||||
merge: merge,
|
||||
isJSON: isJSON
|
||||
}
|
||||
|
||||
function merge (defaultParams, mergeParams) {
|
||||
var mergedOptions = {}
|
||||
for (var option in defaultParams) {
|
||||
mergedOptions[option] = defaultParams[option]
|
||||
if (typeof mergeParams[option] !== 'undefined') {
|
||||
mergedOptions[option] = mergeParams[option]
|
||||
}
|
||||
}
|
||||
return mergedOptions
|
||||
}
|
||||
|
||||
function isJSON (json) {
|
||||
try {
|
||||
if (json instanceof Object && JSON.parse(JSON.stringify(json))) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var _$src_8 = {};
|
||||
(function (window) {
|
||||
'use strict'
|
||||
|
||||
var options = {
|
||||
searchInput: null,
|
||||
resultsContainer: null,
|
||||
json: [],
|
||||
success: Function.prototype,
|
||||
searchResultTemplate: '<li><a href="{url}" title="{desc}">{title}</a></li>',
|
||||
templateMiddleware: Function.prototype,
|
||||
sortMiddleware: function () {
|
||||
return 0
|
||||
},
|
||||
noResultsText: 'No results found',
|
||||
limit: 10,
|
||||
fuzzy: false,
|
||||
exclude: []
|
||||
}
|
||||
|
||||
var requiredOptions = ['searchInput', 'resultsContainer', 'json']
|
||||
|
||||
/* removed: var _$Templater_7 = require('./Templater') */;
|
||||
/* removed: var _$Repository_4 = require('./Repository') */;
|
||||
/* removed: var _$JSONLoader_2 = require('./JSONLoader') */;
|
||||
var optionsValidator = _$OptionsValidator_3({
|
||||
required: requiredOptions
|
||||
})
|
||||
/* removed: var _$utils_9 = require('./utils') */;
|
||||
|
||||
window.SimpleJekyllSearch = function (_options) {
|
||||
var errors = optionsValidator.validate(_options)
|
||||
if (errors.length > 0) {
|
||||
throwError('You must specify the following required options: ' + requiredOptions)
|
||||
}
|
||||
|
||||
options = _$utils_9.merge(options, _options)
|
||||
|
||||
_$Templater_7.setOptions({
|
||||
template: options.searchResultTemplate,
|
||||
middleware: options.templateMiddleware
|
||||
})
|
||||
|
||||
_$Repository_4.setOptions({
|
||||
fuzzy: options.fuzzy,
|
||||
limit: options.limit,
|
||||
sort: options.sortMiddleware
|
||||
})
|
||||
|
||||
if (_$utils_9.isJSON(options.json)) {
|
||||
initWithJSON(options.json)
|
||||
} else {
|
||||
initWithURL(options.json)
|
||||
}
|
||||
|
||||
return {
|
||||
search: search
|
||||
}
|
||||
}
|
||||
|
||||
function initWithJSON (json) {
|
||||
options.success(json)
|
||||
_$Repository_4.put(json)
|
||||
registerInput()
|
||||
}
|
||||
|
||||
function initWithURL (url) {
|
||||
_$JSONLoader_2.load(url, function (err, json) {
|
||||
if (err) {
|
||||
throwError('failed to get JSON (' + url + ')')
|
||||
}
|
||||
initWithJSON(json)
|
||||
})
|
||||
}
|
||||
|
||||
function emptyResultsContainer () {
|
||||
options.resultsContainer.innerHTML = ''
|
||||
}
|
||||
|
||||
function appendToResultsContainer (text) {
|
||||
options.resultsContainer.innerHTML += text
|
||||
}
|
||||
|
||||
function registerInput () {
|
||||
options.searchInput.addEventListener('keyup', function (e) {
|
||||
if (isWhitelistedKey(e.which)) {
|
||||
emptyResultsContainer()
|
||||
search(e.target.value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function search (query) {
|
||||
if (isValidQuery(query)) {
|
||||
emptyResultsContainer()
|
||||
render(_$Repository_4.search(query), query)
|
||||
}
|
||||
}
|
||||
|
||||
function render (results, query) {
|
||||
var len = results.length
|
||||
if (len === 0) {
|
||||
return appendToResultsContainer(options.noResultsText)
|
||||
}
|
||||
for (var i = 0; i < len; i++) {
|
||||
results[i].query = query
|
||||
appendToResultsContainer(_$Templater_7.compile(results[i]))
|
||||
}
|
||||
}
|
||||
|
||||
function isValidQuery (query) {
|
||||
return query && query.length > 0
|
||||
}
|
||||
|
||||
function isWhitelistedKey (key) {
|
||||
return [13, 16, 20, 37, 38, 39, 40, 91].indexOf(key) === -1
|
||||
}
|
||||
|
||||
function throwError (message) {
|
||||
throw new Error('SimpleJekyllSearch --- ' + message)
|
||||
}
|
||||
})(window)
|
||||
|
||||
}());
|
||||
6
js/simple-jekyll-search.min.js
vendored
Normal file
6
js/simple-jekyll-search.min.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/*!
|
||||
* Simple-Jekyll-Search v1.7.2 (https://github.com/christian-fei/Simple-Jekyll-Search)
|
||||
* Copyright 2015-2018, Christian Fei
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
!function(){"use strict";var f={load:function w(t,e){var n=function r(){return window.XMLHttpRequest?new window.XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP")}();n.open("GET",t,!0),n.onreadystatechange=function i(e,n){return function(){if(4===e.readyState&&200===e.status)try{n(null,JSON.parse(e.responseText))}catch(t){n(t,null)}}}(n,e),n.send()}};(function y(t){if(!function e(t){return!!t&&"undefined"!=typeof t.required&&t.required instanceof Array}(t))throw new Error("-- OptionsValidator: required options missing");if(!(this instanceof y))return new y(t);var r=t.required;this.getRequiredOptions=function(){return r},this.validate=function(e){var n=[];return r.forEach(function(t){"undefined"==typeof e[t]&&n.push(t)}),n}});var n=function g(t,e){var n=e.length,r=t.length;if(n<r)return!1;if(r===n)return t===e;t:for(var i=0,o=0;i<r;i++){for(var u=t.charCodeAt(i);o<n;)if(e.charCodeAt(o++)===u)continue t;return!1}return!0},e=new function t(){this.matches=function(t,e){return n(e.toLowerCase(),t.toLowerCase())}};var r=new function O(){this.matches=function(e,t){return!!e&&(e=e.trim().toLowerCase(),(t=t.trim().toLowerCase()).split(" ").filter(function(t){return 0<=e.indexOf(t)}).length===t.split(" ").length)}};var l={put:function z(t){if(c(t))return s(t);if(function e(t){return Boolean(t)&&"[object Array]"===Object.prototype.toString.call(t)}(t))return function i(t){var e=[];a();for(var n=0,r=t.length;n<r;n++)c(t[n])&&e.push(s(t[n]));return e}(t);return undefined},clear:a,search:function S(t){return t?function a(t,e,n,r){for(var i=[],o=0;o<t.length&&i.length<r.limit;o++){var u=d(t[o],e,n,r);u&&i.push(u)}return i}(o,t,u.searchStrategy,u).sort(u.sort):[]},setOptions:function q(t){(u=t||{}).fuzzy=t.fuzzy||!1,u.limit=t.limit||10,u.searchStrategy=t.fuzzy?e:r,u.sort=t.sort||i}};function i(){return 0}var o=[],u={};function a(){return o.length=0,o}function c(t){return Boolean(t)&&"[object Object]"===Object.prototype.toString.call(t)}function s(t){return o.push(t),o}function d(t,e,n,r){for(var i in t)if(!p(t[i],r.exclude)&&n.matches(t[i],e))return t}function p(t,e){for(var n=!1,r=0,i=(e=e||[]).length;r<i;r++){var o=e[r];!n&&new RegExp(t).test(o)&&(n=!0)}return n}u.fuzzy=!1,u.limit=10,u.searchStrategy=u.fuzzy?e:r,u.sort=i;var h={compile:function j(r){return m.template.replace(m.pattern,function(t,e){var n=m.middleware(e,r[e],m.template);return void 0!==n?n:r[e]||t})},setOptions:function C(t){m.pattern=t.pattern||m.pattern,m.template=t.template||m.template,"function"==typeof t.middleware&&(m.middleware=t.middleware)}},m={};m.pattern=/\{(.*?)\}/g,m.template="",m.middleware=function(){};var v={merge:function L(t,e){var n={};for(var r in t)n[r]=t[r],"undefined"!=typeof e[r]&&(n[r]=e[r]);return n},isJSON:function M(t){try{return!!(t instanceof Object&&JSON.parse(JSON.stringify(t)))}catch(e){return!1}}};!function(t){var o={searchInput:null,resultsContainer:null,json:[],success:Function.prototype,searchResultTemplate:'<li><a href="{url}" title="{desc}">{title}</a></li>',templateMiddleware:Function.prototype,sortMiddleware:function(){return 0},noResultsText:"No results found",limit:10,fuzzy:!1,exclude:[]},n=["searchInput","resultsContainer","json"],r=function y(e){if(!function n(t){return!!t&&"undefined"!=typeof t.required&&t.required instanceof Array}(e))throw new Error("-- OptionsValidator: required options missing");if(!(this instanceof y))return new y(e);var r=e.required;this.getRequiredOptions=function(){return r},this.validate=function(e){var n=[];return r.forEach(function(t){"undefined"==typeof e[t]&&n.push(t)}),n}}({required:n});function i(t){o.success(t),l.put(t),function e(){o.searchInput.addEventListener("keyup",function(t){(function e(t){return-1===[13,16,20,37,38,39,40,91].indexOf(t)})(t.which)&&(u(),c(t.target.value))})}()}function u(){o.resultsContainer.innerHTML=""}function a(t){o.resultsContainer.innerHTML+=t}function c(t){(function e(t){return t&&0<t.length})(t)&&(u(),function i(t,e){var n=t.length;if(0===n)return a(o.noResultsText);for(var r=0;r<n;r++)t[r].query=e,a(h.compile(t[r]))}(l.search(t),t))}function s(t){throw new Error("SimpleJekyllSearch --- "+t)}t.SimpleJekyllSearch=function(t){return 0<r.validate(t).length&&s("You must specify the following required options: "+n),o=v.merge(o,t),h.setOptions({template:o.searchResultTemplate,middleware:o.templateMiddleware}),l.setOptions({fuzzy:o.fuzzy,limit:o.limit,sort:o.sortMiddleware}),v.isJSON(o.json)?i(o.json):function e(n){f.load(n,function(t,e){t&&s("failed to get JSON ("+n+")"),i(e)})}(o.json),{search:c}}}(window)}();
|
||||
1598
package-lock.json
generated
1598
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
14
search.json
Normal file
14
search.json
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
layout: null
|
||||
---
|
||||
[
|
||||
{% 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 %}
|
||||
]
|
||||
Reference in New Issue
Block a user