amis/build/generate-search-data.js

39 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2020-08-20 23:56:28 +08:00
/**
* @file 生成给前端全文搜索用的文件
* @author wuduoyi
*/
const glob = require('glob');
const fs = require('fs');
let yaml = require('js-yaml');
var rYml = /^\s*---([\s\S]*?)---\s/;
const resultData = {docs: []};
glob('./docs/**/*.md', {}, function (er, docs) {
for (const doc of docs) {
let content = fs.readFileSync(doc, {encoding: 'utf8'});
let m = rYml.exec(content);
let info = {};
if (m && m[1]) {
info = yaml.safeLoad(m[1]);
content = content.substring(m[0].length);
}
const title = info.title || doc;
// todo: 属性列表单独处理,检索的时候优先检索
resultData.docs.push({
title: title,
// 去掉注释、换行、图片等
body: content
.replace(/<\!---.+-->/g, '')
.replace(/!?\[.*\]\(.*\)/g, '')
.replace(/\n/g, '')
.replace(/```.*```/g, '')
.toLowerCase(),
2020-08-21 15:07:13 +08:00
path: doc.slice(1).replace('.md', '')
2020-08-20 23:56:28 +08:00
});
}
2020-08-21 12:20:05 +08:00
fs.writeFileSync('./examples/docs.json', JSON.stringify(resultData));
2020-08-20 23:56:28 +08:00
});