Skip to content

Commit

Permalink
fix: transform markdown to html for rss (#82)
Browse files Browse the repository at this point in the history
* docs: switch English version as main CONTRIBUTING doc

* feat: add rss support

* docs: add rss doc

* fix: add missing /sub routing

* fix: add RSSService to App

* feat: Add support for remark-gfm and rehype-stringify in RSSService

* fix: remove async in foreach
  • Loading branch information
OXeu authored Jun 8, 2024
1 parent 0b51e6c commit 486a94e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"elysia-oauth2": "^1.2.0",
"feed": "^4.2.2",
"jose": "^5.3.0",
"rehype-stringify": "^10.0.0",
"remark-gfm": "^4.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.0",
"unified": "^11.0.4",
"url": "^0.11.3"
},
"trustedDependencies": [
Expand Down
19 changes: 16 additions & 3 deletions server/src/services/rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import * as schema from "../db/schema";
import { feeds, users } from "../db/schema";
import { extractImage } from "../utils/image";
import { createS3Client } from "../utils/s3";
import remarkGfm from "remark-gfm";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";

export const RSSService = (env: Env) => {
const endpoint = env.S3_ENDPOINT;
Expand Down Expand Up @@ -78,18 +83,26 @@ export async function rssCrontab(env: Env) {
}
}
});
feed_list.forEach(({ summary, content, user, ...other }) => {
for(const f of feed_list) {
const { summary, content, user, ...other } = f;
const file = await unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeStringify)
.process(content)
let contentHtml = file.toString()
feed.addItem({
title: other.title || "No title",
id: other.id?.toString() || "0",
link: `${frontendUrl}/feed/${other.id}`,
date: other.createdAt,
description: summary.length > 0 ? summary : content.length > 100 ? content.slice(0, 100) : content,
content: content,
content: contentHtml,
author: [{ name: user.username }],
image: extractImage(content) || user.avatar as string,
});
});
}
// save rss.xml to s3
console.log('save rss.xml to s3');
const bucket = env.S3_BUCKET;
Expand Down

0 comments on commit 486a94e

Please sign in to comment.