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.
- Loading branch information
Showing
8 changed files
with
308 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@storipress/karbon': patch | ||
--- | ||
|
||
fix: missing author in atom feed |
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
72 changes: 72 additions & 0 deletions
72
packages/karbon/src/runtime/lib/__tests__/__snapshots__/feed.spec.ts.snap
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`addFeedPageLinks > can add feed page links 1`] = ` | ||
"<?xml version="1.0" encoding="utf-8"?> | ||
<feed xmlns="http://www.w3.org/2005/Atom"> | ||
<id>https://example.com/</id> | ||
<title>site name</title> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
<generator>https://github.com/jpmonette/feed</generator> | ||
<link rel="alternate" href="https://example.com/"></link> | ||
<link rel="self" href="https://example.com/atom.xml"></link> | ||
<link rel="previous" href="https://example.com/atom.xml?page=1"></link> | ||
<link rel="next" href="https://example.com/atom.xml?page=3"></link> | ||
<subtitle>site description</subtitle> | ||
<rights>© site name 2024 All Rights Reserved</rights> | ||
<entry> | ||
<title type="html"> | ||
<![CDATA[title]]> | ||
</title> | ||
<id>https://example.com/post</id> | ||
<link href="https://example.com/post"></link> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
</entry> | ||
</feed> | ||
" | ||
`; | ||
|
||
exports[`createFeed > can create feed with default config 1`] = ` | ||
"<?xml version="1.0" encoding="utf-8"?> | ||
<feed xmlns="http://www.w3.org/2005/Atom"> | ||
<id>https://example.com/</id> | ||
<title>site name</title> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
<generator>https://github.com/jpmonette/feed</generator> | ||
<link rel="alternate" href="https://example.com/"/> | ||
<link rel="self" href="https://example.com/atom.xml"/> | ||
<subtitle>site description</subtitle> | ||
<rights>© site name 2024 All Rights Reserved</rights> | ||
<entry> | ||
<title type="html"><![CDATA[title]]></title> | ||
<id>https://example.com/post</id> | ||
<link href="https://example.com/post"/> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
</entry> | ||
</feed>" | ||
`; | ||
|
||
exports[`generateAtomFeed > can generate atom feed 1`] = ` | ||
"<?xml version="1.0" encoding="utf-8"?> | ||
<feed xmlns="http://www.w3.org/2005/Atom"> | ||
<id>https://example.com/</id> | ||
<title>site name</title> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
<generator>https://github.com/jpmonette/feed</generator> | ||
<link rel="alternate" href="https://example.com/"/> | ||
<link rel="self" href="https://example.com/atom.xml"/> | ||
<subtitle>site description</subtitle> | ||
<rights>© site name 2024 All Rights Reserved</rights> | ||
<entry> | ||
<title type="html"><![CDATA[title]]></title> | ||
<id>https://example.com/posts/slug</id> | ||
<link href="https://example.com/posts/slug"/> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
<summary type="html"><![CDATA[plaintext]]></summary> | ||
<content type="html"><![CDATA[html]]></content> | ||
<author> | ||
<name>author</name> | ||
</author> | ||
<published>2024-04-24T00:00:00.000Z</published> | ||
</entry> | ||
</feed>" | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { addFeedPageLinks, createFeed, generateAtomFeed } from '../feed' | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
vi.setSystemTime(new Date('2024-04-24T00:00:00.000Z')) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
describe('createFeed', () => { | ||
it('can create feed with default config', () => { | ||
const feed = createFeed({ | ||
siteName: 'site name', | ||
siteDescription: 'site description', | ||
siteUrl: 'https://example.com', | ||
feedUrl: 'atom.xml', | ||
}) | ||
|
||
feed.addItem({ | ||
title: 'title', | ||
date: new Date('2024-04-24T00:00:00.000Z'), | ||
link: 'https://example.com/post', | ||
}) | ||
|
||
expect(feed.atom1()).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('generateAtomFeed', () => { | ||
it('can generate atom feed', () => { | ||
const atom = generateAtomFeed({ | ||
articles: [ | ||
{ | ||
id: '1', | ||
slug: 'slug', | ||
authors: [{ name: 'author' }] as any, | ||
title: 'title', | ||
html: 'html', | ||
plaintext: 'plaintext', | ||
updated_at: '2024-04-24T00:00:00.000Z', | ||
published_at: '2024-04-24T00:00:00.000Z', | ||
}, | ||
], | ||
getArticleURL: () => '/posts/slug', | ||
siteName: 'site name', | ||
siteDescription: 'site description', | ||
siteUrl: 'https://example.com', | ||
feedUrl: 'atom.xml', | ||
}) | ||
|
||
expect(atom).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
const ATOM_FIXTURE = ` | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<feed xmlns="http://www.w3.org/2005/Atom"> | ||
<id>https://example.com/</id> | ||
<title>site name</title> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
<generator>https://github.com/jpmonette/feed</generator> | ||
<link rel="alternate" href="https://example.com/"/> | ||
<link rel="self" href="https://example.com/atom.xml"/> | ||
<subtitle>site description</subtitle> | ||
<rights>© site name 2024 All Rights Reserved</rights> | ||
<entry> | ||
<title type="html"><![CDATA[title]]></title> | ||
<id>https://example.com/post</id> | ||
<link href="https://example.com/post"/> | ||
<updated>2024-04-24T00:00:00.000Z</updated> | ||
</entry> | ||
</feed> | ||
` | ||
|
||
describe('addFeedPageLinks', () => { | ||
it('can add feed page links', () => { | ||
expect(addFeedPageLinks(ATOM_FIXTURE, 'https://example.com', 2, 3)).toMatchSnapshot() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Feed } from 'feed' | ||
import { encodePath, joinURL, withQuery, withTrailingSlash, withoutTrailingSlash } from 'ufo' | ||
import { XMLBuilder, XMLParser } from 'fast-xml-parser' | ||
import type { _NormalizeArticle } from '../api/normalize-article' | ||
|
||
export function createFeed({ | ||
siteUrl, | ||
siteName, | ||
siteDescription, | ||
feedUrl, | ||
}: { | ||
siteUrl: string | ||
siteName: string | ||
siteDescription: string | ||
feedUrl: string | ||
}) { | ||
return new Feed({ | ||
id: withTrailingSlash(siteUrl), | ||
link: withTrailingSlash(siteUrl), | ||
title: siteName, | ||
description: siteDescription, | ||
updated: new Date(), | ||
feedLinks: { | ||
atom: joinURL(siteUrl, feedUrl), | ||
}, | ||
copyright: `© ${siteName} ${new Date().getFullYear()} All Rights Reserved`, | ||
}) | ||
} | ||
|
||
export function addFeedPageLinks(atomXml: string, siteUrl: string, currentPage: number, maxPage: number): string { | ||
const option = { | ||
ignoreAttributes: false, | ||
attributeNamePrefix: '@_', | ||
cdataPropName: '__cdata', | ||
format: true, | ||
} | ||
const parser = new XMLParser(option) | ||
const builder = new XMLBuilder(option) | ||
|
||
const atomJson = parser.parse(atomXml) | ||
|
||
const rssUrl = `${withoutTrailingSlash(siteUrl)}/atom.xml` | ||
const previousLink = | ||
currentPage > 1 ? [{ '@_rel': 'previous', '@_href': withQuery(rssUrl, { page: currentPage - 1 }) }] : [] | ||
const nextLink = | ||
currentPage < maxPage ? [{ '@_rel': 'next', '@_href': withQuery(rssUrl, { page: currentPage + 1 }) }] : [] | ||
|
||
const buildAtomXml = builder.build({ | ||
...atomJson, | ||
feed: { | ||
...atomJson.feed, | ||
link: [...atomJson.feed.link, ...previousLink, ...nextLink], | ||
}, | ||
}) | ||
return buildAtomXml | ||
} | ||
|
||
export type FeedArticle = Pick< | ||
_NormalizeArticle, | ||
'id' | 'slug' | 'plaintext' | 'authors' | 'updated_at' | 'published_at' | 'title' | 'html' | ||
> | ||
|
||
export interface GenerateAtomFeedInput { | ||
articles: FeedArticle[] | ||
siteUrl: string | ||
siteName: string | ||
siteDescription: string | ||
feedUrl: string | ||
getArticleURL: (article: FeedArticle) => string | ||
} | ||
|
||
export function generateAtomFeed({ | ||
articles, | ||
siteUrl, | ||
siteName, | ||
siteDescription, | ||
feedUrl, | ||
getArticleURL, | ||
}: GenerateAtomFeedInput): string { | ||
const feed = createFeed({ | ||
siteUrl, | ||
siteName, | ||
siteDescription, | ||
feedUrl, | ||
}) | ||
|
||
articles | ||
.filter((article) => article.published_at) | ||
.forEach((article) => { | ||
const id = encodePath(getArticleURL(article)) | ||
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, | ||
}) | ||
}) | ||
|
||
return feed.atom1() | ||
} |
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
Oops, something went wrong.