{"size": 2343, "ext": "js", "max_stars_repo_path": "Gruntfile.js", "max_stars_repo_name": "ashort96/sbpp.github.io", "max_stars_repo_stars_event_min_datetime": "2017-06-16T15:36:44.000Z", "max_stars_repo_stars_event_max_datetime": "2017-06-16T15:36:44.000Z", "max_issues_repo_path": "Gruntfile.js", "max_issues_repo_name": "ashort96/sbpp.github.io", "max_issues_repo_issues_event_min_datetime": "2018-03-05T19:07:23.000Z", "max_issues_repo_issues_event_max_datetime": "2020-09-24T12:54:59.000Z", "max_forks_repo_path": "Gruntfile.js", "max_forks_repo_name": "ashort96/sbpp.github.io", "max_forks_repo_forks_event_min_datetime": "2017-03-30T19:14:52.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-05T20:37:05.000Z"}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": 18, "avg_line_length": 33.0, "max_line_length": 130, "alphanum_fraction": 0.5147247119}
const toml = require('toml');

const CONTENT_PATH_PREFIX = "content/docs";

module.exports = function(grunt) {

    grunt.registerTask("content-index", function() {

        grunt.log.writeln("Build pages index");

        let indexPages = () => {
            let 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;
        }

        let processFile = (abspath, filename) => {
            return (filename.endsWith('.html')) ? processHTMLFile(abspath, filename) : processMDFile(abspath, filename);
        }

        let processHTMLFile = (abspath, filename) => {
            return {
                title: filename.replace('.html', ''),
                href: abspath.replace(CONTENT_PATH_PREFIX, ''),
                content: grunt.file.read(abspath).replace(/<[^>]*>?/gm, '').replace(/[^\w\s]|_/g, '').replace(/\s+/g, ' ').trim(),
            }
        };

        let processMDFile = (abspath, filename) => {
            let content = grunt.file.read(abspath);
            let pageIndex;
            
            // First separate the Front Matter from the content and parse it
            content = content.split("+++");

            let frontMatter;

            try {
                frontMatter = toml.parse(content[1].trim());
            } catch (e) {
                console.log(e.message);
            }

            let href = abspath.replace(CONTENT_PATH_PREFIX, '').replace('.md', '');

            // href for index.md files stops at the folder name
            if (filename === 'index.md') {
                href = abspath.replace(CONTENT_PATH_PREFIX, '').replace(filename, '');
            }

            // Build Lunr index for this page
            pageIndex = {
                title: frontMatter.title,
                tags: frontMatter.tags,
                href: href,
                content: content[2].replace(/<[^>]*>?/gm, '').replace(/[^\w\s]|_/g, '').replace(/\s+/g, ' ').trim(),
            };

            return pageIndex;
        };

        grunt.file.write("static/Content-Index.json", JSON.stringify(indexPages()));
        grunt.log.ok("Index built");
    });
};
