substr/models/article.ts
Râu Cao 9305e9f718
All checks were successful
CI / Test and lint (push) Successful in 19s
Add code highlighting
2025-04-26 18:28:40 +04:00

79 lines
1.9 KiB
TypeScript

import { render as renderMarkdown } from "@deno/gfm";
import { nip19 } from "@nostr/tools";
import { NostrEvent as NEvent } from "@nostrify/nostrify";
import { replaceNostrUris } from "../nostr/links.ts";
import config from "../config.ts";
for (const language of config.prism.extraLanguages) {
await import(`npm:prismjs@1.29.0/components/prism-${language}.js`);
}
export default class Article {
event: NEvent;
constructor(event: NEvent) {
this.event = event;
}
get identifier(): string {
const tag = this.event.tags.find((t) => t[0] === "d");
return tag ? tag[1] : "";
}
get isDraft(): boolean {
return this.event.kind === 30024;
}
get url(): string {
return `${config.base_url}/${this.naddr}`;
}
get title(): string {
const tag = this.event.tags.find((t) => t[0] === "title");
return tag ? tag[1] : "Untitled";
}
get image(): string | undefined {
const tag = this.event.tags.find((t) => t[0] === "image");
return tag ? tag[1] : undefined;
}
get summary(): string {
const tag = this.event.tags.find((t) => t[0] === "summary");
return tag ? tag[1] : "";
}
get publishedAt(): number {
const tag = this.event.tags.find((t) => t[0] === "published_at");
return tag ? parseInt(tag[1]) : this.event.created_at;
}
get updatedAt(): number {
return this.event.created_at;
}
get content(): string {
return this.event.content;
}
get isDeleted(): boolean {
return !!this.event.tags.find((t) => t[0] === "deleted");
}
get naddr(): string {
return nip19.naddrEncode({
identifier: this.identifier,
pubkey: this.event.pubkey,
kind: this.event.kind,
relays: [config.relay_urls[0]],
});
}
async buildContentHtml(): Promise<string> {
let md = this.event.content.trim();
md = md.replace(`# ${this.title}\n`, "");
md = await replaceNostrUris(md);
return renderMarkdown(md);
}
}