Skip to content

Commit

Permalink
Add full content searching support for MDwiki and Wikitten, powered b…
Browse files Browse the repository at this point in the history
…y lunr.js
  • Loading branch information
zohead committed Jan 5, 2017
1 parent 5e9c9d6 commit ea6d35a
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 14 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ I made some modifications to MDwiki and Wikitten, so they can share the same Wik
Once you add new Markdown document or delete/rename document in `library` directory, you should run this **`generate-index.sh`** to generate new index files.
* Support full content searching Markdown document by modified version **lunr.js**, I write an auto generate search index file node.js program **`search.js`**, MDwiki and Wikitten use **search_index.json** index file in Wiki program directory.
```bash
node search.js library search_index.json
```
If you need full content searching support, you should run this **`search.js`** after making any changes to Wiki library.
### Changes to Wikitten
* Support YAML front matter which begin and end with both three back-ticks (`` ` ``) and three dashes (-);
* Show `index.md` document if switch to on directory (instead of tell user to select in Wiki library tree);
* Set Markdown table width to 100%.
* Set Markdown table width to 100%;
* Support full content searching Markdown document, use **`search.js`** node.js program to generate **search_index.json** search index file.
## Deploy
Expand All @@ -49,6 +58,19 @@ Please refer to [Wikitten](http://wikitten.vizuina.com/) website, you need at le
Wikitten config file is `config.php` in root directory.
> **Note**
>
> You need to change Apache or Nginx rule for **search_index.json** if you need full content searching support, or it will be served by Wikitten PHP by default.
>
> Nginx rule may like this:
>
> ```
> location ~* ^/search_index.json$ {
> access_log off;
> expires max;
> }
> ```
### Deploy MDwiki
Please refer to [MDwiki](http://www.mdwiki.info/) website.
Expand Down
58 changes: 57 additions & 1 deletion index.html

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions library/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@
* [Index...](Travel/index.md)

[Blog](https://zohead.com/)

[🔍]()

* # Search file name or content.
* [<input id="search_input" type="text"/>](#)
57 changes: 57 additions & 0 deletions mdwiki-debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -4360,6 +4360,50 @@
</script>
<!-- END dist/MDwiki.js -->
<script type="text/javascript">$.md.logThreshold = $.md.loglevel.DEBUG;</script>

<script type="text/javascript" src="static/js/lunr.min.js"></script>
<script type="text/javascript">
var search_indexes = null;

function doSearch(query) {
if (search_indexes == null) return;
var s_res = search_indexes.search(query);
var t_html = '<ul>';
for (var i = 0; i < s_res.length; i++) {
var t_pos = s_res[i].ref.lastIndexOf('/');
t_html += '<li><a href="#!' + s_res[i].ref + '">' + (t_pos >= 0 ? s_res[i].ref.substr(t_pos + 1) : s_res[i].ref) + '</a></li>';
}
t_html += '</ul>';
$('#search-result-body').html(t_html);
$('#search-result-label').html('Search result for: ' + query);
$('#search-result').modal('show');
}

function search_content(query) {
if (search_indexes == null) {
$.getJSON("search_index.json", function(data){
if (data != null) search_indexes = lunr.Index.load(data);
doSearch(query);
});
} else
doSearch(query);
}

$(document).bind('DOMNodeInserted', function(e) {
if ($('#search_input').length <= 0) return;
$(document).unbind('DOMNodeInserted');

$('#search_input').bind('click', function(event) {
event.stopPropagation();
return false;
});
$('#search_input').bind('keypress', function(event){
if (event.keyCode == '13')
search_content($(this).val());
});
});
</script>

</head>
<body>
<noscript>
Expand All @@ -4369,5 +4413,18 @@

<div id="md-all">
</div>

<div class="modal fade" id="search-result" tabindex="-1" role="dialog" aria-labelledby="search-result-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&#x274c;</button>
<h4 class="modal-title" id="search-result-label"></h4>
</div>
<div class="modal-body" id="search-result-body"></div>
</div>
</div>
</div>

</body>
</html>
56 changes: 56 additions & 0 deletions mdwiki-slim.html

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var fs = require('fs'), removeMd = require('remove-markdown'), lunr = require('./static/js/lunr.min.js');

var md_index = lunr(function () {
this.field('title');
this.field('tags');
this.field('body');
this.ref('url');
});

function walk_dir(wdir, path, callback) {
var dirList = fs.readdirSync(wdir + path);
dirList.forEach(function(item) {
if(fs.statSync(wdir + path + '/' + item).isDirectory())
walk_dir(wdir, path + '/' + item, callback);
else
callback(wdir, path + '/' + item);
});
}

function process_markdown(wdir, path) {
var pos = path.lastIndexOf(".");
if (pos < 0) return;
if (path.substr(pos) != ".md" && path.substr(pos) != ".markdown") return;
var fpos = path.lastIndexOf("/");
var title = (fpos >= 0 ? path.substring(fpos + 1, pos) : path.substr(0, pos));

// ignore navigation menu document
if (path == "/navigation.md") return;

var has_front = false;
var md_doc = {
"url": path,
"title": title,
"tags": ""
};

var data = fs.readFileSync(wdir + path, "utf-8");
// ignore auto generated index.md
if (title == "index" && data.indexOf("Auto-index of") >= 0) return;

// process front matter
if (data.substr(0, 3) == "```") {
pos = data.substr(3).indexOf("```");
if (pos >= 0) {
var front = JSON.parse("{" + data.substr(3, pos) + "}");
has_front = true;
if ("title" in front) md_doc.title = front.title;
if ("tags" in front) md_doc.tags = front.tags.join(' ');
md_doc.body = removeMd(data.substr(pos + 6));
}
}
if (!has_front) md_doc.body = removeMd(data);
md_index.add(md_doc);
}

if (process.argv.length < 4) {
console.log("Usage:\n");
console.log(process.argv[0] + " " + process.argv[1] + " Wiki-document-directory Search-index-file");
console.log(process.argv[0] + " " + process.argv[1] + " Search-index-file Keyword");
process.exit(1);
} else if (!fs.existsSync(process.argv[2])) {
console.log("Invalid Wiki-document-directory or Search-index-file: " + process.argv[2]);
process.exit(1);
}

if (fs.statSync(process.argv[2]).isDirectory()) {
walk_dir(process.argv[2], "", process_markdown);
fs.writeFileSync(process.argv[3], JSON.stringify(md_index.toJSON()));
} else {
md_index = lunr.Index.load(JSON.parse(fs.readFileSync(process.argv[2])));
console.log(JSON.stringify(md_index.search(process.argv[3])));
}
2 changes: 1 addition & 1 deletion static/css/main.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
background: #EBEEF1 url(../img/noise.png);
background-color: #EBEEF1;
font-size: 13px;
}
.clear {
Expand Down
Loading

0 comments on commit ea6d35a

Please sign in to comment.