Skip to content

Commit

Permalink
fix #2 The content is cleared after running
Browse files Browse the repository at this point in the history
 refer to the code in hexo-auto-category
  • Loading branch information
jankiny committed Sep 17, 2024
1 parent a8ec42b commit 78d3245
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 84 deletions.
85 changes: 2 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,4 @@
'use strict';

const strip = require('./lib/strip');
const ai = require('./lib/ai');
const log = require('hexo-log').default({
debug: false,
silent: false
});
const fs = require('hexo-fs');
const fm = require('hexo-front-matter');

const config = hexo.config.hexo_ai_abstract;

hexo.extend.filter.register('after_post_render', async function (data) {
try {
// Ensure that config exists
if (!config || !config.enable) return data;

// Check if summaries are enabled
if (config.enable === 'off') return data;

// Redundant check: If AI Abstract already exists
if (data.aiAbstract && data.aiAbstract.trim() !== '') return data;

// Only continue if AI Abstract is set and empty
if (data.aiAbstract === undefined && config.enable === 'on') return data;

const path = this.source_dir + data.source;
const frontMatterString = await fs.readFile(path);
const frontMatter = fm.parse(frontMatterString);

if (frontMatter.tags && frontMatter.tags.some(tag => config.ignores.byTag.includes(tag))) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 的标签包含于 ignoreTag (${config.ignores.byTag}) 列表,跳过AI摘要生成`);
return data;
}
if (frontMatter.title && config.ignores.byTitle.includes(frontMatter.title)) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 的标题包含于 ignoreTitle (${config.ignores.byTitle}) 列表,跳过AI摘要生成`);
return data;
}
if (config.ignores.byAttribute.some(attr => frontMatter[attr] !== undefined)) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 的属性包含于 ignoreAttribute (${config.ignores.byAttribute}) 列表 ,跳过AI摘要生成`);
return data;
}

const content = strip(data.content, config.ignoreEl);

if (typeof content === 'string' && content.length > config.maxTokens) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 超过 maxTokens 限制`);
return data;
}

log.info(`hexo-ai-abstract: 生成文章 ${data.title} 的AI摘要`);
frontMatter.aiAbstract = data.aiAbstract = await ai(config.apiKey, config.apiUrl, config.model, content, config.prompt, config.maxTokens);
await fs.writeFile(path, `---\n${fm.stringify(frontMatter)}`);

return data;
} catch (error) {
log.error(`hexo-ai-abstract: 处理文章 ${data.title} 时出错: ${error.message}`);
return data;
}
});

hexo.extend.filter.register('before_post_render', function (data) {
try {
if (!data.aiAbstract || data.aiAbstract.trim() === '') return data;

const aiAbstractContent = '**AI导读:**' + data.aiAbstract;
const newContent = config.inject.front ? `\n\n${aiAbstractContent}\n\n${config.inject.anchor}` : `${config.inject.anchor}\n\n${aiAbstractContent}\n\n`

if (typeof data.content === 'string') {
const anchorPattern = new RegExp(escapeRegExp(config.inject.anchor), 'g');
data.content = data.content.replace(anchorPattern, newContent);
// log.info(data.content);
}
return data;
} catch (error) {
log.error(`hexo-ai-abstract: 处理文章 ${data.title} 前渲染时出错: ${error.message}`);
return data;
}
});

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

hexo.extend.filter.register('after_post_render', require('./lib/generator'), 15);
hexo.extend.filter.register('before_post_render', require('./lib/injector'), 15);
70 changes: 70 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const strip = require('./strip');
const ai = require('./ai');

var front = require('hexo-front-matter');
var fs = require('hexo-fs');
debugger;

let generator = async function (data) {
var log = this.log;
const config = this.config.hexo_ai_abstract;

if (data.layout != 'post')
return data;
if (!this.config.render_drafts && data.source.startsWith("_drafts/"))
return data;

// Redundant check: If AI Abstract already exists
if (data.aiAbstract && data.aiAbstract.trim() !== '') return data;

// Ensure that config exists
if (!config || !config.enable) return data;

// Check if hexo_ai_abstract are enabled
if (config.enable === 'off') return data;

// Only continue if AI Abstract is set and empty
if (data.aiAbstract === undefined && config.enable === 'on') return data;

var overwrite = true;
if (overwrite) {
let postStr;
// 1. parse front matter
var tmpPost = front.parse(data.raw);
// 2. ignore posts by Tag, Title, Attribute
if (tmpPost.tags && tmpPost.tags.some(tag => config.ignores.byTag.includes(tag))) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 的标签包含于 ignoreTag (${config.ignores.byTag}) 列表,跳过AI摘要生成`);
return data;
}
if (tmpPost.title && config.ignores.byTitle.includes(tmpPost.title)) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 的标题包含于 ignoreTitle (${config.ignores.byTitle}) 列表,跳过AI摘要生成`);
return data;
}
if (config.ignores.byAttribute.some(attr => tmpPost[attr] !== undefined)) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 的属性包含于 ignoreAttribute (${config.ignores.byAttribute}) 列表 ,跳过AI摘要生成`);
return data;
}
// 3. generate ai abstract
const content = strip(data.content, config.ignoreEl);

if (typeof content === 'string' && content.length > config.maxTokens) {
log.info(`hexo-ai-abstract: 文章 ${data.title} 超过 maxTokens 限制`);
return data;
}

var newAiAbstract = await ai(config.apiKey, config.apiUrl, config.model, content, config.prompt, config.maxTokens);
tmpPost.aiAbstract = newAiAbstract
// 4. process post
postStr = front.stringify(tmpPost);
postStr = '---\n' + postStr;
fs.writeFile(data.full_source, postStr, 'utf-8');
log.i(`hexo-ai-abstract: 生成文章 ${data.title} 的AI摘要`);
}
return data
}



module.exports = generator;
32 changes: 32 additions & 0 deletions lib/injector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

debugger;
let injector = function (data) {
var log = this.log;
try {
const config = this.config.hexo_ai_abstract;

if (!data.aiAbstract || data.aiAbstract.trim() === '') return data;

const aiAbstractContent = '**AI导读:**' + data.aiAbstract;
const newContent = config.inject.front ? `\n\n${aiAbstractContent}\n\n${config.inject.anchor}` : `${config.inject.anchor}\n\n${aiAbstractContent}\n\n`

if (typeof data.content === 'string') {
const anchorPattern = new RegExp(escapeRegExp(config.inject.anchor), 'g');
data.content = data.content.replace(anchorPattern, newContent);
// log.info(data.content);
}

// log.i(`hexo-ai-abstract: 文章 ${data.title} 的AI摘要已注入 ${config.inject.anchor} ${config.inject.front ? '前' : '后'}`);
return data;
} catch (error) {
log.e(`hexo-ai-abstract: 在 injector 文章 ${data.title} 时出错: ${error.message}`);
return data;
}
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

module.exports = injector;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-ai-abstract",
"version": "1.1.0",
"version": "1.2.0",
"description": "A Hexo plugin to generate AI-based abstracts for your blog posts. Fix hexo-ai-excerpt.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 78d3245

Please sign in to comment.