generated from tpl/obsidian-sample-plugin
Add create from goodreads url command
This commit is contained in:
parent
d92ee41a91
commit
76f180aa58
|
@ -0,0 +1,46 @@
|
|||
import { Notice } from "obsidian";
|
||||
import { Command } from "./Command";
|
||||
import type { Goodreads } from "@data-sources/Goodreads";
|
||||
import type { Book } from "@src/types";
|
||||
|
||||
const GOODREADS_URL_PATTERN = /https:\/\/www.goodreads.com\/book\/show\/(\d+)/;
|
||||
|
||||
export class CreateBookFromGoodreadsUrlCommand extends Command {
|
||||
constructor(
|
||||
private readonly goodreads: Goodreads,
|
||||
private readonly cb: (book: Book) => void | PromiseLike<void>
|
||||
) {
|
||||
super(
|
||||
"create-book-from-goodreads-url",
|
||||
"Create Book from Goodreads URL"
|
||||
);
|
||||
}
|
||||
|
||||
async callback() {
|
||||
const url = await navigator.clipboard.readText();
|
||||
const legacyId = parseInt(
|
||||
GOODREADS_URL_PATTERN.exec(url)?.[1] ?? "",
|
||||
10
|
||||
);
|
||||
|
||||
if (isNaN(legacyId)) {
|
||||
new Notice("Clipboard does not contain a valid Goodreads URL.");
|
||||
return;
|
||||
}
|
||||
|
||||
let book: Book;
|
||||
try {
|
||||
// eslint-disable-next-line prefer-const
|
||||
book = await this.goodreads.getBookByLegacyId(legacyId);
|
||||
} catch (error) {
|
||||
console.error("Failed to get book from Goodreads:", error);
|
||||
new Notice(
|
||||
"Failed to get book from Goodreads. Check console for details."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.cb(book);
|
||||
new Notice("Book created from Goodreads URL.");
|
||||
}
|
||||
}
|
16
src/main.ts
16
src/main.ts
|
@ -18,6 +18,7 @@ import { ResetReadingStatusCommand } from "@commands/ResetReadingStatusCommand";
|
|||
import { BackupReadingLogCommand } from "@commands/CreateReadingLogBackupCommand";
|
||||
import { RestoreReadingLogBackupCommand } from "@commands/RestoreReadingLogBackupCommand";
|
||||
import { Goodreads } from "@data-sources/Goodreads";
|
||||
import { CreateBookFromGoodreadsUrlCommand } from "@commands/CreateBookFromGoodreadsUrlCommand";
|
||||
|
||||
export default class BookTrackerPlugin extends Plugin {
|
||||
public settings: BookTrackerPluginSettings;
|
||||
|
@ -70,6 +71,12 @@ export default class BookTrackerPlugin extends Plugin {
|
|||
this.readingLog
|
||||
)
|
||||
);
|
||||
this.addCommand(
|
||||
new CreateBookFromGoodreadsUrlCommand(
|
||||
this.goodreads,
|
||||
this.createEntry.bind(this)
|
||||
)
|
||||
);
|
||||
|
||||
this.addSettingTab(new BookTrackerSettingTab(this));
|
||||
|
||||
|
@ -153,12 +160,9 @@ export default class BookTrackerPlugin extends Plugin {
|
|||
data
|
||||
);
|
||||
|
||||
if (renderedContent) {
|
||||
await this.app.vault.create(
|
||||
this.settings.tbrFolder + "/" + fileName + ".md",
|
||||
renderedContent
|
||||
);
|
||||
}
|
||||
const filePath = this.settings.tbrFolder + "/" + fileName + ".md";
|
||||
const file = await this.app.vault.create(filePath, renderedContent);
|
||||
await this.app.workspace.getLeaf().openFile(file);
|
||||
} catch (error) {
|
||||
console.error("Failed to create book entry:", error);
|
||||
}
|
||||
|
|
|
@ -42,9 +42,11 @@ export class Templater {
|
|||
public async renderTemplateFile(
|
||||
filePath: string,
|
||||
data: Record<string, unknown>
|
||||
): Promise<string | null> {
|
||||
): Promise<string> {
|
||||
const templateContent = await this.getTemplateContent(filePath);
|
||||
if (!templateContent) return null;
|
||||
if (!templateContent) {
|
||||
throw new Error("Template file not found");
|
||||
}
|
||||
|
||||
return this.renderTemplate(templateContent, data);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue