Skip to content

Commit c55cfce

Browse files
authored
Add support for enclosures in Atom (#159)
1 parent ce2d389 commit c55cfce

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/__tests__/__snapshots__/atom1.spec.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ exports[`atom 1.0 should generate a valid feed 1`] = `
2929
<title type=\\"html\\"><![CDATA[Hello World]]></title>
3030
<id>https://example.com/hello-world?id=this&amp;that=true</id>
3131
<link href=\\"https://example.com/hello-world?link=sanitized&amp;value=2\\"/>
32+
<link rel=\\"enclosure\\" href=\\"https://example.com/hello-world.jpg\\" type=\\"image/jpg\\" length=\\"12665\\"/>
33+
<link rel=\\"enclosure\\" href=\\"https://example.com/hello-world.jpg\\" type=\\"image/jpg\\"/>
3234
<updated>2013-07-13T23:00:00.000Z</updated>
3335
<summary type=\\"html\\"><![CDATA[This is an article about Hello World.]]></summary>
3436
<content type=\\"html\\"><![CDATA[Content of my item]]></content>

src/atom1.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as convert from "xml-js";
22
import { generator } from "./config";
33
import { Feed } from "./feed";
4-
import { Author, Category, Item } from "./typings";
4+
import { Author, Category, Enclosure, Item } from "./typings";
55
import { sanitize } from "./utils";
66

77
/**
@@ -139,6 +139,26 @@ export default (ins: Feed) => {
139139
});
140140
}
141141

142+
/**
143+
* Item Enclosure
144+
* https://validator.w3.org/feed/docs/atom.html#link
145+
*/
146+
if (item.enclosure) {
147+
entry.link.push(formatEnclosure(item.enclosure));
148+
}
149+
150+
if (item.image) {
151+
entry.link.push(formatEnclosure(item.image, "image"));
152+
}
153+
154+
if (item.audio) {
155+
entry.link.push(formatEnclosure(item.audio, "audio"));
156+
}
157+
158+
if (item.video) {
159+
entry.link.push(formatEnclosure(item.video, "video"));
160+
}
161+
142162
// contributor
143163
if (item.contributor && Array.isArray(item.contributor)) {
144164
entry.contributor = [];
@@ -185,6 +205,21 @@ const formatAuthor = (author: Author) => {
185205
return out;
186206
};
187207

208+
/**
209+
* Returns a formated enclosure
210+
* @param enclosure
211+
* @param mimeCategory
212+
*/
213+
const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => {
214+
if (typeof enclosure === "string") {
215+
const type = new URL(enclosure).pathname.split(".").slice(-1)[0];
216+
return { _attributes: { rel: "enclosure", href: enclosure, type: `${mimeCategory}/${type}` } };
217+
}
218+
219+
const type = new URL(enclosure.url).pathname.split(".").slice(-1)[0];
220+
return { _attributes: { rel: "enclosure", href: enclosure.url, title: enclosure.title, type: `${mimeCategory}/${type}`, length: enclosure.length } };
221+
};
222+
188223
/**
189224
* Returns a formatted category
190225
* @param category

0 commit comments

Comments
 (0)