summaryrefslogtreecommitdiff
path: root/src/loaders/pleroma.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/loaders/pleroma.ts')
-rw-r--r--src/loaders/pleroma.ts110
1 files changed, 53 insertions, 57 deletions
diff --git a/src/loaders/pleroma.ts b/src/loaders/pleroma.ts
index c57a22b..b3fa8ff 100644
--- a/src/loaders/pleroma.ts
+++ b/src/loaders/pleroma.ts
@@ -265,12 +265,8 @@ function extractTrailingHashtags(content: string): {
/**
* Merge thread posts into a single content structure with image grids per segment
*/
-function mergeThreadContent(chain: PleromaStatus[]): {
- content: string;
- attachments: Array<{ url: string; type: string }>;
-} {
+function mergeThreadContent(chain: PleromaStatus[]): string {
const segments: string[] = [];
- const allAttachments: Array<{ url: string; type: string }> = [];
const allTags = new Set<string>(); // Collect all tags from all segments
for (const post of chain) {
@@ -286,28 +282,9 @@ function mergeThreadContent(chain: PleromaStatus[]): {
let segment = mainContent;
// Add image attachments as HTML grid after the text
- const imageAttachments = post.media_attachments.filter(
- (attachment) => attachment.type === "image",
- );
-
- if (imageAttachments.length > 0) {
- // Build HTML grid for images
- const imageGrid = `
-<div class="mt-4 mb-4 grid grid-cols-1 gap-4 sm:grid-cols-2">
-${imageAttachments
- .map((attachment) => {
- const description = attachment.description || "Image";
- allAttachments.push({
- url: attachment.url,
- type: `image/${attachment.url.split(".").pop() || "jpeg"}`,
- });
- return `<a href="${attachment.url}" target="_blank" rel="noopener noreferrer" class="block overflow-hidden rounded-lg border border-gray-200 transition-colors hover:border-gray-300 dark:border-gray-700 dark:hover:border-gray-600">
-<img src="${attachment.url}" alt="${description}" class="h-48 w-full object-cover" loading="lazy" />
-</a>`;
- })
- .join("\n")}
-</div>`;
+ const imageGrid = buildImageGridHtml(post.media_attachments);
+ if (imageGrid) {
segment = `${segment}\n\n${imageGrid}`;
}
@@ -323,7 +300,7 @@ ${imageAttachments
content = `${content}\n\n${tagLine}`;
}
- return { content, attachments: [] }; // Return empty attachments to avoid duplicate grid at end
+ return content;
}
async function getAccountId(
@@ -539,6 +516,30 @@ function cleanContent(htmlContent: string): string {
}
/**
+ * Build HTML grid for image attachments
+ * Returns empty string if no image attachments are provided
+ */
+function buildImageGridHtml(attachments: PleromaMediaAttachment[]): string {
+ const imageAttachments = attachments.filter((attachment) => attachment.type === "image");
+
+ if (imageAttachments.length === 0) {
+ return "";
+ }
+
+ return `
+<div class="mt-4 mb-4 grid grid-cols-1 gap-4 sm:grid-cols-2">
+${imageAttachments
+ .map((attachment) => {
+ const description = attachment.description || "Image";
+ return `<a href="${attachment.url}" target="_blank" rel="noopener noreferrer" class="block overflow-hidden rounded-lg border border-gray-200 transition-colors hover:border-gray-300 dark:border-gray-700 dark:hover:border-gray-600">
+<img src="${attachment.url}" alt="${description}" class="h-48 w-full object-cover" loading="lazy" />
+</a>`;
+ })
+ .join("\n")}
+</div>`;
+}
+
+/**
* Replace all hashtags in content with internal tag links
* Handles both plain #hashtags and existing markdown links [#tag](url)
* Returns modified content and extracted tags array
@@ -587,7 +588,7 @@ function replacePleromaLinks(
const markdownLinkRegex = new RegExp(`\\[([^\\]]+)\\]\\(${noticePattern}\\)`, "g");
let modifiedContent = content.replace(markdownLinkRegex, (match, linkText, statusId) => {
if (existingPostIds.has(statusId)) {
- return `[${linkText}](/micro/pleroma-${statusId}/)`;
+ return `[${linkText}](/posts/pleroma-${statusId}/)`;
}
return match; // Keep original if post doesn't exist
});
@@ -597,7 +598,7 @@ function replacePleromaLinks(
const plainUrlRegex = new RegExp(`(?<!\\()${noticePattern}(?!\\))`, "g");
modifiedContent = modifiedContent.replace(plainUrlRegex, (match, statusId) => {
if (existingPostIds.has(statusId)) {
- return `/micro/pleroma-${statusId}/`;
+ return `/posts/pleroma-${statusId}/`;
}
return match; // Keep original if post doesn't exist
});
@@ -679,7 +680,6 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
try {
const content = status.content || "";
let cleanedContent: string;
- let attachments: Array<{ url: string; type: string }>;
let postId: string;
let sourceUrl: string;
let tags: string[];
@@ -695,13 +695,14 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
logger.info(`Built chain with ${chain.length} post(s) for thread ${status.id}`);
// Merge thread content
- const merged = mergeThreadContent(chain);
- const { content: contentWithTags, tags: extractedTags } = replaceHashtagsWithLinks(
- merged.content,
- );
- tags = [...extractedTags, "micro"];
+ const mergedContent = mergeThreadContent(chain);
+ const { content: contentWithTags, tags: extractedTags } =
+ replaceHashtagsWithLinks(mergedContent);
+ // Add microblog tag if not already present
+ tags = extractedTags.includes("microblog")
+ ? extractedTags
+ : [...extractedTags, "microblog"];
cleanedContent = replacePleromaLinks(contentWithTags, instanceUrl, allPostIds);
- attachments = merged.attachments;
postId = status.id;
sourceUrl = status.url;
} else {
@@ -709,31 +710,26 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
const rawContent = cleanContent(content);
const { content: contentWithTags, tags: extractedTags } =
replaceHashtagsWithLinks(rawContent);
- tags = [...extractedTags, "micro"];
- cleanedContent = replacePleromaLinks(contentWithTags, instanceUrl, allPostIds);
+ // Add microblog tag if not already present
+ tags = extractedTags.includes("microblog")
+ ? extractedTags
+ : [...extractedTags, "microblog"];
+ const contentWithLinks = replacePleromaLinks(
+ contentWithTags,
+ instanceUrl,
+ allPostIds,
+ );
+
+ // Build image grid HTML and append to content for RSS feeds
+ const imageGrid = buildImageGridHtml(status.media_attachments);
+ cleanedContent = imageGrid ? `${contentWithLinks}\n\n${imageGrid}` : contentWithLinks;
+
postId = status.id;
sourceUrl = status.url;
-
- // Extract image attachments only
- attachments = status.media_attachments
- .filter((attachment) => attachment.type === "image")
- .map((attachment) => ({
- url: attachment.url,
- type: `image/${attachment.url.split(".").pop() || "jpeg"}`,
- alt: attachment.description || undefined,
- }));
}
const title = extractTitle(cleanedContent);
- // Extract author information from status account
- const author = {
- username: status.account.username,
- displayName: status.account.display_name || undefined,
- acct: status.account.acct,
- url: status.account.url || undefined,
- };
-
// Create note entry
store.set({
id: `pleroma-${postId}`,
@@ -743,10 +739,10 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
cleanedContent.substring(0, 160) + (cleanedContent.length > 160 ? "..." : ""),
publishDate: new Date(status.created_at),
sourceUrl,
- author,
- attachments,
language: status.language || undefined,
tags,
+ draft: false,
+ author: "Dawid",
},
body: cleanedContent,
rendered: {