MkDocs 0.16.3版本使用了lunr.js 0.5.3版本来做全站的全文搜索,此版本lunr只能进行全词匹配,无法模糊匹配,用最新版本2.0.3替换。
###以MkDocs 0.16.3版本的默认主题'mkdocs'为例
一 、找到此主题目录下lunr.min.js文件,默认主题路径在python安装路径下的site-packages/mkdocs/themes/mkdocs目录里
cd /usr/lib/python2.7/site-packages/mkdocs/assets/search/mkdocs/js
二 、使用最新版本的lunr替换
三 、修改search.js(此文件是该主题用于搜索时的逻辑处理,与lunr.min.js在同一目录里),修改后的内容如下:
require([
base_url + '/mkdocs/js/mustache.min.js',
base_url + '/mkdocs/js/lunr.min.js',
'text!search-results-template.mustache',
'text!../search_index.json',
], function (Mustache, lunr, results_template, data) {
"use strict";
function getSearchTerm()
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == 'q')
{
return decodeURIComponent(sParameterName[1].replace(/\+/g, '%20'));
}
}
}
var documents = {};
var doc;
var index = lunr(function () {
this.field('title', {BOOST: 10});
this.field('text');
this.ref('location');
data = JSON.parse(data);
//var documents = {};
for (var i=0; i < data.docs.length; i++){
doc = data.docs[i];
doc.location = base_url + doc.location;
this.add(doc);
documents[doc.location] = doc;
}
});
/*data = JSON.parse(data);
var documents = {};
for (var i=0; i < data.docs.length; i++){
var doc = data.docs[i];
doc.location = base_url + doc.location;
index.add(doc);
documents[doc.location] = doc;
}*/
var search = function(){
var query = document.getElementById('mkdocs-search-query').value;
var search_results = document.getElementById("mkdocs-search-results");
while (search_results.firstChild) {
search_results.removeChild(search_results.firstChild);
}
if(query === ''){
return;
}
var results = index.search(query);
if (results.length > 0){
for (var i=0; i < results.length; i++){
var result = results[i];
doc = documents[result.ref];
doc.base_url = base_url;
doc.summary = doc.text.substring(0, 200);
var html = Mustache.to_html(results_template, doc);
search_results.insertAdjacentHTML('beforeend', html);
}
} else {
search_results.insertAdjacentHTML('beforeend', "<p>No results found</p>");
}
if(jQuery){
/*
* We currently only automatically hide bootstrap models. This
* requires jQuery to work.
*/
jQuery('#mkdocs_search_modal a').click(function(){
jQuery('#mkdocs_search_modal').modal('hide');
});
}
};
var search_input = document.getElementById('mkdocs-search-query');
var term = getSearchTerm();
if (term){
search_input.value = term;
search();
}
search_input.addEventListener("keyup", search);
});
大功告成,搜索所时使用*,如*rkdow*可搜索包含rkdow的文本内容了