ebullient_obsidian-deck-notes/src/dn-Api.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-11-17 21:12:54 +00:00
import type { Card } from "./@types/settings";
2025-11-18 02:33:15 +00:00
import type DeckNotesPlugin from "./dn-Plugin";
2025-11-18 02:33:15 +00:00
export class DeckNotesApi {
plugin: DeckNotesPlugin;
2025-11-18 02:33:15 +00:00
constructor(plugin: DeckNotesPlugin) {
this.plugin = plugin;
}
/**
* Get a random card embed as formatted text
2025-11-17 21:12:54 +00:00
* @param deck Tag for deck selection (e.g., "activities")
* @returns The card embed text, or null if no cards available
*/
2025-11-18 03:09:55 +00:00
embedCard(deck?: string): string | null {
2025-11-17 21:12:54 +00:00
return this.plugin.createEmbedText(deck);
}
/**
* Get all unique deck tags discovered in cards
* @returns Array of normalized tag strings (e.g., ["activities", "activities/morning"])
*/
getTags(): string[] {
const tagsSet = new Set<string>();
for (const card of this.plugin.cachedCards) {
for (const tag of card.tags) {
tagsSet.add(tag);
}
}
return Array.from(tagsSet).sort();
}
/**
* Select cards by their hash identifiers
* @param hashes Array of card hashes to select
* @returns Array of matching cards
*/
selectCardsByHash(hashes: string[]): Card[] {
const hashSet = new Set(hashes);
return this.plugin.cachedCards.filter((card) => hashSet.has(card.hash));
}
/**
* Get a random card from a list of hashes
* @param hashes Array of card hashes to select from
* @returns A random card from the list, or null if no matches
*/
selectCardByHash(hashes: string[]): Card | null {
const cards = this.selectCardsByHash(hashes);
if (cards.length === 0) {
return null;
}
return cards[Math.floor(Math.random() * cards.length)];
}
}