Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

feat: add published date to feed #350

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports[`mockTypesenseArticle 1`] = `
},
],
"title": "Mock Article",
"updated_at": 1704758778,
"updated_at": "2024-01-09T00:06:18.000Z",
}
`;

Expand Down
24 changes: 22 additions & 2 deletions packages/karbon/src/runtime/api/normalize-article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,22 @@ export interface RawSEO {
}

export function normalizeArticle(article: RawArticleLike | TypesenseArticleLike) {
const { title, blurb, seo, cover, plan, id, authors, desk, tags, published_at, html, plaintext, ...rest } = article
const {
title,
blurb,
seo,
cover,
plan,
id,
authors,
desk,
tags,
published_at,
updated_at,
html,
plaintext,
...rest
} = article

const articleFilter = useArticleFilter()
const rootDesk = desk?.desk ? { desk: { ...desk.desk, id: String(desk.desk.id) } } : {}
Expand All @@ -101,7 +116,8 @@ export function normalizeArticle(article: RawArticleLike | TypesenseArticleLike)
bio: articleFilter(bio),
bioHTML: bio,
// published_at could be unix timestamp
published_at: typeof published_at === 'string' ? published_at : new Date(published_at * 1000).toISOString(),
published_at: normalizeMaybeUnixTimestamp(published_at),
updated_at: normalizeMaybeUnixTimestamp(updated_at),
title: unwrapParagraph(title),
blurb: unwrapParagraph(blurb),
seo: destr<RawSEO>(seo),
Expand Down Expand Up @@ -139,6 +155,10 @@ export type NormalizeArticle = _NormalizeArticle & {
segments: Segment[]
}

function normalizeMaybeUnixTimestamp(date: number | string): string {
return typeof date === 'string' ? date : new Date(date * 1000).toISOString()
}

export function unwrapParagraph(input: string): string {
if (!input) {
return ''
Expand Down
4 changes: 3 additions & 1 deletion packages/karbon/src/runtime/routes/atom.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface TArticle {
plaintext: string
html: string
published_at: string
updated_at: string
}

export default defineEventHandler(async (e) => {
Expand Down Expand Up @@ -56,7 +57,8 @@ export default defineEventHandler(async (e) => {
id: joinURL(siteUrl, id),
link: joinURL(siteUrl, id),
description: article.plaintext.slice(0, 120),
date: new Date(article.published_at),
date: new Date(article.updated_at),
published: new Date(article.published_at),
author:
article.author?.map((author) => ({
name: author.name,
Expand Down