Skip to content

Commit f5df14c

Browse files
authoredJul 22, 2024
Fixing #13 to replace a pattern within parenthesis (#14)
* update main.ts and package.json * docs: update CHANGELOG.md
1 parent 65c4519 commit f5df14c

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### 1.0.6 July 22, 2024
2+
- Update pattern matching to replace pattern surrounded by parenthesis without including the parenthesis
13
### 1.0.5 Apr 26, 2024
24
- Removed heading from Plugin settings page
35
- Removed unnecessary CSS code

‎main.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,48 @@ export default class LinkMagicPlugin extends Plugin {
5656
if (RegExp("(\[.*\]\(.*\))", "g").test(word)) {
5757
return
5858
}
59+
5960
Object.values(this.settings)
6061
.filter(({ pattern, link }) => pattern && link)
6162
.some(({ pattern, link }) => {
62-
if (RegExp(pattern, "g").test(word)) {
63-
let markdownLink = `[${word}](${link.replace("{pattern}", word)})`
64-
editor.replaceRange(markdownLink, { line: curPos.line, ch: curPos.ch - word.length }, curPos);
63+
const regex = RegExp(pattern, "g")
64+
if (regex.test(word)) {
65+
const enclosingRegex = /^(\()?(.*?)(\))?$/
66+
let match = enclosingRegex.exec(word)
67+
if (match) {
68+
const startChar = match[1] || ""
69+
const innerWord = match[2]
70+
const endChar = match[3] || ""
71+
let markdownLink = `${startChar}[${innerWord}](${link.replace("{pattern}", innerWord)})${endChar}`
72+
editor.replaceRange(markdownLink, { line: curPos.line, ch: curPos.ch - word.length }, curPos);
73+
}
6574
}
6675
})
6776
}
6877

6978
handleEnter(editor: Editor, pos?: EditorPosition) {
70-
//Get the current line position
79+
// Get the current line position
7180
let curPos = pos ? pos : editor.getCursor()
72-
//Don't think we need to check if we're on the first line as this should only be called after enter
81+
// Don't think we need to check if we're on the first line as this should only be called after enter
7382
let lineAbove = editor.getLine(curPos.line - 1)
7483
this.checkForMatch(editor, { line: curPos.line - 1, ch: lineAbove.length })
7584
}
7685

86+
handleTab(editor: Editor, pos?: EditorPosition) {
87+
//Remove all the white space
88+
let curPos = pos ? pos : editor.getCursor()
89+
editor.getLine(curPos.line).trimStart()
90+
91+
}
92+
7793
triggerSnippet(editor: Editor, evt: KeyboardEvent) {
7894
switch (evt.key) {
7995
case " ": {
8096
this.checkForMatch(editor)
8197
break;
8298
}
8399
case "Tab": {
84-
// TODO: Add support for Tab replacement
100+
this.handleTab(editor)
85101
break;
86102
}
87103
case "Enter": {

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-sample-plugin",
3-
"version": "1.0.0",
3+
"version": "1.0.6",
44
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {
@@ -21,4 +21,4 @@
2121
"tslib": "2.4.0",
2222
"typescript": "4.7.4"
2323
}
24-
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.