How to implement a custom search for Hugo usig Gruntjs and Lunrjs.
Install the following tools:
Here is my Hugo based website project structure
MyWebsite/
|- site/ <= Hugo project root folder
|- content/
|- layout/
|- static/
|- js/
|- lunr/ <= Where we generate the lunr json index file
|- vendor/
|- lunrjs.min.js <= lunrjs library
|- ...
|- config.yaml
|- ...
|- Gruntfile.js <= Where the magic happens
|- package.json <= Dependencies declaration required to build the index
|- ...
From the project root folder launch npm install --save-dev grunt string toml
- string <= do almost all the work
- toml
- Used to parse the Frontmatter, mine is in TOML... obviously
- Otherwise you can install yamljs
We will work both at buildtime and runtime. With Gruntjs (buildtime), we'll generate a JSON index file and with a small js script (runtime) initilize and use lunrjs.
Lunrjs allows you to define fields to describe your pages (documents in lunrjs terms) that will be used to search and hopefully find stuff. The index file is basically a JSON file corresponding to an array of all the documents (pages) composing the website.
Here are the fields I chose (and the boost level to increase the page ranking) to describe my pages:
title
<=> page title- boost 10
tags
<=> page tags- boost 5
content
<=> page contentref
<=> page URI
- Recursively walk through all files of the
content
folder - Two possibilities
- Markdown file
- Parse the Frontmatter to extract the
title
and thetags
- Parse and clean the content
- Parse the Frontmatter to extract the
- HTML file
- Parse and clean the content
- Use the file name as
title
- Markdown file
- Use the path file as
ref
(link toward the page)
Here is the Gruntfile.js
file:
var toml = require("toml");
var S = require("string");
var CONTENT_PATH_PREFIX = "site/content";
module.exports = function(grunt) {
grunt.registerTask("lunr-index", function() {
grunt.log.writeln("Build pages index");
var indexPages = function() {
var pagesIndex = [];
grunt.file.recurse(CONTENT_PATH_PREFIX, function(abspath, rootdir, subdir, filename) {
grunt.verbose.writeln("Parse file:",abspath);
pagesIndex.push(processFile(abspath, filename));
});
return pagesIndex;
};
var processFile = function(abspath, filename) {
var pageIndex;
if (S(filename).endsWith(".html")) {
pageIndex = processHTMLFile(abspath, filename);
} else {
pageIndex = processMDFile(abspath, filename);
}
return pageIndex;
};
var processHTMLFile = function(abspath, filename) {
var content = grunt.file.read(abspath);
var pageName = S(filename).chompRight(".html").s;
var apiHref = S(abspath)
.chompLeft(CONTENT_PATH_PREFIX)
.chompRight(filename).s +
"index.html#" +
pageName + "/";
return {
title: pageName,
href: apiHref,
content: S(content).trim().stripTags().stripPunctuation().s
};
};
var processMDFile = function(abspath, filename) {
var content = grunt.file.read(abspath);
var pageIndex;
// First separate the Front Matter from the content and parse it
content = content.split("+++");
var frontMatter;
try {
frontMatter = toml.parse(content[1].trim());
} catch (e) {
conzole.failed(e.message);
}
var href = S(abspath).chompLeft(CONTENT_PATH_PREFIX).chompRight(".md").s;
// href for index.md files stops at the folder name
if (filename === "index.md") {
href = S(abspath).chompLeft(CONTENT_PATH_PREFIX).chompRight(filename).s;
}
// Build Lunr index for this page
pageIndex = {
title: frontMatter.title,
tags: frontMatter.tags,
href: href,
content: S(content[2]).trim().stripTags().stripPunctuation().s
};
return pageIndex;
};
grunt.file.write("site/static/js/lunr/PagesIndex.json", JSON.stringify(indexPages()));
grunt.log.ok("Index built");
});
};
Launch the task: grunt lunr-index
TBD