From b9bf089c3e4a790ad9877b8653c74adc1ce71534 Mon Sep 17 00:00:00 2001 From: Benature Date: Mon, 11 Mar 2024 16:07:37 +0800 Subject: [PATCH] `Chinese-punctuation`: only remove spaces between Chinese characters and punctuations rather than all spaces --- CHANGELOG.md | 13 +++-- README_ZH.md | 2 +- main.ts | 104 +++++++++---------------------------- manifest.json | 2 +- src/presets.ts | 5 +- src/settings/settingTab.ts | 2 +- 6 files changed, 41 insertions(+), 87 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dedd5ac..9c380e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,16 @@ ## 🏗️ developed > to be updated in the next version -# 2.7.0 -- [updated] `Format bullet list`: keep selecting whole paragraphs after formatting rather than set cursor at the end. -- [notice] refactoring i18n, but zh-tw language will be deprecated later becasue of the limitation of developing time. +## 2.7.0 + +- [updated] `Chinese-punctuation`: only remove spaces between Chinese characters and punctuations rather than all spaces +- [updated] `Format bullet list` + - Keep selecting whole paragraphs after formatting rather than set cursor at the end. + - Ensure first line starts with `- ` if multiple lines are selected + +*2.7.0-b1* + +- [notice] refactoring i18n, but zh-tw language will be deprecated later because of the limitation of developing time. - [feature] support [#68](https://github.com/Benature/obsidian-text-format/issues/68) - [updated] `Convert wikiLinks to plain markdown links in selection`: Three path modes - absolute diff --git a/README_ZH.md b/README_ZH.md index 22afc50..f23bc45 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -86,7 +86,7 @@ | 命令 | 描述 | | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | -| 将选中文本中的**多余空格**移除 | 确保单词之间只有一个空格 | +| 将选中文本中的**多余空格**移除 | 确保英文单词之间只有一个空格 | | 将选中文本中的**所有空格**移除 | 删除所有空格 | | 将选中文本中的**行末空格**移除 | 删除行末空格([#61](https://github.com/Benature/obsidian-text-format/issues/61)) | | 将选中文本中的**空行移除** | 将 `\n\n` 替换为 `\n` | diff --git a/main.ts b/main.ts index 8a876d6..a1fe11f 100644 --- a/main.ts +++ b/main.ts @@ -73,65 +73,6 @@ export default class TextFormat extends Plugin { }, }); }) - - // this.addCommand({ - // id: "lowercase", - // name: { en: "Lowercase selected text", zh: "将选中文本转换为小写", "zh-TW": "將選取文字轉換為小寫" }[lang], - // callback: () => { - // this.formatEditorOrTitle("lowercase"); - // }, - // }); - // this.addCommand({ - // id: ":private:lowercase", - // name: "Lowercase in editor", - // editorCheckCallback: (checking: boolean, editor: Editor, view: MarkdownView) => { - // if (!checking) { this.textFormat(editor, view, "lowercase"); } - // return !checking; - // }, - // }); - // this.addCommand({ - // id: "uppercase", - // name: { en: "Uppercase selected text", zh: "将选中文本转换为大写", "zh-TW": "將選取文字轉換為大寫" }[lang], - // callback: () => { - // this.formatEditorOrTitle("uppercase"); - // } - // }) - // this.addCommand({ - // id: ":private:uppercase", - // name: "Uppercase in editor", - // editorCheckCallback: (checking: boolean, editor: Editor, view: MarkdownView) => { - // if (!checking) { this.textFormat(editor, view, "uppercase"); } - // return !checking; - // }, - // }); - // this.addCommand({ - // id: "capitalize-word", - // name: { en: "Capitalize all words in selected text", zh: "将选中文本中的所有单词首字母大写", "zh-TW": "將選取文字中的所有單詞首字母大寫" }[lang], - // editorCallback: (editor: Editor, view: MarkdownView) => { - // this.editorTextFormat(editor, view, "capitalize-word"); - // }, - // }); - // this.addCommand({ - // id: "capitalize-sentence", - // name: { en: "Capitalize only first word of sentence in selected text", zh: "将选中文本中的句子的首字母大写", "zh-TW": "將選取文字中的句子的首字母大寫" }[lang], - // editorCallback: (editor: Editor, view: MarkdownView) => { - // this.editorTextFormat(editor, view, "capitalize-sentence"); - // }, - // }); - // this.addCommand({ - // id: "titlecase", - // name: { en: "Title case selected text", zh: "将选中文本转换为标题格式大小写", "zh-TW": "將選取文字轉換為標題格式大小寫" }[lang], - // editorCallback: (editor: Editor, view: MarkdownView) => { - // this.editorTextFormat(editor, view, "titlecase"); - // }, - // }); - // this.addCommand({ - // id: "togglecase", - // name: { en: "Togglecase selected text", zh: "触发选中文本大小写切换", "zh-TW": "觸發選取文字大小寫切換" }[lang], - // editorCallback: (editor: Editor, view: MarkdownView) => { - // this.editorTextFormat(editor, view, "togglecase"); - // }, - // }); this.addCommand({ id: "slugify", name: { en: "Slugify selected text (`-` for space)", zh: "使用 Slugify 格式化选中文本(`-`连字符)", "zh-TW": "使用 Slugify 格式化選取文字(`-`連字符)" }[lang], @@ -532,20 +473,6 @@ export default class TextFormat extends Plugin { case "remove-citation": replacedText = selectedText.replace(/\[\d+\]|【\d+】/g, "").replace(/ +/g, " "); break; - case "bullet": - let r = this.settings.BulletPoints.replace("-", ""); - replacedText = selectedText - .replace(RegExp(`\\s*[${r}] *`, "g"), (t) => - t.replace(RegExp(`[${r}] *`), "\n- ") - ) - .replace(/\n[~\/Vv] /g, "\n- ") - .replace(/\n+/g, "\n") - .replace(/^\n/, ""); - // if "-" in this.settings.BulletPoints - if (this.settings.BulletPoints.indexOf("-") > -1) { - replacedText = replacedText.replace(/^- /g, "\n- "); - } - break; case "convert-ordered": let orderedCount = 0; // var rx = new RegExp( @@ -578,9 +505,6 @@ export default class TextFormat extends Plugin { break; case "Chinese-punctuation": replacedText = selectedText; - if (this.settings.RemoveBlanksWhenChinese) { - replacedText = removeAllSpaces(selectedText).replace(/[\u4e00-\u9fa5【】()「」《》:“?‘、](\s+)[\u4e00-\u9fa5【】()「」《》:“?‘、]/g, ""); - } replacedText = replacedText .replace(/ ?, ?/g, ",") .replace(/(?:[^\d])( ?\. ?)/g, (t, t1) => t.replace(t1, "。")) @@ -590,9 +514,11 @@ export default class TextFormat extends Plugin { .replace(/[^a-zA-Z0-9](: ?)/g, (t, t1) => t.replace(t1, ":")) .replace(/\!(?=[^\[])/g, "!") .replace(/\?/g, "?") - .replace(/[\((][^\)]*?[\u4e00-\u9fa5]+?[^\)]*?[\))]/g, function (t) { - return `(${t.slice(1, t.length - 1)})`; - }); + .replace(/[\((][^\)]*?[\u4e00-\u9fa5]+?[^\)]*?[\))]/g, (t) => `(${t.slice(1, t.length - 1)})`); + if (this.settings.RemoveBlanksWhenChinese) { + replacedText = replacedText.replace( + /(?<=[\u4e00-\u9fa5【】()「」《》:“?‘、;])(\s+)(?=[\u4e00-\u9fa5【】()「」《》:“?‘、;])/g, ""); + } break; case "English-punctuation": replacedText = selectedText.replace(/[(\(]([\w !\"#$%&'()*+,-./:;<=>?@\[\\\]^_`{\|}~]+)[)\)]/g, "($1)"); @@ -725,7 +651,7 @@ export default class TextFormat extends Plugin { break; } - //: Modify selected text + //: MODIFY SELECTION let replacedText = this.textFormat(selectedText, cmd, args); if (replacedText === null) { switch (cmd) { @@ -763,6 +689,24 @@ export default class TextFormat extends Plugin { } } break; + case "bullet": + let r = this.settings.BulletPoints.replace("-", ""); + replacedText = selectedText + .replace(RegExp(`\\s*[${r}] *`, "g"), (t) => + t.replace(RegExp(`[${r}] *`), "\n- ") + ) + .replace(/\n[~\/Vv] /g, "\n- ") + .replace(/\n+/g, "\n") + .replace(/^\n/, ""); + // if "-" in this.settings.BulletPoints + if (this.settings.BulletPoints.indexOf("-") > -1) { + replacedText = replacedText.replace(/^- /g, "\n- "); + } + // if select multi-paragraphs, add `- ` to the beginning + if (selectedText.indexOf("\n") > -1 && replacedText.slice(0, 2) != "- ") { + replacedText = "- " + replacedText; + } + break; default: Error("Unknown command") return; diff --git a/manifest.json b/manifest.json index 17d0a30..78d965c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-text-format", "name": "Text Format", - "version": "2.6.0", + "version": "2.7.0", "minAppVersion": "0.9.7", "description": "Format text such as lowercase/uppercase/capitalize/titlecase, converting order/bullet list, removing redundant spaces/newline characters.", "author": "Benature", diff --git a/src/presets.ts b/src/presets.ts index 09615b0..dfe7957 100644 --- a/src/presets.ts +++ b/src/presets.ts @@ -111,5 +111,8 @@ export const GreekLetters: { [key: string]: string } = { 'Φ': '\\Phi', 'Χ': '\\Chi', 'Ψ': '\\Psi', - 'Ω': '\\Omega' + 'Ω': '\\Omega', + ' ̃': '\\tilde ', + '∞': '\\infty', + '≠': '\\neq', }; \ No newline at end of file diff --git a/src/settings/settingTab.ts b/src/settings/settingTab.ts index 1aea0ff..3de86d5 100644 --- a/src/settings/settingTab.ts +++ b/src/settings/settingTab.ts @@ -463,7 +463,7 @@ export class TextFormatSettingTab extends PluginSettingTab { ); new Setting(this.contentEl) - .setName("Remove all spaces when converting Chinese punctuation marks") + .setName("Remove spaces when converting Chinese punctuation marks") .setDesc("for OCR case") .addToggle((toggle) => { toggle