Skip to content

Commit

Permalink
rss feed
Browse files Browse the repository at this point in the history
  • Loading branch information
taskylizard committed Dec 31, 2023
1 parent 9baef43 commit f8572d0
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 66 deletions.
7 changes: 5 additions & 2 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defineConfig } from "vitepress";
import UnoCSS from "unocss/vite";
import { presetUno, presetAttributify, presetIcons } from "unocss";
import consola from "consola";
import { commitRef, feedback, meta, socials } from "./constants";
import { generateImages, generateMeta } from "./hooks";
import { generateImages, generateMeta, generateFeed } from "./hooks";
import { toggleStarredPlugin } from "./markdown/toggleStarred";
import { base64DecodePlugin } from "./markdown/base64";

Expand Down Expand Up @@ -35,7 +36,9 @@ export default defineConfig({
],
transformHead: async (context) => generateMeta(context, meta.hostname),
buildEnd: async (context) => {
generateImages(context);
generateImages(context)
.then(() => generateFeed(context))
.finally(() => consola.success("Success!"));
},
vite: {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion .vitepress/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { DefaultTheme } from "vitepress";
export const meta = {
name: "FreeMediaHeckYeah",
description: "The largest collection of free stuff on the internet!",
hostname: "https://fmhy.pages.dev",
hostname: "https://fmhy.net",
keywords: ["stream", "movies", "gaming", "reading", "anime"],
};

Expand Down
2 changes: 2 additions & 0 deletions .vitepress/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

export * from "./meta";
export * from "./opengraph";
export * from "./rss";
export * from "./satoriConfig";
4 changes: 3 additions & 1 deletion .vitepress/hooks/opengraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createContentLoader } from "vitepress";
import type { ContentData, SiteConfig } from "vitepress";
import { type SatoriOptions, satoriVue } from "x-satori/vue";
import { renderAsync } from "@resvg/resvg-js";
import consola from "consola";

const __dirname = dirname(fileURLToPath(import.meta.url));
const __fonts = resolve(__dirname, "../fonts");
Expand Down Expand Up @@ -48,6 +49,7 @@ export async function generateImages(config: SiteConfig): Promise<void> {
fonts,
});
}
return consola.info("Generated opengraph images.");
}

interface GenerateImagesOptions {
Expand Down Expand Up @@ -90,5 +92,5 @@ async function generateImage({

await mkdir(outputFolder, { recursive: true });

return await writeFile(outputFile, render.asPng());
await writeFile(outputFile, render.asPng());
}
42 changes: 42 additions & 0 deletions .vitepress/hooks/rss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from "node:path";
import { writeFileSync } from "node:fs";
import { Feed } from "feed";
import { createContentLoader, type ContentData, type SiteConfig } from "vitepress";
import consola from "consola";
import { meta } from "../constants";

export async function generateFeed(config: SiteConfig): Promise<void> {
const feed: Feed = new Feed({
id: meta.hostname,
link: meta.hostname,
title: `FMHY blog`,
description: meta.description,
language: "en-US",
image: "https://github.com/fmhy.png",
favicon: `${meta.hostname}/favicon.ico`,
copyright: `Copyright (c) 2023-present FMHY`,
});

const posts: ContentData[] = await createContentLoader("posts/*.md", {
excerpt: true,
render: true,
transform: (rawData) => {
return rawData.sort((a, b) => {
return Number(new Date(b.frontmatter.date)) - Number(new Date(a.frontmatter.date));
});
},
}).load();

for (const { url, frontmatter, html } of posts) {
feed.addItem({
title: frontmatter.title as string,
id: `${meta.hostname}${url.replace(/\/\d+\./, "/")}`,
link: `${meta.hostname}${url.replace(/\/\d+\./, "/")}`,
date: frontmatter.date,
content: html!,
});
}

writeFileSync(path.join(config.outDir, "feed.rss"), feed.rss2());
return consola.info("Generated rss feed.");
}
39 changes: 0 additions & 39 deletions .vitepress/rss.ts

This file was deleted.

27 changes: 27 additions & 0 deletions .vitepress/theme/Posts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const formatDate = (raw: string): string => {
<section>
<h1 class="flex items-center gap-2">Posts</h1>
<p>Everything from Monthly Updates to fmhy updates.</p>

We also have a
<a href="/feed.rss" title="RSS feed" class="VPBadge tip">
<div class="i-carbon-rss" />
RSS feed </a
>.
</section>
<template v-for="year in Object.keys(posts).reverse()" :key="year">
<h2>{{ year }}</h2>
Expand All @@ -35,3 +41,24 @@ const formatDate = (raw: string): string => {
</template>
</div>
</template>

<style scoped>
.VPBadge {
border: 1px solid transparent;
border-radius: 8px;
display: inline-flex;
margin-left: 2px;
padding: 0 10px;
line-height: 22px;
font-size: 12px;
font-weight: 500;
transform: translateY(-2px);
align-items: center;
gap: 0.2rem;
padding-right: 10px;
vertical-align: middle;
color: var(--vp-badge-tip-text);
background-color: var(--vp-custom-block-info-bg);
border-color: var(--vp-custom-block-tip-outline);
}
</style>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"api:build": "nitropack build",
"api:preview": "node .output/server/index.mjs",
"og:dev": "x-satori -t ./.vitepress/hooks/Template.vue -c ./.vitepress/hooks/satoriConfig.ts --dev",
"format": "prettier -w --cache .",
"format": "prettier -w --cache --check .",
"lint": "eslint .vitepress -f pretty --cache",
"postinstall": "nitropack prepare"
},
"dependencies": {
"@headlessui/vue": "^1.7.16",
"@resvg/resvg-js": "^2.6.0",
"consola": "^3.2.3",
"feed": "^4.2.2",
"fs-extra": "^11.2.0",
"itty-fetcher": "^0.9.4",
Expand Down
43 changes: 21 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8572d0

Please sign in to comment.