Skip to content

Commit

Permalink
fix: fix order list rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Natumsol committed Aug 15, 2022
1 parent b866b0b commit 6b9238d
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 242 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ A small plugin aims to add space between Chinese Characters and English Alphabet

```diff
- 大多数人在20到30岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。
+ 大多数人在 20 到 30 岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。
+ 大多数人在 20 到 30 岁就已经过完自己的一生;一过了这个年龄段,他们就变成自己的影子。
```

## Manual installation

Download zip archive from [GitHub releases page](https://github.com/natumsol/obsidian-pangu/releases).
Extract the archive into `<vault>/.obsidian/plugins`.

Alternatively, using bash:

```bash
OBSIDIAN_VAULT_DIR=/path/to/your/obsidian/vault
mkdir -p $OBSIDIAN_VAULT_DIR/.obsidian/plugins
Expand All @@ -19,4 +22,4 @@ unzip ~/Downloads/obsidian-pangu_v1.1.0.zip -d $OBSIDIAN_VAULT_DIR/.obsidian/plu

### 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)
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)
144 changes: 72 additions & 72 deletions formatUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ interface IgnoreBlock {
export default {
condenseContent(content: string): string {
// 将 制表符 改成 四个空格
content = content.replace(/\t/g, " ");
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");
content = content.replace(/(\n){3,}/g, '$1$1');
content = content.replace(/(\r\n){3,}/g, '$1$1');
return content;
},
getIgnoreBlocks(lines: string[], token: string = "```"): IgnoreBlock[] {
getIgnoreBlocks(lines: string[], token: string = '```'): IgnoreBlock[] {
const ignoreBlocks: IgnoreBlock[] = [];
let block: IgnoreBlock | null = null;
lines.forEach((line, index) => {
Expand All @@ -34,71 +34,71 @@ export default {
},
deleteSpaces(content: string): string {
// 去掉「`()[]{}<>'"`」: 前后多余的空格
content = content.replace(/\s+([\(\)\[\]\{\}<>'":])\s+/g, " $1 ");
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 ');
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"
'$1$2$3$4'
);
content = content.replace(
/([`\$])\s*([^\$]*)([>\)\]\}])\s*([`\$])/g,
"$1$2$3$4"
'$1$2$3$4'
);

// 去掉「`) _`」、「`) ^`」增加的空格
content = content.replace(/\)\s([_\^])/g, ")$1");
content = content.replace(/\)\s([_\^])/g, ')$1');

// 去掉 [^footnote,2002] 中的空格
content = content.replace(/\[\s*\^([^\]\s]*)\s*\]/g, "[^$1]");
content = content.replace(/\[\s*\^([^\]\s]*)\s*\]/g, '[^$1]');

// 将链接的格式中文括号“[]()”改成英文括号“[]()”,去掉增加的空格
content = content.replace(
/\s*\[\s*([^\]]+)\s*\]\s*[((]\s*([^\s\)]*)\s*[))]\s*/g,
" [$1]($2) "
' [$1]($2) '
);

// 将图片链接的格式中的多余空格“! []()”去掉,变成“![]()”
content = content.replace(
/!\s*\[\s*([^\]]+)\s*\]\s*[((]\s*([^\s\)]*)\s*[))]\s*/g,
"![$1]($2) "
'![$1]($2) '
);

// 将网络地址中“ : // ”符号改成“:https://”
content = content.replace(/\s*:\s*\/\s*\/\s*/g, ":https://");
content = content.replace(/\s*:\s*\/\s*\/\s*/g, ':https://');

// 去掉行末空格
content = content.replace(/(\S*)\s*$/g, "$1");
content = content.replace(/(\S*)\s*$/g, '$1');

// 去掉「123 °」和 「15 %」中的空格
content = content.replace(/([0-9])\s*([°%])/g, "$1$2");
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");
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(/([0-9])\s*,\s*([0-9])/g, '$1,$2');

