首页

自动翻译文档-expr插件和aichat插件的组合技

前言

我有时候有将文章翻译为英文用于分享到英文互联网的需求,所以现在我可以通过 aichat 插件来使用 ai 帮我翻译文档,然后使用 expr 插件 来自动更新英文笔记,最后使用 OceanPress 来分享我的笔记,一套连招丝滑完成。

实现方式

1.
安装 expr 插件最新版
2.
安装 aichat 插件最新版
在需要输出译文的块上添加 expr 属性( 按住shift键点击块标弹出自定义属性面板 ) ​
表达式如下,需要修改其中的 sourceId 为 原文文档的 id (右键对应文档>复制>id)
js
// 需要被翻译的块(文档)的 id const sourceId = '20250306213356-ekvz9w9'; const sourceAttr = await expr.util.api.getBlockAttrs(sourceId); // 表达式块的属性 const blockAttr = await expr.util.api.getBlockAttrs(block.id); // 只支持英文字母和数字,不支持特殊字符和空格 const updateFlag = 'custom-ai-translated-updated'; if (sourceAttr.updated === blockAttr[updateFlag]) { // 原文档没有更新,所以表达式块的内容也不需要更新 return expr.flag.noOutput; } else { const res = await expr.util.api.getBlockKramdown(sourceId); const kramdown = res ? res.kramdown : res; if (!kramdown) { return expr.flag.noOutput; } block.Attr={ [updateFlag]: sourceAttr.updated } return ( '{{{row\n' + (await aiChatPlugin.openaiAPI.ai翻译为英文({ openai: aiChatPlugin.openaiAPI.openai }, kramdown)) .res + '\n}}}' ); }
在翻译之后,如果你更新了原文,对应的译文表达式在特定的时机下会自动同步更新
正在执行的表达式块会有蓝色边框闪烁