-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #2 The content is cleared after running
refer to the code in hexo-auto-category
- Loading branch information
Showing
4 changed files
with
105 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters