summaryrefslogtreecommitdiff
path: root/src/loaders
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-07-03 14:23:49 +0300
committerDawid Rycerz <dawid@rycerz.xyz>2025-07-03 14:23:49 +0300
commite28e2c961659f48177cf8ce39eeb39480b221535 (patch)
tree1138c3de714524db5eadab19df528402f42d8f67 /src/loaders
parent4b9018b6d92ef8f1854d9dc44625295c2acd3fb3 (diff)
Fix markdown rendering of pleroma posts
Diffstat (limited to 'src/loaders')
-rw-r--r--src/loaders/pleroma.ts27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/loaders/pleroma.ts b/src/loaders/pleroma.ts
index dc6a05c..a0b169f 100644
--- a/src/loaders/pleroma.ts
+++ b/src/loaders/pleroma.ts
@@ -1,6 +1,7 @@
import type { Loader } from "astro/loaders";
import { XMLParser } from "fast-xml-parser";
import TurndownService from "turndown";
+import { marked } from "marked";
interface PleromaFeedConfig {
instanceUrl: string;
@@ -192,6 +193,20 @@ function cleanContent(htmlContent: string): string {
return markdown.trim().replace(/\n\s*\n\s*\n/g, "\n\n");
}
+function markdownToHtml(markdown: string): string {
+ // Configure marked options for safe rendering
+ marked.setOptions({
+ breaks: true, // Convert line breaks to <br>
+ gfm: true, // GitHub flavored markdown
+ });
+
+ // Convert markdown to HTML
+ const html = marked.parse(markdown);
+
+ // Return as string (marked.parse can return string or Promise<string>)
+ return typeof html === 'string' ? html : '';
+}
+
function extractTitle(content: string): string {
// Extract first line or first sentence as title
const firstLine = content.split("\n")[0];
@@ -296,6 +311,9 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
// Extract post ID from the entry ID
const postId = entry.id.split("/").pop() || entry.id;
+ // Extract source URL from the entry
+ const sourceUrl = entry.link?.find(link => link["@_rel"] === "alternate")?.["@_href"] || entry.id;
+
// Create note entry
store.set({
id: `pleroma-${postId}`,
@@ -304,10 +322,11 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
description:
cleanedContent.substring(0, 160) + (cleanedContent.length > 160 ? "..." : ""),
publishDate: new Date(entry.published),
+ sourceUrl,
},
body: cleanedContent,
rendered: {
- html: `<p>${cleanedContent.replace(/\n\n/g, "</p><p>")}</p>`,
+ html: markdownToHtml(cleanedContent),
},
});
@@ -341,6 +360,9 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
(typeof item.link === "string" ? item.link.split("/").pop() : null) ||
Math.random().toString(36);
+ // Use the link as source URL
+ const sourceUrl = typeof item.link === "string" ? item.link : item.guid || "";
+
// Create note entry
store.set({
id: `pleroma-${postId}`,
@@ -349,10 +371,11 @@ export function pleromaLoader(config: PleromaFeedConfig): Loader {
description:
cleanedContent.substring(0, 160) + (cleanedContent.length > 160 ? "..." : ""),
publishDate: new Date(item.pubDate),
+ sourceUrl,
},
body: cleanedContent,
rendered: {
- html: `<p>${cleanedContent.replace(/\n\n/g, "</p><p>")}</p>`,
+ html: markdownToHtml(cleanedContent),
},
});