Skip to content

Commit fe856cd

Browse files
committed
Automatic display text replacement for Wikilink formating
1 parent 16fa40b commit fe856cd

File tree

2 files changed

+34
-100
lines changed

2 files changed

+34
-100
lines changed

main.ts

Lines changed: 29 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,44 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Set
22

33
// Remember to rename these classes and interfaces!
44

5-
interface MyPluginSettings {
5+
interface HeaderDisplayTextSettings {
66
mySetting: string;
77
}
88

9-
const DEFAULT_SETTINGS: MyPluginSettings = {
9+
const DEFAULT_SETTINGS: HeaderDisplayTextSettings = {
1010
mySetting: 'default'
1111
}
1212

13-
export default class ProfileBuilder extends Plugin {
14-
settings: MyPluginSettings;
13+
export default class HeaderDisplayText extends Plugin {
14+
settings: HeaderDisplayTextSettings;
1515

1616
async onload() {
1717
await this.loadSettings();
1818

19-
// This creates an icon in the left ribbon.
20-
const ribbonIconEl = this.addRibbonIcon('square-user-round', 'Get Profile', async (evt: MouseEvent) => {
21-
// Called when the user clicks the icon.
22-
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
23-
if (activeView) { // if a file is opened on screen
24-
const editor = activeView.editor;
25-
const selectedText = editor.getSelection(); // get the text selected in the editor
26-
if (selectedText.length === 0) {
27-
new Notice('No text selected');
28-
return;
29-
} else {
30-
new Notice(`Getting profile information for ${selectedText}`);
31-
const profileInfo = await this.getProfileInfo(selectedText);
32-
editor.replaceRange(profileInfo, editor.getCursor());
33-
new Notice('Profile information inserted');
34-
}
35-
} else {
36-
new Notice('No file opened');
37-
}
38-
});
39-
// Perform additional things with the ribbon
40-
ribbonIconEl.addClass('my-plugin-ribbon-class');
41-
42-
this.addCommand({
43-
id: 'get-profile-info',
44-
name: 'Get and insert profile information',
45-
editorCallback: (editor: Editor) => {
46-
const selection = editor.getSelection();
47-
editor.replaceRange('Some text', editor.getCursor());
48-
// Do something with the selection
49-
},
50-
});
51-
5219
// This adds a settings tab so the user can configure various aspects of the plugin
53-
this.addSettingTab(new SampleSettingTab(this.app, this));
20+
this.addSettingTab(new HeaderDisplayTextSettingTab(this.app, this));
5421

5522
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
5623
// Using this function will automatically remove the event listener when this plugin is disabled.
57-
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
58-
console.log('click', evt);
59-
});
60-
61-
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
62-
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
24+
this.registerEvent(
25+
this.app.workspace.on('editor-change', (editor: Editor) => {
26+
// get what is being typed
27+
const cursor = editor.getCursor();
28+
const currentLine = editor.getLine(cursor.line);
29+
// Wikilink format
30+
// match links to other note headings WITHOUT an already defined display text
31+
const headerLinkPattern = /\[\[([^\]]+)#([^|]+)\]\]/;
32+
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
33+
if (match) {
34+
const noteName = match[1];
35+
const heading = match[2];
36+
console.log(noteName, heading);
37+
const startIndex = (match.index ?? 0) + match[0].length - 2;
38+
editor.replaceRange(`|${heading}`, {line: cursor.line, ch: startIndex}, undefined, 'headerAliases')
39+
new Notice ('Link changed!')
40+
}
41+
})
42+
);
6343
}
6444

6545
onunload() {
@@ -74,58 +54,12 @@ export default class ProfileBuilder extends Plugin {
7454
await this.saveData(this.settings);
7555
}
7656

77-
async getProfileInfo(userSelection: string) {
78-
// Try changing this to a google search for the linkedin, downloading the page, using an AI to parse the information?
79-
const body = {
80-
"model":"llama-3.1-sonar-huge-128k-online",
81-
"messages":
82-
[
83-
{
84-
"role":"system",
85-
"content":"Find the academic and work experience of the given person. Output the information in this structured, Markdown format: ##Background###Work###Education. Each section should just be a list of experiences noting the company and position for work or degree and field of study for education. At the end of each item should be the start and end date. If the end date is not available, just put the start date. If the start date is not available, just put the end date. If both are not available, just put 'N/A'. After these two sections, you may include anything else you find about the person."
86-
},
87-
{
88-
"role":"user",
89-
"content":`Tell me about ${userSelection}`
90-
}
91-
],
92-
"search_domain_filer": ["linkedin.com"]
93-
}
94-
const options = {
95-
method: 'POST',
96-
headers: {Authorization: `Bearer ${process.env.PPLX_API_KEY}`, 'Content-Type': 'application/json'},
97-
body: JSON.stringify(body)
98-
};
99-
try {
100-
const response = await fetch('https://api.perplexity.ai/chat/completions', options);
101-
const data = await response.json();
102-
return data.choices[0].message.content;
103-
} catch (err) {
104-
return err.message;
105-
}
106-
}
107-
}
108-
109-
class SampleModal extends Modal {
110-
constructor(app: App) {
111-
super(app);
112-
}
113-
114-
onOpen() {
115-
const {contentEl} = this;
116-
contentEl.setText('Woah!');
117-
}
118-
119-
onClose() {
120-
const {contentEl} = this;
121-
contentEl.empty();
122-
}
12357
}
12458

125-
class SampleSettingTab extends PluginSettingTab {
126-
plugin: ProfileBuilder;
59+
class HeaderDisplayTextSettingTab extends PluginSettingTab {
60+
plugin: HeaderDisplayText;
12761

128-
constructor(app: App, plugin: ProfileBuilder) {
62+
constructor(app: App, plugin: HeaderDisplayText) {
12963
super(app, plugin);
13064
this.plugin = plugin;
13165
}
@@ -136,8 +70,8 @@ class SampleSettingTab extends PluginSettingTab {
13670
containerEl.empty();
13771

13872
new Setting(containerEl)
139-
.setName('Setting #1')
140-
.setDesc('It\'s a secret')
73+
.setName('Display Text Format')
74+
.setDesc('Change the format of the display text.')
14175
.addText(text => text
14276
.setPlaceholder('Enter your secret')
14377
.setValue(this.plugin.settings.mySetting)

manifest.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"id": "sample-plugin",
3-
"name": "Sample Plugin",
4-
"version": "1.0.0",
2+
"id": "header-display-text",
3+
"name": "Header Display Text",
4+
"version": "0.0.0",
55
"minAppVersion": "0.15.0",
6-
"description": "Demonstrates some of the capabilities of the Obsidian API.",
7-
"author": "Obsidian",
6+
"description": "Automatically uses linked headers as the display text.",
7+
"author": "Robert Arsenault",
88
"authorUrl": "https://obsidian.md",
99
"fundingUrl": "https://obsidian.md/pricing",
1010
"isDesktopOnly": false

0 commit comments

Comments
 (0)