-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.xml.ts
More file actions
84 lines (78 loc) · 2.67 KB
/
Copy pathrss.xml.ts
File metadata and controls
84 lines (78 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { serverQueryContent } from '#content/server'
import jstoxml from 'jstoxml'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import remarkFrontmatter from 'remark-frontmatter'
import { unified } from 'unified'
const { toXML } = jstoxml
const parseHTML = async (md: string) => {
const html = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.use(remarkFrontmatter)
.process(md)
return html.value
}
export default defineEventHandler(async (event) => {
const config = useAppConfig()
const url = config.url?.replace(/\/$/, '')
const host = import.meta.dev ? event.node.req.headers.host : 'localhost:3000'
const docs = await serverQueryContent(event)
.where({ hidden: { $ne: true } })
.sort({ date: -1 })
.find()
const now = new Date()
const content = {
_name: 'rss',
_attrs: {
version: '2.0',
},
_content: {
channel: [
{ title: config.name },
{ description: config.description },
{ link: url },
{ docs: 'http://cyber.law.harvard.edu/rss/rss.html' },
{ language: config.language },
{ copyright: `All rights reserved ${now.getFullYear()}, ${config.name}` },
{ generator: 'bloggrify-smh' },
{
_name: 'cloud',
_attrs: {
domain: 'rpc.rsscloud.io',
port: 5337,
path: '/pleaseNotify',
registerProcedure: '',
protocol: 'http-post',
},
},
],
},
}
for (const post of docs) {
// docs.forEach(async (post) => {
const path = post._path
if (post.date) {
const mdUrl = `http://${host}/markdown${path}.md`
const md = await $fetch(mdUrl, { responseType: 'text' })
const parsedHTML = await parseHTML(md)
content._content.channel.push({
item: {
title: post.title ?? '-',
link: url + path,
guid: url + path,
// description: post.description,
description: `<![CDATA[${parsedHTML}]]>`,
pubDate: new Date(post.date).toUTCString(),
},
})
}
}
const configXML = {
indent: ' ',
}
event.node.res.setHeader('content-type', 'text/xml')
return toXML(content, configXML)
})