const fs = require('fs');
const path = require('path');
hexo.extend.helper.register('icon', (name) => svgIcon(name));
// Register custom clean_url helper function
hexo.extend.helper.register('clean_url', function (url) {
if (!url) return '';
// If it's an absolute URL, convert it to a relative URL first
const cleanedUrl = this.url_for(url);
// Remove .html suffix
return cleanedUrl.replace(/(index)?\.html$/, '');
});
// Register enhanced url_for helper (optional)
hexo.extend.helper.register('url_for_clean', function (path, options = {}) {
const url = this.url_for(path, options);
return url.replace(/(index)?\.html$/, '');
});
hexo.extend.generator.register('json', (locals) => {
const searchName = 'search.json';
const datas = locals.posts.sort('date') || [];
const res = [];
let index = 0;
datas.each((data) => {
if (data.indexing !== undefined && !data.indexing) {
return;
}
if (data.layout === 'note') {
return;
}
const temp_data = {};
temp_data.index = index;
temp_data.title = data.title;
temp_data.path = data.permalink.replace(hexo.config.url, '');
temp_data.icon = svgIcon(data.slug);
temp_data.background = data.background;
temp_data.intro = data.intro;
// categories
temp_data.categories = [];
if (data.categories && data.categories.length > 0) {
data.categories.forEach((category) => {
temp_data.categories.push(category.name);
});
}
// tags
temp_data.tags = [];
if (data.tags && data.tags.length > 0) {
data.tags.forEach((tag) => {
temp_data.tags.push(tag.name);
});
}
// sections
temp_data.sections = data.sections;
res[index] = temp_data;
index += 1;
});
const json = JSON.stringify(res);
return { path: searchName, data: json };
});
hexo.extend.generator.register('list', (locals) => {
const themeConfig = hexo.theme.config;
let content = '';
themeConfig.index_categories.forEach((category) => {
content += `\n${category}
\n\n`;
locals.categories
.findOne({ name: category })
.posts.sort('-date')
.map((post) => {
content += `- [${post.title}](https://cheatsheets.zip/${post.path}): ${post.intro.trim()}\n`;
});
content += '\n \n\n';
});
return {
path: 'list.md',
data: content
};
});
function svgIcon(name) {
// Check file exists
const svgPath = path.resolve('./', 'source/assets/icon/', `${name}.svg`);
if (fs.existsSync(svgPath)) {
const svgContent = fs.readFileSync(svgPath, 'utf8');
return `${svgContent}`;
} else {
const defaultSvg =
'';
return `${defaultSvg}`;
}
}