1
- import { App , Command , Editor , EditorPosition , EditorSuggest , EditorSuggestTriggerInfo , Notice , Plugin , PluginSettingTab , Setting , TFile } from 'obsidian' ;
1
+ import { App , Command , Editor , EditorPosition , EditorSuggest , EditorSuggestTriggerInfo , Notice , Plugin , PluginSettingTab , Setting , TFile , debounce } from 'obsidian' ;
2
2
3
3
interface AnchorDisplaySuggestion {
4
4
displayText : string ;
@@ -35,10 +35,14 @@ export default class AnchorDisplayText extends Plugin {
35
35
36
36
// look for header link creation
37
37
this . registerEvent (
38
- this . app . workspace . on ( 'editor-change' , ( editor : Editor ) => {
39
- // get what is being typed
38
+ this . app . workspace . on ( 'editor-change' , debounce ( ( editor : Editor ) => {
39
+ // Only process if the last typed character is ']'
40
40
const cursor = editor . getCursor ( ) ;
41
41
const currentLine = editor . getLine ( cursor . line ) ;
42
+
43
+ const lastChar = currentLine [ cursor . ch - 1 ] ;
44
+ if ( lastChar !== ']' ) return ;
45
+
42
46
// match anchor links WITHOUT an already defined display text
43
47
const headerLinkPattern = / \[ \[ ( [ ^ \] ] + # [ ^ | \n \r \] ] + ) \] \] / ;
44
48
const match = currentLine . slice ( 0 , cursor . ch ) . match ( headerLinkPattern ) ;
@@ -63,13 +67,18 @@ export default class AnchorDisplayText extends Plugin {
63
67
} else if ( this . settings . includeNoteName === 'noteNameLast' ) {
64
68
displayText = `${ displayText } ${ this . settings . sep } ${ headings [ 0 ] } ` ;
65
69
}
70
+
71
+ if ( displayText . startsWith ( '^' ) ) {
72
+ displayText = displayText . slice ( 1 ) ;
73
+ }
74
+
66
75
// change the display text of the link
67
76
editor . replaceRange ( `|${ displayText } ` , { line : cursor . line , ch : startIndex } , undefined , 'headerDisplayText' ) ;
68
77
if ( this . settings . includeNotice ) {
69
78
new Notice ( `Updated anchor link display text.` ) ;
70
79
}
71
80
}
72
- } )
81
+ } , 150 ) ) // 150ms debounce
73
82
) ;
74
83
}
75
84
0 commit comments