Skip to content

Commit

Permalink
支持清理从 iBooks 中提取的内容
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkPHP authored and ThinkPHP committed Oct 6, 2021
1 parent ab80909 commit 090d8b7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bobplugin-google-translate",
"version": "1.0.2",
"version": "1.1.0",
"description": "Google 翻译插件,无需申请 API 秘钥",
"homepage": "https://github.com/tingv/bobplugin-google-translate",
"repository": "https://github.com/tingv/bobplugin-google-translate.git",
Expand Down
18 changes: 17 additions & 1 deletion src/info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"identifier": "com.tingv.bobplugin.googletranslate",
"category": "translate",
"version": "1.0.2",
"version": "1.1.0",
"name": "Google 翻译",
"summary": "Google 翻译插件,无需申请 API 秘钥",
"author": "TingV",
Expand Down Expand Up @@ -41,6 +41,22 @@
"value": "cn"
}
]
},
{
"identifier": "iBooksCleanup",
"type": "menu",
"title": "iBooks 清理",
"defaultValue": "disable",
"menuValues": [
{
"title": "开启",
"value": "enable"
},
{
"title": "关闭",
"value": "disable"
}
]
}
]
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function supportLanguages(): Bob.supportLanguages {
export function translate(query: Bob.TranslateQuery, completion: Bob.Completion) {
const { text = '', detectFrom, detectTo } = query;
const str = formatString(text);
const params = { from: detectFrom, to: detectTo, cache: Bob.api.getOption('cache'), tld: Bob.api.getOption('tld'), };
const params = { from: detectFrom, to: detectTo, cache: Bob.api.getOption('cache'), tld: Bob.api.getOption('tld'), iBooksCleanup: Bob.api.getOption('iBooksCleanup'), };
let res = _translate(str, params);

res
Expand Down
11 changes: 10 additions & 1 deletion src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface QueryOption {
cache?: string;
tld?: string;
timeout?: number;
iBooksCleanup?: string;
}

var resultCache = new Bob.CacheResult('translate-result');
Expand All @@ -22,11 +23,19 @@ var resultCache = new Bob.CacheResult('translate-result');
* @return {object} 一个符合 bob 识别的翻译结果对象
*/
async function _translate(text: string, options: QueryOption = {}): Promise<Bob.TranslateResult> {
const { from = 'auto', to = 'auto', cache = 'disable', tld = 'com', timeout = 10000 } = options;
const { from = 'auto', to = 'auto', cache = 'disable', tld = 'com', timeout = 10000, iBooksCleanup = 'disable' } = options;

const sourceLanguage = standardToNoStandard(from);
const targetLanguage = standardToNoStandard(to);

// 清理从 iBooks 中提取的内容
if (iBooksCleanup === 'enable') {
const matchText = text.match(/^([\s\S]*)\n+:/);
if (matchText && typeof matchText[1] !== "undefined") {
text = matchText[1];
}
}

const cacheKey = CryptoJS.MD5(`${text}${from}${to}`);
if (cache === 'enable') {
const _cacheData = resultCache.get(cacheKey);
Expand Down

0 comments on commit 090d8b7

Please sign in to comment.