Skip to content

Commit

Permalink
Add custom tag option in the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
crybot committed Jun 1, 2024
1 parent 853aa31 commit 24b17b2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
27 changes: 22 additions & 5 deletions main.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export class InputModal extends Modal {
})
);

new Setting(contentEl)
.setName("Flashcards tag")
.addText((text) =>
text
.setPlaceholder("#flashcards")
.setValue(this.plugin.settings.tag)
.onChange(async (value) => {
this.configuration.tag = value
})
);

new Setting(contentEl)
.setName("Additional prompt")
.addText((text) =>
Expand Down
15 changes: 9 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { FlashcardsSettings, FlashcardsSettingsTab } from "./settings"
// TODO:
// - Status bar
// - Enforce newline separation (stream post processing)
// - Always append flashcards at the end of the file (ch:0, line: last)
// - Disable user input while generating
// - Custom tag for flashcards blocks
// - Insert an optional header before flashcards
Expand All @@ -21,7 +20,8 @@ const DEFAULT_SETTINGS: FlashcardsSettings = {
additionalPrompt: "",
maxTokens: 300,
streaming: true,
hideInPreview: true
hideInPreview: true,
tag: "#flashcards"
};

export default class FlashcardsLLMPlugin extends Plugin {
Expand Down Expand Up @@ -65,10 +65,11 @@ export default class FlashcardsLLMPlugin extends Plugin {
}

const blocks = element.findAll("blockquote");
const tag = this.settings.tag;

for(let block of blocks) {
const anchors = Array.from(block.querySelectorAll("a"));
if (anchors.some((a) => a.getAttribute("href")?.startsWith("#flashcards"))) {
if (anchors.some((a) => a.getAttribute("href")?.startsWith(`${tag}`))) {
block.style.display = 'none'
}
}
Expand Down Expand Up @@ -107,15 +108,17 @@ export default class FlashcardsLLMPlugin extends Plugin {
maxTokens = 300
}

const tag = configuration.tag;

const wholeText = editor.getValue()
const currentText = (editor.somethingSelected() ? editor.getSelection() : wholeText)
// Check if the header is already present
const headerRegex = /\n\n### Generated Flashcards\n/;
const hasHeader = headerRegex.test(wholeText);

// Check if the #flashcards tag is already present
const tagRegex = /\n#flashcards.*\n/;
const hasTag = tagRegex.test(wholeText);
// const tagRegex = /\n#flashcards.*\n/;
// const hasTag = tagRegex.test(wholeText);


const streaming = configuration.streaming
Expand Down Expand Up @@ -147,7 +150,7 @@ export default class FlashcardsLLMPlugin extends Plugin {
// if (!hasTag) {
// updatedText += "> #flashcards\n> \n> ";
// }
updatedText += "\n\n> #flashcards\n> \n> ";
updatedText += `\n\n> ${tag}\n> \n> `;

editor.setCursor(editor.lastLine())
editor.replaceRange(updatedText, editor.getCursor())
Expand Down
19 changes: 19 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { App, MarkdownView, PluginSettingTab, Setting } from 'obsidian';
import { availableChatModels, availableCompletionModels } from "./models";
import FlashcardsLLMPlugin from "./main"

// TODO:
// - make additional prompt a resizable textarea

export interface FlashcardsSettings {
apiKey: string;
model: string;
Expand All @@ -12,6 +15,7 @@ export interface FlashcardsSettings {
maxTokens: number;
streaming: boolean;
hideInPreview: boolean;
tag: string;
}


Expand Down Expand Up @@ -84,6 +88,20 @@ export class FlashcardsSettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Flashcards tag")
.setDesc("Set which tag to append upon flashcards generation. " +
"See the Spaced Repetition plugin for details")
.addText((text) =>
text
.setPlaceholder("#flashcards")
.setValue(this.plugin.settings.tag)
.onChange(async (value) => {
this.plugin.settings.tag = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Number of flashcards to generate")
.setDesc("Set this to the total number of flashcards the model should "+
Expand All @@ -98,6 +116,7 @@ export class FlashcardsSettingsTab extends PluginSettingTab {
})
);


new Setting(containerEl)
.setName("Additional prompt")
.setDesc("Provide additional instructions to the language model")
Expand Down

0 comments on commit 24b17b2

Please sign in to comment.