initial
This commit is contained in:
70
ts/classes.post.ts
Normal file
70
ts/classes.post.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import type { Ghost } from './classes.ghost.js';
|
||||
import * as plugins from './ghost.plugins.js';
|
||||
|
||||
export interface IPostOptions {
|
||||
id: string;
|
||||
title: string;
|
||||
html: string;
|
||||
excerpt?: string;
|
||||
feature_image?: string;
|
||||
[key: string]: any; // To allow for additional properties
|
||||
}
|
||||
|
||||
export class Post {
|
||||
public ghostInstanceRef: Ghost;
|
||||
private postData: IPostOptions;
|
||||
|
||||
constructor(ghostInstanceRefArg: Ghost, postData: IPostOptions) {
|
||||
this.ghostInstanceRef = ghostInstanceRefArg;
|
||||
this.postData = postData;
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return this.postData.id;
|
||||
}
|
||||
|
||||
public getTitle(): string {
|
||||
return this.postData.title;
|
||||
}
|
||||
|
||||
public getHtml(): string {
|
||||
return this.postData.html;
|
||||
}
|
||||
|
||||
public getExcerpt(): string {
|
||||
return this.postData.excerpt || this.generateExcerpt();
|
||||
}
|
||||
|
||||
public getFeatureImage(): string | undefined {
|
||||
return this.postData.feature_image;
|
||||
}
|
||||
|
||||
public toJson(): IPostOptions {
|
||||
return this.postData;
|
||||
}
|
||||
|
||||
public async update(postData: IPostOptions): Promise<Post> {
|
||||
try {
|
||||
const updatedPostData = await this.ghostInstanceRef.adminApi.posts.edit(postData);
|
||||
this.postData = updatedPostData;
|
||||
return this;
|
||||
} catch (error) {
|
||||
console.error('Error updating post:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async delete(): Promise<void> {
|
||||
try {
|
||||
await this.ghostInstanceRef.adminApi.posts.delete({ id: this.getId() });
|
||||
} catch (error) {
|
||||
console.error(`Error deleting post with id ${this.getId()}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private generateExcerpt(length: number = 100): string {
|
||||
const plainText = this.postData.html.replace(/<[^>]+>/g, ''); // Strip HTML tags
|
||||
return plainText.length > length ? plainText.substring(0, length) + '...' : plainText;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user