// 全角標點與其他字符之間不加空格
// 将无序列表的-后面的空格保留
// 将有序列表的-后面的空格保留
content = content.replace(
/^(?<![-|\d.]\s*)\s*([,。、《》?『』「」;∶【】{}—!@¥%…()])\s*/g,
"$1"
'$1'
);
return content;
},
Expand All @@ -108,18 +108,18 @@ export default {
// 在 “中文123” 之间加入空格 “中文 123”
content = content.replace(
/(?<!\[.*\]\(.*)([\u4e00-\u9fa5\u3040-\u30FF])([a-zA-Z0-9`])/g,
"$1 $2"
'$1 $2'
);

// 在 “English中文” 之间加入空格 “English 中文”
// 在 “123中文” 之间加入空格 “123 中文”
content = content.replace(
/(?<!\[.*\]\(.*)([a-zA-Z0-9%`])([*]*[\u4e00-\u9fa5\u3040-\u30FF])/g,
"$1 $2"
'$1 $2'
);

// 在 「I said:it's a good news」的冒号与英文之间加入空格 「I said: it's a good news」
content = content.replace(/([:])\s*([a-zA-z])/g, "$1 $2");
content = content.replace(/([:])\s*([a-zA-z])/g, '$1 $2');

return content;
},
Expand All @@ -128,56 +128,56 @@ export default {
// 必须在结尾或者有空格的点才被改成句号
content = content.replace(
/([\u4e00-\u9fa5\u3040-\u30FF])\.($|\s*)/g,
"$1。"
'$1。'
);
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]),/g, "$1,");
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]);/g, "$1;");
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])!/g, "$1!");
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\?/g, "$1?");
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\\/g, "$1、");
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\s*\:/g, "$1:");
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]),/g, '$1,');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF]);/g, '$1;');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])!/g, '$1!');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\?/g, '$1?');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\\/g, '$1、');
content = content.replace(/([\u4e00-\u9fa5\u3040-\u30FF])\s*\:/g, '$1:');

