Add wikilink

This commit is contained in:
Hyeonseo Nam 2023-05-03 22:50:42 +09:00
parent 8080215b40
commit dc31456f5e
3 changed files with 123 additions and 51 deletions

View file

@ -1,5 +1,5 @@
import { Plugin, Notice } from "obsidian";
import { AutoClassifierSettingTab, AutoClassifierSettings, DEFAULT_SETTINGS, OutLocation } from "src/settings";
import { AutoClassifierSettingTab, AutoClassifierSettings, DEFAULT_SETTINGS, OutLocation, OutType} from "src/settings";
import { ViewManager } from "src/view-manager";
import { ChatGPT } from 'src/api';
@ -165,7 +165,7 @@ export default class AutoClassifierPlugin extends Plugin {
}
// Cursor
else if (commandOption.outLocation == OutLocation.Cursor) {
this.viewManager.insertAtCursor(resOutput, commandOption.overwrite);
this.viewManager.insertAtCursor(resOutput, commandOption.overwrite, commandOption.outType);
new Notice(`${this.manifest.name}: classified to ${resOutput} at Current Cursor`);
}

View file

@ -10,9 +10,19 @@ export enum ReferenceType {
}
export enum OutLocation {
FrontMatter,
Title,
Cursor,
Title,
FrontMatter,
}
// export enum OutLocation_link {
// Cursor,
// // ContentTop,
// }
export enum OutType {
Tag,
Wikilink,
}
// for tag, keyword
@ -23,6 +33,8 @@ export interface CommandOption {
refType: ReferenceType;
filterRegex: string; // for ReferenceType - Filter
outLocation: OutLocation;
// outLocation_link: OutLocation_link;
outType: OutType;
key: string; // for OutLocation - FrontMatter
overwrite: boolean; // for OutLocation - FrontMatter
@ -48,7 +60,9 @@ export const DEFAULT_SETTINGS: AutoClassifierSettings = {
manualRefs: [],
refType: ReferenceType.All,
filterRegex: '',
outLocation: OutLocation.FrontMatter,
outLocation: OutLocation.Cursor,
// outLocation_link: OutLocation_link.Cursor,
outType: OutType.Tag,
key: 'tags',
overwrite: false,
useCustomCommand: false,
@ -236,62 +250,116 @@ export class AutoClassifierSettingTab extends PluginSettingTab {
// ------- [Output Tag Setting] -------
// Tag Location dropdown
containerEl.createEl('h1', { text: 'Output Tag Setting' });
// ------- [Output Setting] -------
containerEl.createEl('h1', { text: 'Output Setting' });
// Output type dropdown
new Setting(containerEl)
.setName('Output Tag Location')
.setDesc('Specify where to put the output tag')
.setName('Output Type')
.setDesc('Specify output type')
.addDropdown((cb) => {
cb.addOption(String(OutLocation.FrontMatter), 'FrontMatter')
.addOption(String(OutLocation.Title), 'Title alternative')
.addOption(String(OutLocation.Cursor), 'Current cursor')
.setValue(String(commandOption.outLocation))
cb.addOption(String(OutType.Tag), '#Tag')
.addOption(String(OutType.Wikilink), '[[Wikilink]]')
.setValue(String(commandOption.outType))
.onChange(async (value) => {
commandOption.outLocation = parseInt(value);
commandOption.outType = parseInt(value);
commandOption.outLocation = 0; // Initialize
await this.plugin.saveSettings();
this.display();
});
});
// Frontmatter - key text setting
let overwriteName = '';
if (commandOption.outLocation == OutLocation.FrontMatter) {
overwriteName = 'Overwrite value of the key';
// Output Tag -
if (commandOption.outType == OutType.Tag) {
// Tag - Location dropdown
new Setting(containerEl)
.setName('FrontMatter key')
.setDesc('Specify FrontMatter key to put the output tag')
.setClass('setting-item-child')
.addText((text) =>
text
.setPlaceholder('Key')
.setValue(commandOption.key)
.setName('Output Location')
.setDesc('Specify where to put the output tag')
.addDropdown((cb) => {
cb.addOption(String(OutLocation.Cursor), 'Current Cursor')
.addOption(String(OutLocation.Title), 'Title alternative')
.addOption(String(OutLocation.FrontMatter), 'FrontMatter')
.setValue(String(commandOption.outLocation))
.onChange(async (value) => {
commandOption.key = value;
commandOption.outLocation = parseInt(value);
await this.plugin.saveSettings();
this.display();
});
});
// Frontmatter - key text setting
let overwriteName = '';
if (commandOption.outLocation == OutLocation.FrontMatter) {
overwriteName = 'Overwrite value of the key';
new Setting(containerEl)
.setName('FrontMatter key')
.setDesc('Specify FrontMatter key to put the output tag')
.setClass('setting-item-child')
.addText((text) =>
text
.setPlaceholder('Key')
.setValue(commandOption.key)
.onChange(async (value) => {
commandOption.key = value;
await this.plugin.saveSettings();
})
);
}
else if (commandOption.outLocation == OutLocation.Title) {
overwriteName = 'Overwrite whole title';
}
else if (commandOption.outLocation == OutLocation.Cursor) {
overwriteName = 'Overwrite selected area if selected';
}
// Overwrite toggle
new Setting(containerEl)
.setName(overwriteName)
.setClass('setting-item-child')
.addToggle((toggle) =>
toggle
.setValue(commandOption.overwrite)
.onChange(async (value) => {
commandOption.overwrite = value;
await this.plugin.saveSettings();
})
);
}
else if (commandOption.outLocation == OutLocation.Title) {
overwriteName = 'Overwrite whole title';
else if (commandOption.outType == OutType.Wikilink) {
// Wikilink - Location dropdown
new Setting(containerEl)
.setName('Output Location')
.setDesc('Specify where to put the output wikilink')
.addDropdown((cb) => {
cb.addOption(String(OutLocation.Cursor), 'Current Cursor')
// .addOption(String(OutLocation.ContentTop), 'Top of Content')
.setValue(String(commandOption.outLocation))
.onChange(async (value) => {
commandOption.outLocation = parseInt(value);
await this.plugin.saveSettings();
this.display();
});
});
// Set overwriteName
let overwriteName = '';
if (commandOption.outLocation == OutLocation.Cursor) {
overwriteName = 'Overwrite selected area if selected';
}
// Overwrite toggle
new Setting(containerEl)
.setName(overwriteName)
.setClass('setting-item-child')
.addToggle((toggle) =>
toggle
.setValue(commandOption.overwrite)
.onChange(async (value) => {
commandOption.overwrite = value;
await this.plugin.saveSettings();
})
);
}
else if (commandOption.outLocation == OutLocation.Cursor) {
overwriteName = 'Overwrite selected area';
}
// Overwrite toggle
new Setting(containerEl)
.setName(overwriteName)
.setClass('setting-item-child')
.addToggle((toggle) =>
toggle
.setValue(commandOption.overwrite)
.onChange(async (value) => {
commandOption.overwrite = value;
await this.plugin.saveSettings();
})
);
// ------- [Advanced Setting] -------
// Toggle custom rule
containerEl.createEl('h1', { text: 'Advanced Setting' });
@ -339,7 +407,7 @@ export class AutoClassifierSettingTab extends PluginSettingTab {
// Different default template depanding on useRef
if (commandOption.useRef) commandOption.prmpt_template = DEFAULT_PROMPT_TEMPLATE;
else commandOption.prmpt_template = DEFAULT_PROMPT_TEMPLATE_WO_REF;
await this.plugin.saveSettings();
this.display();
})

View file

@ -1,4 +1,5 @@
import { App, MarkdownView, Editor, FrontMatterCache } from "obsidian";
import { OutType } from "src/settings";
export class ViewManager {
app: App;
@ -106,9 +107,12 @@ export class ViewManager {
await this.app.fileManager.renameFile(file, newPath);
}
async insertAtCursor(value: string, overwrite = false): Promise<void> {
async insertAtCursor(value: string, overwrite = false, outType: OutType): Promise<void> {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
let tag = ` #${value} `;
let output = '';
if (outType == OutType.Tag) output = ` #${value} `;
else if (outType == OutType.Wikilink) output = `[[${value}]]`;
if (activeView) {
const editor = activeView.editor;
const selection = editor.getSelection();
@ -117,7 +121,7 @@ export class ViewManager {
editor.setSelection(editor.getCursor('to'));
}
// overwrite
editor.replaceSelection(tag);
editor.replaceSelection(output);
}
}