1
1
import { App , Editor , Notice , Plugin , PluginSettingTab , Setting } from 'obsidian' ;
2
2
3
- // Remember to rename these classes and interfaces!
4
-
5
3
interface HeaderDisplayTextSettings {
6
- displayTextFormat : string ;
4
+ includeNoteName : string ;
5
+ whichHeadings : string ;
7
6
includeNotice : boolean ;
7
+ noticeText : string ;
8
+ sep : string ;
8
9
}
9
10
10
11
const DEFAULT_SETTINGS : HeaderDisplayTextSettings = {
11
- displayTextFormat : 'headerOnly' ,
12
- includeNotice : false
12
+ includeNoteName : 'headersOnly' ,
13
+ whichHeadings : 'allHeaders' ,
14
+ includeNotice : false ,
15
+ noticeText : 'Link changed!' ,
16
+ sep : ' '
13
17
}
14
18
15
19
export default class HeaderDisplayText extends Plugin {
@@ -18,31 +22,41 @@ export default class HeaderDisplayText extends Plugin {
18
22
async onload ( ) {
19
23
await this . loadSettings ( ) ;
20
24
21
- // This adds a settings tab so the user can configure various aspects of the plugin
22
25
this . addSettingTab ( new HeaderDisplayTextSettingTab ( this . app , this ) ) ;
23
26
24
- // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
25
- // Using this function will automatically remove the event listener when this plugin is disabled.
27
+ // look for header link creation
26
28
this . registerEvent (
27
29
this . app . workspace . on ( 'editor-change' , ( editor : Editor ) => {
28
30
// get what is being typed
29
31
const cursor = editor . getCursor ( ) ;
30
32
const currentLine = editor . getLine ( cursor . line ) ;
31
- // Wikilink format
32
33
// match links to other note headings WITHOUT an already defined display text
33
- const headerLinkPattern = / \[ \[ ( [ ^ \] ] + ) # ( [ ^ | ] + ) \] \] / ;
34
+ const headerLinkPattern = / \[ \[ ( [ ^ \] ] + # [ ^ | ] + ) \] \] / ;
34
35
const match = currentLine . slice ( 0 , cursor . ch ) . match ( headerLinkPattern ) ;
35
36
if ( match ) {
36
- const noteName = match [ 1 ] ;
37
- const heading = match [ 2 ] ;
37
+ // handle multiple subheadings
38
+ const headings = match [ 1 ] . split ( '#' )
39
+ let displayText = ''
40
+ if ( this . settings . whichHeadings === 'lastHeader' ) {
41
+ displayText = headings [ headings . length - 1 ] ;
42
+ } else {
43
+ displayText = headings [ 1 ] ;
44
+ if ( this . settings . whichHeadings === 'allHeaders' ) {
45
+ for ( let i = 2 ; i < headings . length ; i ++ ) {
46
+ displayText += this . settings . sep + headings [ i ] ;
47
+ }
48
+ }
49
+ }
38
50
const startIndex = ( match . index ?? 0 ) + match [ 0 ] . length - 2 ;
39
- if ( this . settings . displayTextFormat === 'headerOnly' ) {
40
- editor . replaceRange ( `|${ heading } ` , { line : cursor . line , ch : startIndex } , undefined , 'headerAliases' )
41
- } else if ( this . settings . displayTextFormat === 'withNoteName' ) {
42
- editor . replaceRange ( `|${ noteName } ${ heading } ` , { line : cursor . line , ch : startIndex } , undefined , 'headerAliases' )
51
+ if ( this . settings . includeNoteName === 'headersOnly' ) {
52
+ editor . replaceRange ( `|${ displayText } ` , { line : cursor . line , ch : startIndex } , undefined , 'headerDisplayText' ) ;
53
+ } else if ( this . settings . includeNoteName === 'noteNameFirst' ) {
54
+ editor . replaceRange ( `|${ headings [ 0 ] } ${ this . settings . sep } ${ displayText } ` , { line : cursor . line , ch : startIndex } , undefined , 'headerDisplayText' ) ;
55
+ } else if ( this . settings . includeNoteName === 'noteNameLast' ) {
56
+ editor . replaceRange ( `|${ displayText } ${ this . settings . sep } ${ headings [ 0 ] } ` , { line : cursor . line , ch : startIndex } , undefined , 'headerDisplayText' ) ;
43
57
}
44
58
if ( this . settings . includeNotice ) {
45
- new Notice ( 'Link changed!' )
59
+ new Notice ( this . settings . noticeText )
46
60
}
47
61
}
48
62
} )
@@ -74,27 +88,65 @@ class HeaderDisplayTextSettingTab extends PluginSettingTab {
74
88
display ( ) : void {
75
89
const { containerEl} = this ;
76
90
containerEl . empty ( ) ;
91
+
92
+ containerEl . createEl ( 'h2' , { text : 'Link display text' } )
77
93
new Setting ( containerEl )
78
- . setName ( 'Display Text Format ' )
79
- . setDesc ( 'Change the format of the auto populated display text.' )
94
+ . setName ( 'Include note name ' )
95
+ . setDesc ( 'Include the title of the note in the display text.' )
80
96
. addDropdown ( dropdown => {
81
- dropdown . addOption ( 'headerOnly' , 'Header Only' ) ;
82
- dropdown . addOption ( 'withNoteName' , 'Note Name and Header' ) ;
83
- dropdown . setValue ( 'headerOnly' ) ;
97
+ dropdown . addOption ( 'headersOnly' , 'Don\'t include note name' ) ;
98
+ dropdown . addOption ( 'noteNameFirst' , 'Note name and then heading(s)' ) ;
99
+ dropdown . addOption ( 'noteNameLast' , 'Heading(s) and then note name' ) ;
100
+ dropdown . setValue ( this . plugin . settings . includeNoteName ) ;
84
101
dropdown . onChange ( value => {
85
- this . plugin . settings . displayTextFormat = value ;
102
+ this . plugin . settings . includeNoteName = value ;
86
103
this . plugin . saveSettings ( ) ;
87
104
} ) ;
88
- } )
105
+ } ) ;
106
+ new Setting ( containerEl )
107
+ . setName ( 'Headings to include' )
108
+ . setDesc ( 'Change which headings and subheadings are in the display text.' )
109
+ . addDropdown ( dropdown => {
110
+ dropdown . addOption ( 'allHeaders' , 'All linked headings' ) ;
111
+ dropdown . addOption ( 'lastHeader' , 'Last heading only' ) ;
112
+ dropdown . addOption ( 'firstHeader' , 'First heading only' ) ;
113
+ dropdown . setValue ( this . plugin . settings . whichHeadings ) ;
114
+ dropdown . onChange ( value => {
115
+ this . plugin . settings . whichHeadings = value ;
116
+ this . plugin . saveSettings ( ) ;
117
+ } ) ;
118
+ } ) ;
89
119
new Setting ( containerEl )
90
- . setName ( 'Notifications' )
91
- . setDesc ( 'Have a notification pop up whenever a link is automatically changed.' )
120
+ . setName ( 'Set seperators' )
121
+ . setDesc ( 'Choose what to insert between headings, instead of #. Ex: , , ->, :, etc.' )
122
+ . addText ( text => {
123
+ text . setValue ( this . plugin . settings . sep ) ;
124
+ text . onChange ( value => {
125
+ this . plugin . settings . sep = value ;
126
+ this . plugin . saveSettings ( ) ;
127
+ } ) ;
128
+ } ) ;
129
+
130
+ containerEl . createEl ( 'h2' , { text : 'Notifications' } )
131
+ new Setting ( containerEl )
132
+ . setName ( 'Enable notifications' )
133
+ . setDesc ( 'Have a notice pop up whenever a link is automatically changed.' )
92
134
. addToggle ( toggle => {
93
135
toggle . setValue ( this . plugin . settings . includeNotice ) ;
94
136
toggle . onChange ( value => {
95
137
this . plugin . settings . includeNotice = value ;
96
138
this . plugin . saveSettings ( ) ;
97
139
} ) ;
98
- } )
140
+ } ) ;
141
+ new Setting ( containerEl )
142
+ . setName ( 'Notification text' )
143
+ . setDesc ( 'Set the text to appear in the notification.' )
144
+ . addText ( text => {
145
+ text . setValue ( this . plugin . settings . noticeText ) ;
146
+ text . onChange ( value => {
147
+ this . plugin . settings . noticeText = value ;
148
+ this . plugin . saveSettings ( ) ;
149
+ } ) ;
150
+ } ) ;
99
151
}
100
152
}
0 commit comments