mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-08 23:14:43 -04:00
(nuxt) migrate middleware/atom.js
This commit is contained in:
parent
433de45bfb
commit
d977d67c58
55
middleware/atom.global.ts
Normal file
55
middleware/atom.global.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { Feed } from 'feed';
|
||||
import { defineNuxtRouteMiddleware, useNuxtApp, useRuntimeConfig } from 'nuxt/app';
|
||||
import parseMarkdown from '~/src/parseMarkdown.ts';
|
||||
import useConfig from '~/composables/useConfig.ts';
|
||||
import type { Post } from '~/server/express/blog.ts';
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to) => {
|
||||
const { $translator: translator, ssrContext } = useNuxtApp();
|
||||
const res = ssrContext?.event.node.res;
|
||||
|
||||
if (!res || to.path !== '/blog.atom') {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = useConfig();
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
const posts = await $fetch<Post[]>('/api/blog');
|
||||
|
||||
const feed = new Feed({
|
||||
title: `${translator.translate('title')} • ${translator.translate('links.blog')}`,
|
||||
description: translator.translate('description'),
|
||||
id: runtimeConfig.public.baseUrl,
|
||||
link: `${runtimeConfig.public.baseUrl}/blog.atom`,
|
||||
language: config.locale,
|
||||
image: `${runtimeConfig.public.baseUrl}/icon.png`,
|
||||
favicon: `${runtimeConfig.public.baseUrl}/icon.png`,
|
||||
updated: new Date(posts[0].date),
|
||||
copyright: '',
|
||||
});
|
||||
|
||||
for (const post of posts) {
|
||||
const markdown = (await import(`../data/blog/${post.slug}.md`)).default;
|
||||
const parsed = await parseMarkdown(markdown, translator);
|
||||
|
||||
feed.addItem({
|
||||
title: post.title,
|
||||
id: `${runtimeConfig.public.baseUrl}/${config.links.blogRoute}/${post.slug}`,
|
||||
link: `${runtimeConfig.public.baseUrl}/${config.links.blogRoute}/${post.slug}`,
|
||||
description: parsed.intro ?? undefined,
|
||||
content: parsed.content ?? undefined,
|
||||
author: post.authors.map((author) => ({
|
||||
name: author,
|
||||
link: author.startsWith('@') ? `${runtimeConfig.public.baseUrl}/${author}` : undefined,
|
||||
})),
|
||||
date: new Date(post.date),
|
||||
image: post.hero ? `${runtimeConfig.public.baseUrl}${post.hero.src}` : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8');
|
||||
res.end(feed.atom1());
|
||||
|
||||
return new Promise(() => {}); // halt execution
|
||||
});
|
@ -1,49 +0,0 @@
|
||||
import { Feed } from 'feed';
|
||||
import parseMarkdown from '../src/parseMarkdown.ts';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
export default async function ({ app, route, res }) {
|
||||
if (!process.server || route.path !== '/blog.atom') {
|
||||
return;
|
||||
}
|
||||
|
||||
const posts = await (await fetch(`${process.env.BASE_URL}/api/blog`)).json();
|
||||
|
||||
const feed = new Feed({
|
||||
title: `${app.$t('title')} • ${app.$t('links.blog')}`,
|
||||
description: app.$t('description'),
|
||||
id: process.env.BASE_URL,
|
||||
link: `${process.env.BASE_URL}/blog.atom`,
|
||||
language: app.$config.locale,
|
||||
image: `${process.env.BASE_URL}/icon.png`,
|
||||
favicon: `${process.env.BASE_URL}/icon.png`,
|
||||
updated: new Date(posts[0].date),
|
||||
});
|
||||
|
||||
for (const post of posts) {
|
||||
const parsed = await parseMarkdown(
|
||||
(await import(`../locale/${app.$config.locale}/blog/${post.slug}.md`)).default,
|
||||
app.$translator,
|
||||
);
|
||||
|
||||
feed.addItem({
|
||||
title: post.title,
|
||||
id: `${process.env.BASE_URL}/${app.$config.links.blogRoute}/${post.slug}`,
|
||||
link: `${process.env.BASE_URL}/${app.$config.links.blogRoute}/${post.slug}`,
|
||||
description: parsed.intro,
|
||||
content: parsed.content,
|
||||
author: post.authors.map((author) => ({
|
||||
name: author,
|
||||
link: author.startsWith('@') ? `${process.env.BASE_URL}/${author}` : undefined,
|
||||
})),
|
||||
date: new Date(post.date),
|
||||
image: post.hero ? `${process.env.BASE_URL}${post.hero.src}` : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8');
|
||||
res.end(feed.atom1());
|
||||
|
||||
return new Promise(() => {}); // halt execution
|
||||
}
|
@ -534,7 +534,6 @@ exports.randomFillSync = randomFillSync`],
|
||||
}
|
||||
},
|
||||
},
|
||||
middleware: 'atom',
|
||||
loadingIndicator: {
|
||||
name: 'views/loading.html',
|
||||
},
|
||||
|
@ -4,7 +4,7 @@ import { handleErrorAsync } from '../../src/helpers.ts';
|
||||
import fs from 'fs';
|
||||
import { caches } from '../../src/cache.ts';
|
||||
|
||||
interface Post {
|
||||
export interface Post {
|
||||
slug: string;
|
||||
title: string;
|
||||
date: string;
|
||||
|
Loading…
x
Reference in New Issue
Block a user