// 簡體中文使用直角引號
content = content.replace(//g, "『");
content = content.replace(//g, "』");
content = content.replace(//g, "「");
content = content.replace(//g, "」");
content = content.replace(//g, '『');
content = content.replace(//g, '』');
content = content.replace(//g, '「');
content = content.replace(//g, '」');
// 括号使用半角标点
// 半角括号的两边都有空格就不在这里处理了,放到行中处理
content = content.replace(/\s*[((]\s*/g, " ( ");
content = content.replace(/\s*[))]\s*/g, " ) ");
content = content.replace(/\s*[((]\s*/g, ' ( ');
content = content.replace(/\s*[))]\s*/g, ' ) ');

// 英文和数字内部的全角标点 `,。;‘’“”:?!@#%&-=+{}【】|\~`改成半角标点
content = content.replace(/(\w)\s*,\s*(\w)/g, "$1, $2");
content = content.replace(/(\w)\s*。\s*(\w)/g, "$1. $2");
content = content.replace(/(\w)\s*;\s*(\w)/g, "$1; $2");
content = content.replace(/(\w)\s*,\s*(\w)/g, '$1, $2');
content = content.replace(/(\w)\s*。\s*(\w)/g, '$1. $2');
content = content.replace(/(\w)\s*;\s*(\w)/g, '$1; $2');
content = content.replace(/(\w)\s*‘\s*(\w)/g, "$1 '$2");
content = content.replace(/(\w)\s*’\s*(\w)/g, "$1' $2");
content = content.replace(/(\w)\s*“\s*(\w)/g, '$1 "$2');
content = content.replace(/(\w)\s*”\s*(\w)/g, '$1" $2');
content = content.replace(/(\w)\s*:\s*(\w)/g, "$1: $2");
content = content.replace(/(\w)\s*?\s*(\w)/g, "$1? $2");
content = content.replace(/(\w)\s*!\s*(\w)/g, "$1! $2");
content = content.replace(/(\w)\s*@\s*(\w)/g, "$1@$2");
content = content.replace(/(\w)\s*#\s*(\w)/g, "$1#$2");
content = content.replace(/(\w)\s*%\s*(\w)/g, "$1 % $2");
content = content.replace(/(\w)\s*&\s*(\w)/g, "$1 & $2");
content = content.replace(/(\w)\s*-\s*(\w)/g, "$1 - $2");
content = content.replace(/(\w)\s*=\s*(\w)/g, "$1 = $2");
content = content.replace(/(\w)\s*+\s*(\w)/g, "$1 + $2");
content = content.replace(/(\w)\s*{\s*(\w)/g, "$1 {$2");
content = content.replace(/(\w)\s*}\s*(\w)/g, "$1} $2");
content = content.replace(/(\w)\s*[【\[]\s*(\w)/g, "$1 [$2");
content = content.replace(/(\w)\s*[】\]]\s*(\w)/g, "$1] $2");
content = content.replace(/(\w)\s*|\s*(\w)/g, "$1 | $2");
content = content.replace(/(\w)\s*\\s*(\w)/g, "$1 $2");
content = content.replace(/(\w)\s*~\s*(\w)/g, "$1~$2");
content = content.replace(/(\w)\s*:\s*(\w)/g, '$1: $2');
content = content.replace(/(\w)\s*?\s*(\w)/g, '$1? $2');
content = content.replace(/(\w)\s*!\s*(\w)/g, '$1! $2');
content = content.replace(/(\w)\s*@\s*(\w)/g, '$1@$2');
content = content.replace(/(\w)\s*#\s*(\w)/g, '$1#$2');
content = content.replace(/(\w)\s*%\s*(\w)/g, '$1 % $2');
content = content.replace(/(\w)\s*&\s*(\w)/g, '$1 & $2');
content = content.replace(/(\w)\s*-\s*(\w)/g, '$1 - $2');
content = content.replace(/(\w)\s*=\s*(\w)/g, '$1 = $2');
content = content.replace(/(\w)\s*+\s*(\w)/g, '$1 + $2');
content = content.replace(/(\w)\s*{\s*(\w)/g, '$1 {$2');
content = content.replace(/(\w)\s*}\s*(\w)/g, '$1} $2');
content = content.replace(/(\w)\s*[【\[]\s*(\w)/g, '$1 [$2');
content = content.replace(/(\w)\s*[】\]]\s*(\w)/g, '$1] $2');
content = content.replace(/(\w)\s*|\s*(\w)/g, '$1 | $2');
content = content.replace(/(\w)\s*\\s*(\w)/g, '$1 $2');
content = content.replace(/(\w)\s*~\s*(\w)/g, '$1~$2');
// 连续三个以上的 `。` 改成 `......`
content = content.replace(/[。]{3,}/g, "……");
content = content.replace(/[。]{3,}/g, '……');
// 截断连续超过一个的 ?和! 为一个,「!?」也算一个
content = content.replace(/([!?]+)\1{1,}/g, "$1");
content = content.replace(/([!?]+)\1{1,}/g, '$1');
// 截断连续超过一个的 。,;:、“”『』〖〗《》 为一个
content = content.replace(/([。,;:、“”『』〖〗《》【】])\1{1,}/g, "$1");
content = content.replace(/([。,;:、“”『』〖〗《》【】])\1{1,}/g, '$1');
return content;
},

Expand All @@ -198,7 +198,7 @@ export default {
content = this.condenseContent(content);

// 每行操作
const lines = content.split("\n");
const lines = content.split('\n');

const ignoreBlocks: IgnoreBlock[] = this.getIgnoreBlocks(lines);

Expand All @@ -219,20 +219,20 @@ export default {
// 插入必要的空格
line = this.insertSpace(line);

// 将有编号列表的“1. ”改成 “1.
line = line.replace(/^(\s*)(\d\.)\s+(\S)/, "$1$2 $3");
// 将有编号列表的“1. ”改成 “1. ”
line = line.replace(/^(\s*)(\d\.)\s+(\S)/, '$1$2 $3');

// 将无编号列表的“* ”改成 “- ”
// 将无编号列表的“- ”改成 “- ”
line = line.replace(/^(\s*)[-\*]\s+(\S)/, "$1- $2");
line = line.replace(/^(\s*)[-\*]\s+(\S)/, '$1- $2');

return line;
})
.join("\n");
.join('\n');

// 结束文档整理前再删除最后一个回车
content = content.replace(/(\n){2,}$/g, "$1");
content = content.replace(/(\r\n){2,}$/g, "$1");
content = content.replace(/(\n){2,}$/g, '$1');
content = content.replace(/(\r\n){2,}$/g, '$1');
return content;
},
};
19 changes: 14 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, MarkdownView } from 'obsidian';
import { App, Plugin, PluginSettingTab, Setting, MarkdownView } from 'obsidian';
import formatUtil from './formatUtil';

export default class Pangu extends Plugin {
Expand All @@ -20,7 +20,10 @@ export default class Pangu extends Plugin {
// 保持光标格式化后不变
const newDocLine = cm.getLine(cursor.line);
try {
cursor = { ...cursor, ch: newDocLine.indexOf(cursorContent) + cursorContent.length };
cursor = {
...cursor,
ch: newDocLine.indexOf(cursorContent) + cursorContent.length,
};
} catch (error) {}

cm.setCursor(cursor);
Expand All @@ -31,9 +34,11 @@ export default class Pangu extends Plugin {
id: 'pangu-format',
name: '为中英文字符间自动加入空格',
callback: () => {
const activeLeafView = this.app.workspace.getActiveViewOfType(MarkdownView);
const activeLeafView =
this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeLeafView) {
this.format(activeLeafView.sourceMode.cmEditor);
// @ts-ignore
this.format(activeLeafView?.sourceMode?.cmEditor);
}
},
hotkeys: [
Expand Down Expand Up @@ -70,6 +75,10 @@ class PanguSettingTab extends PluginSettingTab {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Pangu 使用说明' });
new Setting(containerEl).setName('').setDesc('默认快捷键为:Mac - Command + Shift + S,Windows - Shift + Ctrl + S。当然,您可以到「设置 - 快捷键」里进行更改。');
new Setting(containerEl)
.setName('')
.setDesc(
'默认快捷键为:Mac - Command + Shift + S,Windows - Shift + Ctrl + S。当然,您可以到「设置 - 快捷键」里进行更改。'
);
}
}
Loading

0 comments on commit 6b9238d

Please sign in to comment.