Deploy / validate-dispatch (push) Successful in 43s
Deploy / scheduled-rebuild-dev (push) Skipped
Deploy / scheduled-rebuild-master (push) Skipped
Deploy / rollback-dev (push) Skipped
Deploy / rollback-master (push) Skipped
Deploy / changes (push) Successful in 13s
Deploy / secret-scan (push) Successful in 9s
Deploy / app-quality (push) Successful in 11m21s
Deploy / security-deps (push) Successful in 14s
Deploy / context (push) Failing after 1s
Deploy / app-image (push) Skipped
Deploy / deploy-dev (push) Skipped
Deploy / deploy-master (push) Skipped
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
/**
|
|
* @fileoverview Hilfsfunktionen für die Behandlung von Beiträgen (Posts)
|
|
*
|
|
* Dieses Modul bietet Funktionen zum Sortieren von Beiträgen und Extrahieren von
|
|
* Textinhalten aus Markdown.
|
|
*
|
|
* @module utils/postUtils
|
|
*/
|
|
|
|
import type { PostMetadata } from "../types";
|
|
|
|
/**
|
|
* Sortiert eine Liste von Beiträgen nach Erstellungsdatum (neueste zuerst)
|
|
*
|
|
* @param {PostMetadata[]} posts - Array von Beitrags-Metadaten
|
|
* @returns {PostMetadata[]} Sortiertes Array (neueste zuerst)
|
|
*/
|
|
export function sortPostsByCreatedDate(posts: PostMetadata[]): PostMetadata[] {
|
|
return [...posts].sort((a, b) => {
|
|
const dateB = b.createdAt.getTime();
|
|
const dateA = a.createdAt.getTime();
|
|
return dateB - dateA;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Extrahiert reinen Text aus Markdown-Inhalten
|
|
*
|
|
* Entfernt HTML-Tags, Markdown-Syntax (Headings, Bold, etc.), Links und
|
|
* normalisiert Whitespace für eine saubere Textdarstellung.
|
|
*
|
|
* @param {string} content - Markdown-Textinhalt
|
|
* @returns {string} Reiner Textinhalt ohne Markdown-Syntax
|
|
*/
|
|
export function extractPlainText(content: string): string {
|
|
return content
|
|
.replace(/<[^>]+>/g, " ")
|
|
.replace(/[#>*_`~\\-]+/g, " ")
|
|
.replace(/\[(.*?)\]\(.*?\)/g, "$1")
|
|
.replace(/\s+/g, " ")
|
|
.trim();
|
|
}
|
|
|
|
/**
|
|
* Erstellt einen kurzen Auszug (Excerpt) aus Text
|
|
*
|
|
* Schneidet den Text nach der maxLength ab und fügt "..." hinzu,
|
|
* wenn der Text zu lang ist.
|
|
*
|
|
* @param {string} plainText - Der zu kürzende Text
|
|
* @param {number} maxLength - Maximale Länge des Auszugs
|
|
* @returns {string} Der gekürzte Auszug
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* createExcerpt("Das ist ein sehr langer Text", 10) // returns "Das ist e..."
|
|
* ```
|
|
*/
|
|
export function createExcerpt(plainText: string, maxLength: number): string {
|
|
if (plainText.length <= maxLength) {
|
|
return plainText;
|
|
}
|
|
return `${plainText.slice(0, Math.max(0, maxLength - 3))}...`;
|
|
}
|