From 4fcb8beba22c4f786842a3d0a2dc6b0d596bcf5b Mon Sep 17 00:00:00 2001 From: natumsol Date: Sat, 6 Mar 2021 15:04:51 +0800 Subject: [PATCH] feat: replace pangu.js to reg exp --- README.md | 22 +++--- formatUtil.ts | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.ts | 59 +++++++++----- package.json | 4 +- 4 files changed, 262 insertions(+), 31 deletions(-) create mode 100644 formatUtil.ts diff --git a/README.md b/README.md index ad825c0..8099b0e 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,22 @@ ## Obsidian Pangu Plugin -A small plugin aims to add space between Chinese Characters and English Alphabet, powered by [pangu.js](https://github.com/vinta/pangu.js). - -For Example: +A small plugin aims to add space between Chinese Characters and English Alphabet, For Example: ```diff - 大多数人在20到30岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。 + 大多数人在 20 到 30 岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。 ``` -### How to use - -- Clone this repo. -- `npm i` or `yarn` to install dependencies -- `npm run dev` to start compilation in watch mode. +## Manual installation +Download zip archive from [GitHub releases page](https://github.com/natumsol/obsidian-pangu/releases). +Extract the archive into `/.obsidian/plugins`. -### Manually installing the plugin +Alternatively, using bash: +```bash +OBSIDIAN_VAULT_DIR=/path/to/your/obsidian/vault +mkdir -p $OBSIDIAN_VAULT_DIR/.obsidian/plugins +unzip ~/Downloads/obsidian-pangu_v1.1.0.zip -d $OBSIDIAN_VAULT_DIR/.obsidian/plugins +``` -- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`. +### Thanks +Thanks to [pangu.vim](https://github.com/hotoo/pangu.vim), [writing4cn](https://marketplace.visualstudio.com/items?itemName=twocucao.writing4cn) and [pangu-markdown-vscode ](https://github.com/zhuyuanxiang/pangu-markdown-vscode) \ No newline at end of file diff --git a/formatUtil.ts b/formatUtil.ts new file mode 100644 index 0000000..eb620b9 --- /dev/null +++ b/formatUtil.ts @@ -0,0 +1,208 @@ +export default { + condenseContent(content: string): string { + // 将 制表符 改成 四个空格 + content = content.replace(/\t/g, " "); + + // 删除超过2个的回车 + // Unix 的只有 LF,Windows 的需要 CR LF + content = content.replace(/(\n){3,}/g, "$1$1"); + content = content.replace(/(\r\n){3,}/g, "$1$1"); + return content; + }, + deleteSpaces(content: string): string { + // 去掉「`()[]{}<>'"`」: 前后多余的空格 + content = content.replace(/\s+([\(\)\[\]\{\}<>'":])\s+/g, ' $1 '); + + // 去掉连续括号增加的空格,例如:「` ( [ { < > } ] ) `」 + content = content.replace(/([<\(\{\[])\s([<\(\{\[])\s/g, "$1$2 "); + content = content.replace(/([<\(\{\[])\s([<\(\{\[])\s/g, "$1$2 "); + content = content.replace(/([<\(\{\[])\s([<\(\{\[])\s/g, "$1$2 "); + content = content.replace(/([<\(\{\[])\s([<\(\{\[])\s/g, "$1$2 "); + content = content.replace(/\s([>\)\]\}])\s([>\)\]\}])/g, " $1$2"); + content = content.replace(/\s([>\)\]\}])\s([>\)\]\}])/g, " $1$2"); + content = content.replace(/\s([>\)\]\}])\s([>\)\]\}])/g, " $1$2"); + content = content.replace(/\s([>\)\]\}])\s([>\)\]\}])/g, " $1$2"); + + // 去掉 「`$ () $`」, 「`$ [] $`」, 「`$ {} $`」 里面增加的空格 + // 去掉开始 $ 后面增加的空格,结束 $ 前面增加的空格 + // 去掉包裹代码的符号里面增加的空格 + // 去掉开始 ` 后面增加的空格,结束 ` 前面增加的空格 + content = content.replace(/([`\$])\s*([<\(\[\{])([^\$]*)\s*([`\$])/g, "$1$2$3$4"); + content = content.replace(/([`\$])\s*([^\$]*)([>\)\]\}])\s*([`\$])/g, "$1$2$3$4"); + + // 去掉「`) _`」、「`) ^`」增加的空格 + content = content.replace(/\)\s([_\^])/g, ")$1"); + + // 去掉 [^footnote,2002] 中的空格 + content = content.replace(/\[\s*\^([^\]\s]*)\s*\]/g, "[^$1]"); + + // 将链接的格式中文括号“[]()”改成英文括号“[]()”,去掉增加的空格 + content = content.replace(/\s*\[\s*([^\]]+)\s*\]\s*[((]\s*([^\s\)]*)\s*[))]\s*/g, " [$1]($2) "); + + // 将图片链接的格式中的多余空格“! []()”去掉,变成“![]()” + content = content.replace(/!\s*\[\s*([^\]]+)\s*\]\s*[((]\s*([^\s\)]*)\s*[))]\s*/g, "![$1]($2) "); + + // 将网络地址中“ : // ”符号改成“://” + content = content.replace(/\s*:\s*\/\s*\/\s*/g, "://"); + + // 去掉行末空格 + content = content.replace(/(\S*)\s*$/g, '$1'); + + // 去掉「123 °」和 「15 %」中的空格 + content = content.replace(/([0-9])\s*([°%])/g, '$1$2'); + + // 去掉 2020 - 04 - 20, 08 : 00 : 00 这种日期时间表示的数字内的空格 + content = content.replace(/([0-9])\s*-\s*([0-9])/g, "$1-$2"); + content = content.replace(/([0-9])\s*:\s*([0-9])/g, "$1:$2"); + + // 去掉 1 , 234 , 567 这种千分位表示的数字内的空格 + content = content.replace(/([0-9])\s*,\s*([0-9])/g, "$1,$2"); + + // 全角標點與其他字符之間不加空格 + // 将无序列表的-后面的空格保留 + // 将有序列表的-后面的空格保留 + content = content.replace(/^(? { + // 中文内部使用全角标点 + line = formatUtil.replacePunctuations(line); + + // 删除多余的空格 + line = formatUtil.deleteSpaces(line); + + // 插入必要的空格 + line = formatUtil.insertSpace(line); + + // 将有编号列表的“1. ”改成 “1. ” + line = line.replace(/^(\s*)(\d\.)\s+(\S)/, '$1$2 $3'); + + // 将无编号列表的“* ”改成 “- ” + // 将无编号列表的“- ”改成 “- ” + line = line.replace(/^(\s*)[-\*]\s+(\S)/, '$1- $2'); + + return line; + }).join("\n"); + + // 结束文档整理前再删除最后一个回车 + content = content.replace(/(\n){2,}$/g, '$1'); + content = content.replace(/(\r\n){2,}$/g, '$1'); + + cm.setValue(content); cm.setCursor(cursor); } + async onload() { await this.loadSettings(); @@ -56,22 +95,6 @@ export default class MyPlugin extends Plugin { } } -class SampleModal extends Modal { - constructor(app: App) { - super(app); - } - - onOpen() { - let { contentEl } = this; - contentEl.setText('Woah!'); - } - - onClose() { - let { contentEl } = this; - contentEl.empty(); - } -} - class SampleSettingTab extends PluginSettingTab { plugin: MyPlugin; diff --git a/package.json b/package.json index 5efb133..aa394e7 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,5 @@ "tslib": "^2.0.3", "typescript": "^4.0.3" }, - "dependencies": { - "pangu": "^4.0.7" - } + "dependencies": {} } \ No newline at end of file