Generate Fuse Index With Node.js

Fuse.js is a lightweight fuzzy-search, in JavaScript, with zero dependencies.

Cheerio is a fast, flexible, and elegant library for parsing and manipulating HTML and XML.

With Node.js, use Cheerio to manipulate HTML, then select elements to generate Fuse index.

const fs = require('fs');
const cheerio = require('cheerio');
const entries = [];

for (const file in htmlFiles) {
    const htmlContent = fs.readFileSync(file, {encoding: 'utf8', flag: 'r'});
    const $ = cheerio.load(htmlContent);
    const title = $('title').text();
    const description = $('meta[name=description]').attr('content');
    const keywords = $('meta[name=keywords]').attr('content');
    const url = '/posts/' + file;
    entries.push({
        title: title || '',
        description: description || '',
        keywords: keywords || '',
        url: url
    });
}

console.log(JSON.stringify(entries));

Related

Minify CSS and JavaScript

发布博客时,压缩CSS和JavaScript文件的大小,能提升网页的加载速度,这里介绍使用cssnano和Terser工具来压缩。