diff --git a/README.md b/README.md index 3801fca..711d248 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # bobplugin-google-translate -> 这是一个 [Bob](https://ripperhe.gitee.io/bob/#/) 的一个 google 翻译插件 +> 这是 [Bob](https://ripperhe.gitee.io/bob/#/) 的一个 google 翻译插件 ## 特性 diff --git a/images/preview-result.png b/images/preview-result.png index 06110dd..e47e519 100644 Binary files a/images/preview-result.png and b/images/preview-result.png differ diff --git a/src/libs/human-string.js b/src/libs/human-string.js index 41bbdae..7aa58d5 100644 --- a/src/libs/human-string.js +++ b/src/libs/human-string.js @@ -14,11 +14,12 @@ var humanizeString = (input) => { if (typeof _input !== 'string') { throw new TypeError('Expected a string'); } - - _input = decamelize(_input); + _input = decamelize(_input, ''); _input = _input - .replace(/[_-]+/g, ' ') - .replace(/\s{2,}/g, ' ') + .replace(/[]+/g, ' ') + .replace(/([_-])+/g, ' $1 ') + // https://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines#answer-3469155:~:text=Use%20a%20double%2Dnegative + .replace(/[^\S\r\n]{2,}/g, ' ') .trim(); _input = `${_input.charAt(0).toUpperCase()}${_input.slice(1)}`; diff --git a/src/main.ts b/src/main.ts index ccc3612..50be534 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import Bob from './bob'; import _lang from './lang'; import GoogleTranslate from './google-translate'; -var humanizeString = require('./libs/human-string'); +var formatString = require('./libs/human-string'); export function supportLanguages(): Bob.supportLanguages { return [..._lang]; @@ -11,7 +11,7 @@ export function supportLanguages(): Bob.supportLanguages { // https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate export function translate(query: Bob.TranslateQuery, completion: Bob.Completion) { const { text = '', detectFrom, detectTo } = query; - const str = humanizeString(text); + const str = formatString(text); const params = { from: detectFrom, to: detectTo, tld: Bob.getOption('tld'), cache: Bob.getOption('cache') }; GoogleTranslate._translate(str, params) .then((result) => completion({ result }))