2024-07-01 14:32:22 +00:00
|
|
|
import type { Ghost } from './classes.ghost.js';
|
|
|
|
import * as plugins from './ghost.plugins.js';
|
|
|
|
|
2024-07-06 15:47:06 +00:00
|
|
|
export interface IAuthor {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
slug: string;
|
|
|
|
profile_image?: string;
|
|
|
|
cover_image?: string;
|
|
|
|
bio?: string;
|
|
|
|
website?: string;
|
|
|
|
location?: string;
|
|
|
|
facebook?: string;
|
|
|
|
twitter?: string;
|
|
|
|
meta_title?: string;
|
|
|
|
meta_description?: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ITag {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
slug: string;
|
|
|
|
description?: string;
|
|
|
|
feature_image?: string;
|
|
|
|
visibility: string;
|
|
|
|
og_image?: string;
|
|
|
|
og_title?: string;
|
|
|
|
og_description?: string;
|
|
|
|
twitter_image?: string;
|
|
|
|
twitter_title?: string;
|
|
|
|
twitter_description?: string;
|
|
|
|
meta_title?: string;
|
|
|
|
meta_description?: string;
|
|
|
|
codeinjection_head?: string;
|
|
|
|
codeinjection_foot?: string;
|
|
|
|
canonical_url?: string;
|
|
|
|
accent_color?: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
2024-07-01 14:32:22 +00:00
|
|
|
export interface IPostOptions {
|
|
|
|
id: string;
|
2024-07-06 15:47:06 +00:00
|
|
|
uuid: string;
|
2024-07-01 14:32:22 +00:00
|
|
|
title: string;
|
2024-07-06 15:47:06 +00:00
|
|
|
slug: string;
|
2024-07-01 14:32:22 +00:00
|
|
|
html: string;
|
2024-07-06 15:47:06 +00:00
|
|
|
comment_id: string;
|
2024-07-01 14:32:22 +00:00
|
|
|
feature_image?: string;
|
2024-07-06 15:47:06 +00:00
|
|
|
featured: boolean;
|
|
|
|
visibility: string;
|
|
|
|
created_at: string;
|
|
|
|
updated_at: string;
|
|
|
|
published_at: string;
|
|
|
|
custom_excerpt?: string;
|
|
|
|
codeinjection_head?: string | null;
|
|
|
|
codeinjection_foot?: string | null;
|
|
|
|
custom_template?: string | null;
|
|
|
|
canonical_url?: string | null;
|
|
|
|
url: string;
|
|
|
|
excerpt?: string;
|
|
|
|
reading_time: number;
|
|
|
|
access: boolean;
|
|
|
|
comments: boolean;
|
|
|
|
og_image?: string | null;
|
|
|
|
og_title?: string | null;
|
|
|
|
og_description?: string | null;
|
|
|
|
twitter_image?: string | null;
|
|
|
|
twitter_title?: string | null;
|
|
|
|
twitter_description?: string | null;
|
|
|
|
meta_title?: string | null;
|
|
|
|
meta_description?: string | null;
|
|
|
|
email_subject?: string | null;
|
|
|
|
frontmatter?: string | null;
|
|
|
|
feature_image_alt?: string | null;
|
|
|
|
feature_image_caption?: string | null;
|
|
|
|
authors: IAuthor[];
|
|
|
|
tags: ITag[];
|
|
|
|
primary_author: IAuthor;
|
|
|
|
primary_tag: ITag;
|
2024-07-01 14:32:22 +00:00
|
|
|
[key: string]: any; // To allow for additional properties
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Post {
|
|
|
|
public ghostInstanceRef: Ghost;
|
2024-07-06 15:47:06 +00:00
|
|
|
public postData: IPostOptions;
|
2024-07-01 14:32:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-07-06 15:47:06 +00:00
|
|
|
public getAuthor(): IAuthor {
|
|
|
|
return this.postData.primary_author;
|
|
|
|
}
|
|
|
|
|
2024-07-01 14:32:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|