This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use shared atom generating function
- Loading branch information
Showing
6 changed files
with
137 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,42 @@ | ||
import type { H3Event } from 'h3' | ||
import { defineEventHandler, getQuery, setHeader } from 'h3' | ||
import type { RuntimeConfig } from '@nuxt/schema' | ||
import { encodePath, joinURL } from 'ufo' | ||
import { addFeedPageLinks, createFeed, listFeedArticles } from '@storipress/karbon/internal' | ||
import type { Author } from '../composables/page-meta' | ||
import type { FeedArticle } from '@storipress/karbon/internal' | ||
import { addFeedPageLinks, generateAtomFeed, listFeedArticles } from '@storipress/karbon/internal' | ||
import type { _NormalizeArticle } from '../api/normalize-article' | ||
import { useRuntimeConfig } from '#imports' | ||
import urls from '#sp-internal/storipress-urls.mjs' | ||
|
||
interface TArticle { | ||
title: string | ||
id: string | ||
link: string | ||
description: string | ||
content: string | ||
date: Date | ||
authors: Author[] | ||
plaintext: string | ||
html: string | ||
published_at: string | ||
updated_at: string | ||
} | ||
|
||
export default defineEventHandler(async (event) => { | ||
setHeader(event, 'Content-Type', 'text/xml; charset=UTF-8') | ||
if (!process.dev) setHeader(event, 'Cache-Control', 'max-age=600, must-revalidate') | ||
|
||
const runtimeConfig = useRuntimeConfig() | ||
return await generateAtomFeed(runtimeConfig, event) | ||
}) | ||
|
||
const ARTICLES_PER_PAGE = 100 | ||
const siteUrl = runtimeConfig.public.siteUrl as string | ||
|
||
async function generateAtomFeed(runtimeConfig: RuntimeConfig, event: H3Event) { | ||
const articles = await listFeedArticles() | ||
const { currentPageArticles, currentPage, maxPage } = paginateArticle(event, await listFeedArticles()) | ||
|
||
const siteUrl = runtimeConfig.public.siteUrl as string | ||
const feed = createFeed({ | ||
const atomXml = generateAtomFeed({ | ||
articles: currentPageArticles, | ||
siteUrl, | ||
siteName: runtimeConfig.public.siteName as string, | ||
siteDescription: runtimeConfig.public.siteDescription as string, | ||
feedUrl: 'atom.xml', | ||
getArticleURL: (article) => urls.article.toURL(article, urls.article._context), | ||
}) | ||
|
||
const buildAtomXml = addFeedPageLinks(atomXml, siteUrl, currentPage, maxPage) | ||
|
||
return buildAtomXml | ||
}) | ||
|
||
const ARTICLES_PER_PAGE = 100 | ||
|
||
function paginateArticle(event: H3Event, articles: FeedArticle[]) { | ||
const queryString = getQuery(event) | ||
const page = Number(queryString.page) || 1 | ||
const maxPage = Math.ceil(articles.length / ARTICLES_PER_PAGE) | ||
const currentPage = page > maxPage ? maxPage : page | ||
const currentPageArticles = articles.slice((currentPage - 1) * ARTICLES_PER_PAGE, currentPage * ARTICLES_PER_PAGE) | ||
|
||
currentPageArticles | ||
.filter((article: TArticle) => article.published_at) | ||
.forEach((article: TArticle) => { | ||
const id = encodePath(urls.article.toURL(article, urls.article._context)) | ||
feed.addItem({ | ||
title: article.title, | ||
id: joinURL(siteUrl, id), | ||
link: joinURL(siteUrl, id), | ||
description: article.plaintext.slice(0, 120), | ||
date: new Date(article.updated_at), | ||
published: new Date(article.published_at), | ||
author: | ||
article.authors?.map((author) => ({ | ||
name: author.name, | ||
})) || [], | ||
content: article.html, | ||
}) | ||
}) | ||
|
||
const atomXml = feed.atom1() | ||
|
||
const buildAtomXml = addFeedPageLinks(atomXml, siteUrl, currentPage, maxPage) | ||
return buildAtomXml | ||
return { currentPageArticles, currentPage, maxPage } | ||
} |