Skip to content

Commit 7771925

Browse files
authored
Merge pull request #4 from Moyf/handle-block-id
Special handling for the case of block id
2 parents 4d5e5f8 + e29a3fe commit 7771925

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

main.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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';
22

33
interface AnchorDisplaySuggestion {
44
displayText: string;
@@ -35,10 +35,14 @@ export default class AnchorDisplayText extends Plugin {
3535

3636
// look for header link creation
3737
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 ']'
4040
const cursor = editor.getCursor();
4141
const currentLine = editor.getLine(cursor.line);
42+
43+
const lastChar = currentLine[cursor.ch - 1];
44+
if (lastChar !== ']') return;
45+
4246
// match anchor links WITHOUT an already defined display text
4347
const headerLinkPattern = /\[\[([^\]]+#[^|\n\r\]]+)\]\]/;
4448
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
@@ -63,13 +67,18 @@ export default class AnchorDisplayText extends Plugin {
6367
} else if (this.settings.includeNoteName === 'noteNameLast') {
6468
displayText = `${displayText}${this.settings.sep}${headings[0]}`;
6569
}
70+
71+
if (displayText.startsWith('^')) {
72+
displayText = displayText.slice(1);
73+
}
74+
6675
// change the display text of the link
6776
editor.replaceRange(`|${displayText}`, {line: cursor.line, ch: startIndex}, undefined, 'headerDisplayText');
6877
if (this.settings.includeNotice) {
6978
new Notice (`Updated anchor link display text.`);
7079
}
7180
}
72-
})
81+
}, 150)) // 150ms debounce
7382
);
7483
}
7584

0 commit comments

Comments
 (0)