Skip to content

Instantly share code, notes, and snippets.

@Quorafind
Created July 16, 2024 15:37
Show Gist options
  • Save Quorafind/0b1f4585f9edfd263f8d58f94d17cff7 to your computer and use it in GitHub Desktop.
Save Quorafind/0b1f4585f9edfd263f8d58f94d17cff7 to your computer and use it in GitHub Desktop.
Register epub file extension in Obsidian
export default class ExamplePlugin extends Plugin {
async onload() {
this.registerExtensions(["epub"], "epub");
this.registerView("epub", (leaf) => new EpubView(leaf));
}
onunload() {
}
}
export class EpubView extends FileView {
allowNoFile = false;
private rendition: Rendition;
private book: Book;
constructor(leaf: WorkspaceLeaf) {
super(leaf);
}
getViewType(): string {
return "epub";
}
onunload(): void {
this.contentEl.empty();
}
canAcceptExtension(extension: string) {
return "epub" === extension;
}
onload() {
super.onload();
}
async onLoadFile(file: TFile): Promise<void> {
this.contentEl.setAttr('id', 'area');
const content = await this.app.vault.adapter.readBinary(file.path);
this.book = ePub(content);
this.rendition = this.book.renderTo("area", {width: 600, height: 400});
await this.rendition.display();
return super.onLoadFile(file);
}
onUnloadFile(file: TFile): Promise<void> {
this.rendition.destroy();
this.book.destroy();
this.contentEl.empty();
return super.onUnloadFile(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment