-
Notifications
You must be signed in to change notification settings - Fork 6
Add author/dc:creator support to fulltext feed output using feedsmith #964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
11
commits into
master
Choose a base branch
from
copilot/add-author-element-to-fulltext-feed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−77
Draft
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c55df2b
Initial plan
Copilot 0fefe72
Add author/dc:creator support to fulltext feed output
Copilot d152925
Refactor: extract author utilities to separate module, fix CDATA inje…
Copilot 8d414eb
Plan: Replace feed library with feedsmith for native dc:creator support
Copilot 4ca6317
Replace feed library with feedsmith for native dc:creator support
Copilot 5aef183
Fix build: upgrade TypeScript to 5.x for feedsmith compatibility
Copilot 0d10fee
Address review: remove skipLibCheck, delete author.test.ts, use sprea…
Copilot 376d0ac
plan: fix feed processing for UN, FreshRSS, and AliasVault feeds
Copilot 6072ca5
Fix feed processing: add gzip support, Atom author/guid mapping, clea…
Copilot a28a9bf
Remove gzip handling from application code, revert to parser.parseURL()
Copilot 5b70a82
Remove customFields, use rss-parser generic type for Atom fields
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,190 @@ | ||
| // @ts-nocheck | ||
| import { Feed } from 'feed' | ||
| import type { FeedOptions, Item } from 'feed/lib/typings' | ||
| import { parseCreator, addDcCreators } from './author' | ||
|
|
||
| describe('parseCreator', () => { | ||
| test('parses name-only creator', () => { | ||
| const result = parseCreator('John Doe') | ||
| expect(result).toEqual({ name: 'John Doe' }) | ||
| }) | ||
|
|
||
| test('parses email (name) format', () => { | ||
| const result = parseCreator('john@example.com (John Doe)') | ||
| expect(result).toEqual({ email: 'john@example.com', name: 'John Doe' }) | ||
| }) | ||
|
|
||
| test('handles creator with just a username', () => { | ||
| const result = parseCreator('admin') | ||
| expect(result).toEqual({ name: 'admin' }) | ||
| }) | ||
|
|
||
| test('does not parse invalid email format', () => { | ||
| const result = parseCreator('not-an-email (Name)') | ||
| expect(result).toEqual({ name: 'not-an-email (Name)' }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('addDcCreators', () => { | ||
| function buildFeedXml(items: Item[]): { xml: string; items: Item[] } { | ||
| const feedOptions: FeedOptions = { | ||
| title: 'Test Feed', | ||
| id: 'https://example.com/feed', | ||
| link: 'https://example.com', | ||
| copyright: '', | ||
| } | ||
| const feed = new Feed(feedOptions) | ||
| for (const item of items) { | ||
| feed.addItem(item) | ||
| } | ||
| return { xml: feed.rss2(), items: feed.items } | ||
| } | ||
|
|
||
| test('adds dc:creator for item with name-only author', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Test Article', | ||
| link: 'https://example.com/article', | ||
| date: new Date('2024-01-01'), | ||
| content: 'Test content', | ||
| author: [{ name: 'John Doe' }], | ||
| }, | ||
| ] | ||
| const { xml, items: feedItems } = buildFeedXml(items) | ||
| const result = addDcCreators(xml, feedItems) | ||
|
|
||
| expect(result).toContain('<dc:creator><![CDATA[John Doe]]></dc:creator>') | ||
| expect(result).toContain('xmlns:dc=') | ||
| }) | ||
|
|
||
| test('adds dc:creator for item with email and name author', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Test Article', | ||
| link: 'https://example.com/article', | ||
| date: new Date('2024-01-01'), | ||
| content: 'Test content', | ||
| author: [{ name: 'John Doe', email: 'john@example.com' }], | ||
| }, | ||
| ] | ||
| const { xml, items: feedItems } = buildFeedXml(items) | ||
| const result = addDcCreators(xml, feedItems) | ||
|
|
||
| expect(result).toContain('<dc:creator><![CDATA[John Doe]]></dc:creator>') | ||
| // Should also have <author> element from feed library | ||
| expect(result).toContain('<author>john@example.com (John Doe)</author>') | ||
| }) | ||
|
|
||
| test('skips dc:creator for items without author', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Test Article', | ||
| link: 'https://example.com/article', | ||
| date: new Date('2024-01-01'), | ||
| content: 'Test content', | ||
| }, | ||
| ] | ||
| const { xml, items: feedItems } = buildFeedXml(items) | ||
| const result = addDcCreators(xml, feedItems) | ||
|
|
||
| expect(result).not.toContain('<dc:creator>') | ||
| }) | ||
|
|
||
| test('handles mixed items with and without authors', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Article 1', | ||
| link: 'https://example.com/1', | ||
| date: new Date('2024-01-01'), | ||
| content: 'Content 1', | ||
| author: [{ name: 'Author One' }], | ||
| }, | ||
| { | ||
| title: 'Article 2', | ||
| link: 'https://example.com/2', | ||
| date: new Date('2024-01-02'), | ||
| content: 'Content 2', | ||
| }, | ||
| { | ||
| title: 'Article 3', | ||
| link: 'https://example.com/3', | ||
| date: new Date('2024-01-03'), | ||
| content: 'Content 3', | ||
| author: [{ name: 'Author Three' }], | ||
| }, | ||
| ] | ||
| const { xml, items: feedItems } = buildFeedXml(items) | ||
| const result = addDcCreators(xml, feedItems) | ||
|
|
||
| expect(result).toContain('<dc:creator><![CDATA[Author One]]></dc:creator>') | ||
| expect(result).not.toContain('<dc:creator><![CDATA[Author Two]]></dc:creator>') | ||
| expect(result).toContain('<dc:creator><![CDATA[Author Three]]></dc:creator>') | ||
| }) | ||
|
|
||
| test('adds xmlns:dc namespace if not present', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Test Article', | ||
| link: 'https://example.com/article', | ||
| date: new Date('2024-01-01'), | ||
| author: [{ name: 'John Doe' }], | ||
| }, | ||
| ] | ||
| const feedOptions: FeedOptions = { | ||
| title: 'Test Feed', | ||
| id: 'https://example.com/feed', | ||
| link: 'https://example.com', | ||
| copyright: '', | ||
| } | ||
| const feed = new Feed(feedOptions) | ||
| for (const item of items) { | ||
| feed.addItem(item) | ||
| } | ||
| // Without content, the feed library won't add xmlns:dc | ||
| const xml = feed.rss2() | ||
| expect(xml).not.toContain('xmlns:dc=') | ||
|
|
||
| const result = addDcCreators(xml, feed.items) | ||
| expect(result).toContain('xmlns:dc="http://purl.org/dc/elements/1.1/"') | ||
| expect(result).toContain('<dc:creator><![CDATA[John Doe]]></dc:creator>') | ||
| }) | ||
|
|
||
| test('produces valid XML structure', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Test Article', | ||
| link: 'https://example.com/article', | ||
| date: new Date('2024-01-01'), | ||
| content: 'Test content', | ||
| author: [{ name: 'John Doe' }], | ||
| }, | ||
| ] | ||
| const { xml, items: feedItems } = buildFeedXml(items) | ||
| const result = addDcCreators(xml, feedItems) | ||
|
|
||
| // dc:creator should be inside <item> block | ||
| const itemStart = result.indexOf('<item>') | ||
| const itemEnd = result.indexOf('</item>') | ||
| const dcCreatorIdx = result.indexOf('<dc:creator>') | ||
| expect(dcCreatorIdx).toBeGreaterThan(itemStart) | ||
| expect(dcCreatorIdx).toBeLessThan(itemEnd) | ||
| }) | ||
|
|
||
| test('escapes ]]> in author names to prevent CDATA injection', () => { | ||
| const items: Item[] = [ | ||
| { | ||
| title: 'Test Article', | ||
| link: 'https://example.com/article', | ||
| date: new Date('2024-01-01'), | ||
| content: 'Test content', | ||
| author: [{ name: 'Evil]]>Author' }], | ||
| }, | ||
| ] | ||
| const { xml, items: feedItems } = buildFeedXml(items) | ||
| const result = addDcCreators(xml, feedItems) | ||
|
|
||
| // The ]]> sequence should be escaped by splitting the CDATA section | ||
| expect(result).not.toContain('<dc:creator><![CDATA[Evil]]>Author]]></dc:creator>') | ||
| expect(result).toContain('<dc:creator><![CDATA[Evil]]]]><![CDATA[>Author]]></dc:creator>') | ||
| }) | ||
| }) |
This file contains hidden or 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,40 @@ | ||
| import type { Item, Author } from 'feed/lib/typings' | ||
|
|
||
| export function parseCreator(creator: string): Author { | ||
| const match = creator.match(/^(\S+@\S+)\s+\((.+)\)$/) | ||
| if (match) { | ||
| return { email: match[1], name: match[2] } | ||
| } | ||
| return { name: creator } | ||
| } | ||
|
|
||
| export function addDcCreators(rssXml: string, items: Item[]): string { | ||
| let result = rssXml | ||
| let searchFrom = 0 | ||
|
|
||
| for (const item of items) { | ||
| const closeIdx = result.indexOf('</item>', searchFrom) | ||
| if (closeIdx === -1) break | ||
|
|
||
| if (item.author?.length && item.author[0].name) { | ||
| const name = item.author[0].name.replace(/]]>/g, ']]]]><![CDATA[>') | ||
| const newlinePos = result.lastIndexOf('\n', closeIdx) | ||
| const itemIndent = result.substring(newlinePos + 1, closeIdx) | ||
| const childIndent = itemIndent + ' ' | ||
| const dcLine = '\n' + childIndent + `<dc:creator><![CDATA[${name}]]></dc:creator>` | ||
| result = result.slice(0, newlinePos) + dcLine + result.slice(newlinePos) | ||
| searchFrom = closeIdx + dcLine.length + '</item>'.length | ||
| } else { | ||
| searchFrom = closeIdx + '</item>'.length | ||
| } | ||
| } | ||
|
|
||
| if (!result.includes('xmlns:dc=')) { | ||
| result = result.replace( | ||
| 'version="2.0"', | ||
| 'version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"' | ||
| ) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this implementation feels too hack. if our current library don't support this, could you explore and find a library with good supporting on author & creator fields>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced the
feedlibrary withfeedsmithwhich has native support for Dublin Core namespace elements includingdc:creator. The hacky XML post-processing insrc/author.tsis now completely removed — feedsmith handlesdc:creatornatively viadc: { creator: '...' }on items, and automatically manages namespace declarations. See commit4ca6317.