Skip to content

Commit

Permalink
FeedEntry.id fallback (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelPorttila authored Feb 13, 2025
1 parent 6a801d9 commit e2b9738
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const mediaContent = entries[0][MediaRss.Content];

| Feed | Atom | RSS2 | RSS |
| --------------------- | --------------------------------------------- | -------------------------------------- | ----------------------------- |
| Id | Id | Guid | DC:URI or Link |
| Id | Id | Guid or Link or DC:URI | DC:URI or Link |
| Author | Author | Author or DC:Creator | DC:Creator |
| Title | Title | Title or DC:Title | Title or DC:Title |
| Description | Summary | Description or DC:Description | Description or DC:Description |
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@mikaelporttila/rss",
"version": "1.1.1",
"version": "1.1.2",
"exports": "./mod.ts"
}
10 changes: 8 additions & 2 deletions src/mappers/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ function mapRss2ToFeed(rss: InternalRSS2): Feed {
copyValueFields(DublinCoreFieldArray, entry, entry);
copyMedia(entry as InternalMediaRss, entry);

entry.id = guid?.value as string || entry[DublinCoreFields.URI] as string;

const titleValue = entry[DublinCoreFields.Title] || title?.value;
if (titleValue) {
entry.title = {
Expand Down Expand Up @@ -310,6 +308,14 @@ function mapRss2ToFeed(rss: InternalRSS2): Feed {
entry.links.push({ href: link?.value });
}

entry.id = guid?.value as string || entry[DublinCoreFields.URI] as string;
if (!entry.id && entry.links.length > 0) {
const linkWithHref = entry.links.find(x => x.href);
if (linkWithHref) {
entry.id = linkWithHref.href;
}
}

if (enclosure) {
entry.attachments = enclosure.map((x) => ({
url: x.url,
Expand Down
34 changes: 33 additions & 1 deletion src/mappers/mapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ function composeRss2 (
value: "RSS2:Channel:Item:2:content:encoded:Value",
},
},
{
title: {
value: "RSS2:Channel:Item:3:Title:Value",
},
description: {
value: "RSS2:Channel:Item:3:Description:Value",
},
author: {
value: "RSS2:Channel:Item:3:Author:Value",
},
link: {
value: "RSS2:Channel:Item:3:Link:Value",
},
guid: {
value: undefined,
},
comments: {
value: "RSS2:Channel:Item:3:Comments:Value",
},
categories: [
{ value: "RSS2:Channel:Item:3:Categories:0:Value" },
{ value: "RSS2:Channel:Item:3:Categories:1:Value" },
]
},
],
},
} as InternalRSS2;
Expand Down Expand Up @@ -990,7 +1014,7 @@ function testArrayLength (
getValue: (src: Feed) => src.image?.width,
assert: [{ fn: assertEquals, expect: 34 }],
},
...testArrayLength("Items", (src: Feed) => src.entries, 3),
...testArrayLength("Items", (src: Feed) => src.entries, 4),
...testTextField(
"Items:0:Title",
(src: Feed) => src.entries[0].title,
Expand Down Expand Up @@ -1111,6 +1135,14 @@ function testArrayLength (
expect: "RSS2:Channel:Item:2:FeedburnerLink:Value",
}],
},
{
name: "Items:3:Id:Fallback",
getValue: (src: Feed) => src.entries[3].id,
assert: [{
fn: assertEquals,
expect: "RSS2:Channel:Item:3:Link:Value",
}],
},
{
name: "Items:2:ContentEncoded",
getValue: (src: Feed) => src.entries[2].content?.value,
Expand Down
2 changes: 1 addition & 1 deletion src/mappers/media_mapper_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* import { assertEquals, assertNotEquals } from "./../../test_deps.ts";
import { copyMedia } from "./media-mapper.ts"; */

// TODO(MikaelPorttila): Add tests...
// TODO: Add tests...
4 changes: 2 additions & 2 deletions src/types/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export interface FeedEntry extends DublinCore, Slash, MediaRss {
title?: TextField;
description?: TextField;
links: Link[];
id: string;
// TODO(MikaelPorttila): Replace with Authors to match JSONFeed.
id?: string;
// TODO: Replace with Authors to match JSONFeed.
author?: Author;
published?: Date;
publishedRaw?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/types/internal/internal_rss2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface Channel extends InternalDublinCore {
cloud?: Cloud;
ttl?: ValueField<number>;
image?: Image;
textInput?: any; // TODO(MikaelPorttila): Type textInput
textInput?: any; // TODO: Type textInput
skipHours?: {
hour?: ValueField<number>[];
};
Expand Down

0 comments on commit e2b9738

Please sign in to comment.