Last active
September 19, 2023 08:35
-
-
Save radan-magie/b83e34bc9ec2884ff19a8cee23c2f613 to your computer and use it in GitHub Desktop.
Gruntfile to allow Lunr to work on a standard goHugo setup, some problems with multiline YAML variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Originale here: | |
https://gist.github.com/sebz/efddfc8fdcb6b480f567 | |
*/ | |
// var toml = require("toml"); | |
var yaml = require("yamljs"); | |
var S = require("string"); | |
var CONTENT_PATH_PREFIX = "website/content"; | |
var OUTPUT_FILE = "website/static/assets/js/lunr/PagesIndex.json"; | |
var FMSEP = '---'; // other option '+++' | |
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 href = S(abspath) | |
.chompLeft(CONTENT_PATH_PREFIX).s; | |
return { | |
title: pageName, | |
href: href, | |
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(FMSEP); | |
var frontMatter; | |
var title = 'Mintlayer Page'; | |
try { | |
// frontMatter = toml.parse(content[1].trim()); | |
frontMatter = yaml.parse(content[1].trim()); | |
// console.log( 'abspath', abspath ); | |
// console.log( 'title', frontMatter.title ); | |
} catch (e) { | |
console.log( 'abspath', abspath ); | |
console.log(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 | |
if( frontMatter.hasOwnProperty('title') ) { | |
tmptitle = frontMatter.title; | |
} else { | |
console.log( 'no title in: ', abspath ); | |
} | |
pageIndex = { | |
// title: frontMatter.title, | |
title: tmptitle, | |
tags: frontMatter.tags, | |
href: href, | |
content: S(content[2]).trim().stripTags().stripPunctuation().s | |
}; | |
return pageIndex; | |
}; | |
grunt.file.write(OUTPUT_FILE , JSON.stringify(indexPages())); | |
grunt.log.ok("Index built"); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment