Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
No3371 committed May 8, 2021
2 parents 4b6c9ec + ada4009 commit ad17b77
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 44 deletions.
122 changes: 84 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## Regex Pipeline
# Regex Pipeline

Regex Pipeline is a [obsidian](https://obsidian.md/) plugin that allows users setup custom regex rules to automatically format notes, this is especially useful in scenerios like building personal knowledge database.
Regex Pipeline is a [obsidian](https://obsidian.md/) plugin that allows users to setup custom regex rules to automatically format notes, this is especially useful in scenerios like building personal knowledge database, because you often clip webpage from same sources.

![](https://i.imgur.com/KxLwG89.gif)
![](https://i.imgur.com/6TL8okh.gif)

## Usage
First of all, enable the plugin, a file named index.txt should be created at `.obsidian/regex-rulesets/`. Due to how obsidian protects your disks, you have to specified what ruleset files are there to be read, that's why we need a index file.
First of all, enable the plugin, a file named index.txt should be created at `.obsidian/regex-rulesets/`. Due to how Obsidian protects your disks, you have to specify what ruleset files are there to be read, that's why we need a index file.

#### Writing Rulesets
Now you can start editing your own rule sets.
Expand All @@ -26,7 +26,7 @@ By default, `gm` (multiline) flag is appended to the **SEARCH** regex, you can o
"SEARCH"gmu->"REPLACE"
```

Noted that `gm` flags are bascially neccessary for this plugin to be useful, you seldom wants to only replace only 1 occurances or operate on a single line notes.
Noted that `gm` flags are bascially neccessary for this plugin to be useful, you seldom wants to replace only 1 occurances or operate on a note only contains 1 line.

#### Replace With Nothing
Due to how the plugin parse rules, the replacement string can not be a length zero string, if you want to delete with regex (replace with ""), you have to add a custom `x` flag:
Expand All @@ -43,9 +43,13 @@ After you saved your ruleset, you have to add the filename into the `index.txt`,

#### Applying Rulesets
Press the sidebar button of this plugin to show the rulesets menu, select your ruleset then it'll apply.
The menu is a command so you can also bind it to a shortcut.

**Note**: The plugin support applying rules to selection only, if anything is selected, only selection is modified!

## Examples
**Index.txt**
**index.txt**
.obsidian/regex-rulesets/index.txt
```
number-to-alphabet.txt
goo
Expand All @@ -67,43 +71,85 @@ goo
**[Goo 辞書 Formatter](dictionary.goo.ne.jp/word/彷徨く/)** (Gif above)
.obsidian/regex-rulesets/goo
```
"\!\[\]\(https://dictionary\.goo\.ne\.jp/img/daijisen/gaiji/02539\.gif\)"->"![[@1.gif]]"
"\!\[\]\(https://dictionary\.goo\.ne\.jp/img/daijisen/gaiji/02540\.gif\)"->"![[@2.gif]]"
"\!\[\]\(https://dictionary\.goo\.ne\.jp/img/daijisen/gaiji/02541\.gif\)"->"![[@3.gif]]"
"\!\[\]\(https://dictionary\.goo\.ne\.jp/img/daijisen/gaiji/02542\.gif\)"->"![[@4.gif]]"
"\!\[\]\(https://dictionary\.goo\.ne\.jp/img/daijisen/gaiji/02543\.gif\)"->"![[@5.gif]]"
"\!\[\]\(https://dictionary\.goo\.ne\.jp/img/daijisen/gaiji/02544\.gif\)"->"![[@6.gif]]"
::This fix example sentence right after a topic
"[(.+?)](.+)\n\n1\. 1\. 「"->"[$1]$2
>「"
::This extract entry name from the title
"^#\s.+goo国語辞書\n+(.+)\n+の解説\n+\-+"->"# $1"
::This extract titles for sub entries2
"(.+?)\n\nの解説\n+?\-+"->"---
# $1"
:: This convert these lines to headers
"^類語"->"#### 類語"
"^関連語"->"#### 関連語"
" [\n\s]? 1\.\s+"->" >"
"^下接句"->"#### 下接句"
:: Convert all second level single example sentence
" [\n\s]? 1\.\s+「(?!.+\n\s+?2\.)"->" >「"
:: These 2 fix arrow reference
" >→\[(.+?)\]\((.+)\)[(.+?)]"->" →[$1]($2)[$3]"
" 1\. →\[(.+?)\]\((.+)\)[(.+?)]"->" →[$1]($2)[$3]"
:: This fix \n\n → to \n →
"[\n\s]+?(?= →\[.+?\]\(.+\)[.+?])"->"
"
"^ 「"->" >「"
:: This fix wrong bullets captured by [MardDownload](github.com/deathau/markdownload)
"^1. \*\*1\*\*"->"1. "
"^1. \*\*2\*\*"->"2. "
"^1. \*\*3\*\*"->"3. "
"^1. \*\*4\*\*"->"4. "
"^1. \*\*5\*\*"->"5. "
"^1. \*\*6\*\*"->"6. "
"^1. \*\*7\*\*"->"7. "
"^1. \*\*8\*\*"->"8. "
"^1. \*\*9\*\*"->"9. "
"^1. \*\*10\*\*"->"10. "
"^1. \*\*11\*\*"->"11. "
"^1. \*\*12\*\*"->"12. "
"^1. \*\*13\*\*"->"13. "
"^1. \*\*14\*\*"->"14. "
"^1. \*\*15\*\*"->"15. "
"^1. \*\*16\*\*"->"16. "
"^1. \*\*17\*\*"->"17. "
"^1. \*\*18\*\*"->"18. "
"^1. \*\*19\*\*"->"19. "
"^1. \*\*20\*\*"->"20. "
"^1. \*\*21\*\*"->"21. "
"^1. \*\*22\*\*"->"22. "
"^1. \*\*23\*\*"->"23. "
"^1. \*\*24\*\*"->"24. "
"^1. \*\*25\*\*"->"25. "
"^1. \*\*26\*\*"->"26. "
"^1. \*\*27\*\*"->"27. "
"^1. \*\*28\*\*"->"28. "
"^1. \*\*29\*\*"->"29. "
"^1. \*\*30\*\*"->"30. "
"^1\. \*\*1\*\*"->"1. "
"^1\. \*\*2\*\*"->"2. "
"^1\. \*\*3\*\*"->"3. "
"^1\. \*\*4\*\*"->"4. "
"^1\. \*\*5\*\*"->"5. "
"^1\. \*\*6\*\*"->"6. "
"^1\. \*\*7\*\*"->"7. "
"^1\. \*\*8\*\*"->"8. "
"^1\. \*\*9\*\*"->"9. "
"^1\. \*\*10\*\*"->"10. "
"^1\. \*\*11\*\*"->"11. "
"^1\. \*\*12\*\*"->"12. "
"^1\. \*\*13\*\*"->"13. "
"^1\. \*\*14\*\*"->"14. "
"^1\. \*\*15\*\*"->"15. "
"^1\. \*\*16\*\*"->"16. "
"^1\. \*\*17\*\*"->"17. "
"^1\. \*\*18\*\*"->"18. "
"^1\. \*\*19\*\*"->"19. "
"^1\. \*\*20\*\*"->"20. "
"^1\. \*\*21\*\*"->"21. "
"^1\. \*\*22\*\*"->"22. "
"^1\. \*\*23\*\*"->"23. "
"^1\. \*\*24\*\*"->"24. "
"^1\. \*\*25\*\*"->"25. "
"^1\. \*\*26\*\*"->"26. "
"^1\. \*\*27\*\*"->"27. "
"^1\. \*\*28\*\*"->"28. "
"^1\. \*\*29\*\*"->"29. "
"^1\. \*\*30\*\*"->"30. "
"^(\d+?)\. "->"$1. "
:: This fix 2 space after bullets
" (\d+?)\. "->" $1. "
::This clears empty lines.
"\n\s+?\n (\d+?)\. "->"
$1. "
::This clears empty lines.
"\n\s+?\n >「"->"
>「"
::This clears empty lines.
"^\s+\n(\d+)\."->"$1."
```
" (\d+?)\. 「"->" $1. >「"
```
11 changes: 7 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { App, BaseComponent, ButtonComponent, Component, MarkdownView, Modal, No

export default class RegexPipeline extends Plugin {
rules: string[]
pathToRulesets = this.app.vault.configDir + "/regex-rulesets/";
pathToRulesets = this.app.vault.configDir + "/regex-rulesets";
pathToIndex = this.app.vault.configDir + "/regex-rulesets/index.txt"

log (message?: any, ...optionalParams: any[])
Expand Down Expand Up @@ -46,8 +46,11 @@ export default class RegexPipeline extends Plugin {
}

async reloadRulesets() {
if (!this.app.vault.adapter.exists(this.pathToIndex))
if (!await this.app.vault.adapter.exists(this.pathToRulesets))
await this.app.vault.createFolder(this.pathToRulesets)
if (!await this.app.vault.adapter.exists(this.pathToIndex))
await this.app.vault.adapter.write(this.pathToIndex, "");

let p = this.app.vault.adapter.read(this.pathToIndex);
p.then(s => {
this.rules = s.split(/\r\n|\r|\n/);
Expand Down Expand Up @@ -112,13 +115,13 @@ class ApplyRuleSetMenu extends Modal {

onOpen() {
let {contentEl} = this;
contentEl.append(contentEl.createEl("h1", null, el => el.innerHTML = this.plugin.pathToRulesets + "..."));
contentEl.append(contentEl.createEl("h1", null, el => el.innerHTML = this.plugin.pathToRulesets + "/..."));
for (let i = 0; i < this.plugin.rules.length; i++)
{
new Setting(contentEl)
.setName(this.plugin.rules[i])
.addButton(btn => btn.onClick(async () => {
this.plugin.applyRuleset(this.plugin.pathToRulesets + this.plugin.rules[i]);
this.plugin.applyRuleset(this.plugin.pathToRulesets + "/" + this.plugin.rules[i])
this.close();
}).setButtonText("Apply"));
}
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-regex-pipeline",
"name": "Regex Pipeline",
"version": "1.0.4",
"version": "1.0.7",
"minAppVersion": "0.9.12",
"description": "Allows users setup custom regex rules to automatically format notes",
"author": "No3371",
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"1.0.4": "0.9.12"
"1.0.7": "0.9.12"
}

0 comments on commit ad17b77

Please sign in to comment.