Skip to content

Commit b3f4c81

Browse files
committed
changes to debounce, match autoinsert logic in suggestions, bump version
1 parent 7771925 commit b3f4c81

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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-
## Unreleased
7+
## [1.2.0] - 2025-2-25
88

99
### Added
1010

@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1616

1717
### Fixed
1818

19+
- `^` character will not be included in the display text when linking to a block.
1920
- Minor typos.
2021

2122
## [1.1.0] - 2025-1-25

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# Anchor Link Display Text
22

3-
This is a plugin for [Obsidian](https://obsidian.md).
3+
This is a plugin for [Obsidian](https://obsidian.md) which automatically sets the display text of anchor links.
4+
5+
## What's New in v1.2
6+
7+
### Added
8+
9+
- Display text suggestions. A suggestions popup will appear when the cursor is directly after an anchor link with display text. There will be three suggestions one, for each of the display text formats that can be used with this plugin (no note name, note name and then heading(s), heading(s) and then note name).
10+
11+
### Changed
12+
13+
- Heading separators are now validated to not include link breaking characters `[]|#^`. If any of these characters are typed into the separator field, the character will be ignored and a warning will appear.
14+
15+
### Fixed
16+
17+
- Minor typos.
418

519
## Description
620

@@ -36,6 +50,6 @@ Show only the first heading: [[Title#Heading#Subheading#Subsubheading|Heading]]
3650
Show only the last heading: [[Title#Heading#Subheading#Subsubheading|Subsubheading]]
3751
```
3852

39-
By default, the headings in the display text will be separated by a single space, but this can be changed to whatever you prefer. Some examples may be a comma (, ), colon (: ), or arrow (-> ). Just note that whatever is typed in the separator text box in the settings will be exactly what is used in the display text, nothing is added to it or removed from it.
53+
By default, the headings in the display text will be separated by a single space, but this can be changed to whatever you prefer. Some examples may be a comma (, ), colon (: ), or arrow (-> ). Just note that whatever is typed in the separator text box in the settings will be exactly what is used in the display text, nothing is added to it or removed from it, with the exception of characters which will break links: `[]|#^`.
4054

4155
Additionally, there is an option for enabling display text alternative suggestions. This is a suggestions window which will appear when the cursor is next to an existing anchor link. All three display text formats described above will be available as suggestions regardless of the option chosen for automatic display text generation. This is useful for users who wish to use multiple formats.

main.ts

Lines changed: 21 additions & 10 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, debounce } from 'obsidian';
1+
import { App, Editor, EditorPosition, EditorSuggest, EditorSuggestTriggerInfo, Notice, Plugin, PluginSettingTab, Setting, debounce } from 'obsidian';
22

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

3636
// look for header link creation
3737
this.registerEvent(
38-
this.app.workspace.on('editor-change', debounce((editor: Editor) => {
38+
this.app.workspace.on('editor-change', (editor: Editor) => {
3939
// Only process if the last typed character is ']'
4040
const cursor = editor.getCursor();
4141
const currentLine = editor.getLine(cursor.line);
@@ -78,7 +78,7 @@ export default class AnchorDisplayText extends Plugin {
7878
new Notice (`Updated anchor link display text.`);
7979
}
8080
}
81-
}, 150)) // 150ms debounce
81+
})
8282
);
8383
}
8484

@@ -106,18 +106,18 @@ class AnchorDisplaySuggest extends EditorSuggest<AnchorDisplaySuggestion> {
106106

107107
onTrigger(cursor: EditorPosition, editor: Editor): EditorSuggestTriggerInfo | null {
108108
// turns off suggestions if the setting is disabled but the app hasn't been reloaded
109-
if (!this.plugin.settings.suggest) {
110-
return null;
111-
}
109+
if (!this.plugin.settings.suggest) return null;
110+
112111
const currentLine = editor.getLine(cursor.line);
112+
const lastChar = currentLine[cursor.ch - 1];
113+
if (lastChar !== ']') return null;
114+
113115
// match anchor links, even if they already have a display text
114116
const headerLinkPattern = /(\[\[([^\]]+#[^\n\r\]]+)\]\])$/;
115117
// only when cursor is immediately after the link
116118
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
117119

118-
if(!match) {
119-
return null;
120-
}
120+
if(!match) return null;
121121

122122
return {
123123
start: {
@@ -137,6 +137,11 @@ class AnchorDisplaySuggest extends EditorSuggest<AnchorDisplaySuggestion> {
137137
const headings = context.query.split('|')[0].split('#');
138138

139139
let displayText = headings[1];
140+
141+
if (displayText.startsWith('^')) {
142+
displayText = displayText.slice(1);
143+
}
144+
140145
for (let i = 2; i < headings.length; i++) {
141146
displayText += this.plugin.settings.sep + headings[i];
142147
}
@@ -192,6 +197,12 @@ class AnchorDisplaySuggest extends EditorSuggest<AnchorDisplaySuggestion> {
192197
class AnchorDisplayTextSettingTab extends PluginSettingTab {
193198
plugin: AnchorDisplayText;
194199

200+
private showSepNotice = debounce(
201+
() => new Notice(`Separators cannot contain any of the following characters: []#^|`),
202+
1000,
203+
true
204+
);
205+
195206
constructor(app: App, plugin: AnchorDisplayText) {
196207
super(app, plugin);
197208
this.plugin = plugin;
@@ -205,7 +216,7 @@ class AnchorDisplayTextSettingTab extends PluginSettingTab {
205216
}
206217
}
207218
if (validValue != value) {
208-
new Notice(`Separators cannot contain any of the following characters: []#^|`);
219+
this.showSepNotice();
209220
}
210221
return validValue;
211222
}

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.1.0",
4+
"version": "1.2.0",
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.1.0",
3+
"version": "1.2.0",
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)