mirror of
https://github.com/mmahdium/portfolio.git
synced 2026-08-15 20:52:49 +03:30
blog mvp completed
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const siteUrl = config.public.siteUrl || 'https://example.com'
|
||||
|
||||
// Detect locale from path
|
||||
const locale = 'en'
|
||||
|
||||
// Fetch published blog posts
|
||||
const posts = await serverQueryContent(event, `${locale}/blog`)
|
||||
.where({ draft: { $ne: true } })
|
||||
.sort({ date: -1 })
|
||||
.find()
|
||||
|
||||
const escapeXml = (str: string) => {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
}
|
||||
|
||||
const rssItems = posts
|
||||
.map((post) => {
|
||||
const link = `${siteUrl}${post._path}`
|
||||
const pubDate = new Date(post.date).toUTCString()
|
||||
|
||||
return `
|
||||
<item>
|
||||
<title>${escapeXml(post.title)}</title>
|
||||
<link>${escapeXml(link)}</link>
|
||||
<guid>${escapeXml(link)}</guid>
|
||||
<pubDate>${pubDate}</pubDate>
|
||||
<description>${escapeXml(post.description || '')}</description>
|
||||
</item>`
|
||||
})
|
||||
.join('')
|
||||
|
||||
const rss = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<channel>
|
||||
<title>Blog - ${escapeXml(config.public.siteName || 'My Site')}</title>
|
||||
<link>${siteUrl}/blog</link>
|
||||
<description>Latest blog posts</description>
|
||||
<language>${locale}</language>
|
||||
<atom:link href="${siteUrl}/blog/rss.xml" rel="self" type="application/rss+xml" />
|
||||
${rssItems}
|
||||
</channel>
|
||||
</rss>`
|
||||
|
||||
event.node.res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8')
|
||||
return rss
|
||||
})
|
||||
@@ -0,0 +1,53 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const siteUrl = config.public.siteUrl || 'https://example.com'
|
||||
|
||||
// Persian locale
|
||||
const locale = 'fa'
|
||||
|
||||
// Fetch published blog posts
|
||||
const posts = await serverQueryContent(event, `${locale}/blog`)
|
||||
.where({ draft: { $ne: true } })
|
||||
.sort({ date: -1 })
|
||||
.find()
|
||||
|
||||
const escapeXml = (str: string) => {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
}
|
||||
|
||||
const rssItems = posts
|
||||
.map((post) => {
|
||||
const link = `${siteUrl}${post._path}`
|
||||
const pubDate = new Date(post.date).toUTCString()
|
||||
|
||||
return `
|
||||
<item>
|
||||
<title>${escapeXml(post.title)}</title>
|
||||
<link>${escapeXml(link)}</link>
|
||||
<guid>${escapeXml(link)}</guid>
|
||||
<pubDate>${pubDate}</pubDate>
|
||||
<description>${escapeXml(post.description || '')}</description>
|
||||
</item>`
|
||||
})
|
||||
.join('')
|
||||
|
||||
const rss = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<channel>
|
||||
<title>وبلاگ - ${escapeXml(config.public.siteName || 'سایت من')}</title>
|
||||
<link>${siteUrl}/fa/blog</link>
|
||||
<description>آخرین پستهای وبلاگ</description>
|
||||
<language>${locale}</language>
|
||||
<atom:link href="${siteUrl}/fa/blog/rss.xml" rel="self" type="application/rss+xml" />
|
||||
${rssItems}
|
||||
</channel>
|
||||
</rss>`
|
||||
|
||||
event.node.res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8')
|
||||
return rss
|
||||
})
|
||||
Reference in New Issue
Block a user