Skip to content

Commit 4331e65

Browse files
authored
Merge pull request #1 from rca-umb/dev
Dev
2 parents 1807032 + bcf1812 commit 4331e65

File tree

5 files changed

+99
-30
lines changed

5 files changed

+99
-30
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
6+
7+
## [0.2.0] - 2024-11-29
8+
9+
### Added
10+
11+
- Option to pick which headings to include in link display text when linking to multiple headings.
12+
- Options to change where the note name goes in relation to the headings in the link display text.
13+
- Option to change the message of the optional notice that appears whenever the plugin auto changes the display text.
14+
15+
### Fixed
16+
17+
- Incorrect display text when the link includes multiple headings.

main.ts

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { App, Editor, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
22

3-
// Remember to rename these classes and interfaces!
4-
53
interface HeaderDisplayTextSettings {
6-
displayTextFormat: string;
4+
includeNoteName : string;
5+
whichHeadings: string;
76
includeNotice: boolean;
7+
noticeText: string;
8+
sep : string;
89
}
910

1011
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: ' '
1317
}
1418

1519
export default class HeaderDisplayText extends Plugin {
@@ -18,31 +22,41 @@ export default class HeaderDisplayText extends Plugin {
1822
async onload() {
1923
await this.loadSettings();
2024

21-
// This adds a settings tab so the user can configure various aspects of the plugin
2225
this.addSettingTab(new HeaderDisplayTextSettingTab(this.app, this));
2326

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
2628
this.registerEvent(
2729
this.app.workspace.on('editor-change', (editor: Editor) => {
2830
// get what is being typed
2931
const cursor = editor.getCursor();
3032
const currentLine = editor.getLine(cursor.line);
31-
// Wikilink format
3233
// match links to other note headings WITHOUT an already defined display text
33-
const headerLinkPattern = /\[\[([^\]]+)#([^|]+)\]\]/;
34+
const headerLinkPattern = /\[\[([^\]]+#[^|]+)\]\]/;
3435
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
3536
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+
}
3850
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');
4357
}
4458
if (this.settings.includeNotice) {
45-
new Notice ('Link changed!')
59+
new Notice (this.settings.noticeText)
4660
}
4761
}
4862
})
@@ -74,27 +88,65 @@ class HeaderDisplayTextSettingTab extends PluginSettingTab {
7488
display(): void {
7589
const {containerEl} = this;
7690
containerEl.empty();
91+
92+
containerEl.createEl('h2', {text: 'Link display text'})
7793
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.')
8096
.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);
84101
dropdown.onChange(value => {
85-
this.plugin.settings.displayTextFormat = value;
102+
this.plugin.settings.includeNoteName = value;
86103
this.plugin.saveSettings();
87104
});
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+
});
89119
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.')
92134
.addToggle(toggle => {
93135
toggle.setValue(this.plugin.settings.includeNotice);
94136
toggle.onChange(value => {
95137
this.plugin.settings.includeNotice = value;
96138
this.plugin.saveSettings();
97139
});
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+
});
99151
}
100152
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "header-display-text",
33
"name": "Header Display Text",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"minAppVersion": "0.15.0",
66
"description": "Automatically uses linked headers as the display text for the link.",
77
"author": "Robert Arsenault",

package.json

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

versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"0.1.0": "0.15.0"
2+
"0.2.0": "0.15.0"
33
}

0 commit comments

Comments
 (0)