owainwilliams_umbPublisher/services/ContentParser.ts
Owain Williams 9c713af3bf [UPDATE] Release version 1.2.0
- Updated main.ts to include new services
- Modified generateGuid method for better uniqueness
- Added ContentParser service for content parsing
- Added DocumentService for document management
- Implemented ErrorHandler service for improved error handling
- Created SettingsValidator service to validate configuration settings
- Developed UmbracoApiService for interacting with Umbraco API
2025-10-30 14:37:23 +00:00

52 lines
No EOL
1.6 KiB
TypeScript

import { MarkdownView, Notice, App } from 'obsidian';
export interface ParsedContent {
title: string;
tags: string[];
featured: boolean;
status: string;
excerpt?: string;
feature_image?: string;
content: string;
}
export class ContentParser {
constructor(private app: App) {}
async parseContent(view: MarkdownView): Promise<ParsedContent | null> {
const noteFile = view.app.workspace.getActiveFile();
if (!noteFile) {
new Notice('No active file found.');
return null;
}
const fileCache = this.app.metadataCache.getFileCache(noteFile);
const metaMatter = fileCache?.frontmatter;
const fileContent = await this.app.vault.read(noteFile);
let content = fileContent;
if (fileCache?.frontmatterPosition) {
const { start, end } = fileCache.frontmatterPosition;
const lines = fileContent.split('\n');
content = lines.slice(end.line + 1).join('\n');
}
return {
title: metaMatter?.title || view.file?.basename || 'Untitled',
tags: metaMatter?.tags || [],
featured: metaMatter?.featured || false,
status: metaMatter?.published ? "published" : "draft",
excerpt: metaMatter?.excerpt || undefined,
feature_image: metaMatter?.feature_image || undefined,
content: content,
};
}
getActiveFileTitle(): string | null {
const activeLeaf = this.app.workspace.activeLeaf;
if (activeLeaf && activeLeaf.getDisplayText) {
return activeLeaf.getDisplayText();
}
return null;
}
}