@@ -56,32 +56,48 @@ export default class LinkMagicPlugin extends Plugin {
56
56
if ( RegExp ( "(\[.*\]\(.*\))" , "g" ) . test ( word ) ) {
57
57
return
58
58
}
59
+
59
60
Object . values ( this . settings )
60
61
. filter ( ( { pattern, link } ) => pattern && link )
61
62
. 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
+ }
65
74
}
66
75
} )
67
76
}
68
77
69
78
handleEnter ( editor : Editor , pos ?: EditorPosition ) {
70
- //Get the current line position
79
+ // Get the current line position
71
80
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
73
82
let lineAbove = editor . getLine ( curPos . line - 1 )
74
83
this . checkForMatch ( editor , { line : curPos . line - 1 , ch : lineAbove . length } )
75
84
}
76
85
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
+
77
93
triggerSnippet ( editor : Editor , evt : KeyboardEvent ) {
78
94
switch ( evt . key ) {
79
95
case " " : {
80
96
this . checkForMatch ( editor )
81
97
break ;
82
98
}
83
99
case "Tab" : {
84
- // TODO: Add support for Tab replacement
100
+ this . handleTab ( editor )
85
101
break ;
86
102
}
87
103
case "Enter" : {
0 commit comments