Skip to content

Commit e5388ac

Browse files
authored
Merge pull request #7 from rca-umb/tab
Tab
2 parents 308d76b + 870728a commit e5388ac

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.2.1] - 2025-3-4
8+
9+
### Fixed
10+
11+
- If a suggestion is selected, the suggestion window will disappear until the next editor change.
12+
- Separator warning will appear until clicked or the problematic characters are removed, and only instance of this warning will appear at a time.
13+
714
## [1.2.0] - 2025-2-25
815

916
### Added

main.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, Editor, EditorPosition, EditorSuggest, EditorSuggestTriggerInfo, Notice, Plugin, PluginSettingTab, Setting, debounce } from 'obsidian';
1+
import { App, Editor, EditorPosition, EditorSuggest, EditorSuggestTriggerInfo, Notice, Plugin, PluginSettingTab, Setting} from 'obsidian';
22

33
interface AnchorDisplaySuggestion {
44
displayText: string;
@@ -40,12 +40,14 @@ export default class AnchorDisplayText extends Plugin {
4040
const cursor = editor.getCursor();
4141
const currentLine = editor.getLine(cursor.line);
4242

43-
const lastChar = currentLine[cursor.ch - 1];
44-
if (lastChar !== ']') return;
43+
const lastChars = currentLine.slice(cursor.ch - 2, cursor.ch);
44+
if (lastChars !== ']]') {
45+
return
46+
}
4547

4648
// match anchor links WITHOUT an already defined display text
4749
const headerLinkPattern = /\[\[([^\]]+#[^|\n\r\]]+)\]\]/;
48-
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
50+
const match = currentLine.slice(0, cursor.ch + 2).match(headerLinkPattern);
4951
if (match) {
5052
// handle multiple subheadings
5153
const headings = match[1].split('#')
@@ -98,6 +100,7 @@ export default class AnchorDisplayText extends Plugin {
98100

99101
class AnchorDisplaySuggest extends EditorSuggest<AnchorDisplaySuggestion> {
100102
private plugin: AnchorDisplayText;
103+
private suggestionSelected: EditorPosition | null = null;
101104

102105
constructor(plugin: AnchorDisplayText) {
103106
super(plugin.app);
@@ -107,17 +110,23 @@ class AnchorDisplaySuggest extends EditorSuggest<AnchorDisplaySuggestion> {
107110
onTrigger(cursor: EditorPosition, editor: Editor): EditorSuggestTriggerInfo | null {
108111
// turns off suggestions if the setting is disabled but the app hasn't been reloaded
109112
if (!this.plugin.settings.suggest) return null;
113+
if (this.suggestionSelected) {
114+
if (this.suggestionSelected.ch === cursor.ch
115+
&& this.suggestionSelected.line === cursor.line) return null;
116+
this.suggestionSelected = null;
117+
return null;
118+
}
110119

111120
const currentLine = editor.getLine(cursor.line);
112-
const lastChar = currentLine[cursor.ch - 1];
113-
if (lastChar !== ']') return null;
121+
const lastChars = currentLine.slice(cursor.ch - 2, cursor.ch);
122+
if (lastChars !== ']]') return null;
114123

115124
// match anchor links, even if they already have a display text
116125
const headerLinkPattern = /(\[\[([^\]]+#[^\n\r\]]+)\]\])$/;
117126
// only when cursor is immediately after the link
118127
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
119128

120-
if(!match) return null;
129+
if (!match) return null;
121130

122131
return {
123132
start: {
@@ -191,17 +200,13 @@ class AnchorDisplaySuggest extends EditorSuggest<AnchorDisplaySuggestion> {
191200
this.context!.start.ch = this.context!.start.ch - match[0].length;
192201
}
193202
editor.replaceRange(`|${value.displayText}`, this.context!.start, this.context!.end, 'headerDisplayText');
203+
this.suggestionSelected = this.context!.end;
194204
};
195205
}
196206

197207
class AnchorDisplayTextSettingTab extends PluginSettingTab {
198208
plugin: AnchorDisplayText;
199-
200-
private showSepNotice = debounce(
201-
() => new Notice(`Separators cannot contain any of the following characters: []#^|`),
202-
1000,
203-
true
204-
);
209+
private sepWarning: Notice | null = null;
205210

206211
constructor(app: App, plugin: AnchorDisplayText) {
207212
super(app, plugin);
@@ -210,13 +215,21 @@ class AnchorDisplayTextSettingTab extends PluginSettingTab {
210215

211216
validateSep(value: string): string {
212217
let validValue: string = value;
218+
213219
for (const c of value) {
214220
if ('[]#^|'.includes(c)) {
215221
validValue = validValue.replace(c, '');
216222
}
217223
}
218224
if (validValue != value) {
219-
this.showSepNotice();
225+
if (!this.sepWarning) {
226+
this.sepWarning = new Notice(`Separators cannot contain any of the following characters: []#^|`, 0);
227+
}
228+
} else {
229+
if (this.sepWarning) {
230+
this.sepWarning!.hide();
231+
this.sepWarning = null;
232+
}
220233
}
221234
return validValue;
222235
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "anchor-display-text",
33
"name": "Anchor Link Display Text",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"minAppVersion": "0.15.0",
66
"description": "Automatically uses the linked heading as the display text for the anchor links.",
77
"author": "Robert C Arsenault",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "anchor-display-text-plugin",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Plugin for Obsidian (https://obsidian.md) that sets the display text of anchor links to the name of the heading.",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)