Skip to content

Commit

Permalink
Chinese-punctuation: only remove spaces between Chinese characters …
Browse files Browse the repository at this point in the history
…and punctuations rather than all spaces
  • Loading branch information
Benature committed Mar 11, 2024
1 parent 2d30e89 commit b9bf089
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 87 deletions.
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

| 命令 | 描述 |
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| 将选中文本中的**多余空格**移除 | 确保单词之间只有一个空格 |
| 将选中文本中的**多余空格**移除 | 确保英文单词之间只有一个空格 |
| 将选中文本中的**所有空格**移除 | 删除所有空格 |
| 将选中文本中的**行末空格**移除 | 删除行末空格([#61](https://github.com/Benature/obsidian-text-format/issues/61)|
| 将选中文本中的**空行移除** |`\n\n` 替换为 `\n` |
Expand Down
104 changes: 24 additions & 80 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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, "。"))
Expand All @@ -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)");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,8 @@ export const GreekLetters: { [key: string]: string } = {
'Φ': '\\Phi',
'Χ': '\\Chi',
'Ψ': '\\Psi',
'Ω': '\\Omega'
'Ω': '\\Omega',
' ̃': '\\tilde ',
'∞': '\\infty',
'≠': '\\neq',
};
2 changes: 1 addition & 1 deletion src/settings/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b9bf089

Please sign in to comment.