|
1 | 1 | import * as convert from "xml-js"; |
2 | 2 | import { generator } from "./config"; |
3 | 3 | import { Feed } from "./feed"; |
4 | | -import { Author, Category, Item } from "./typings"; |
| 4 | +import { Author, Category, Enclosure, Item } from "./typings"; |
5 | 5 | import { sanitize } from "./utils"; |
6 | 6 |
|
7 | 7 | /** |
@@ -139,6 +139,26 @@ export default (ins: Feed) => { |
139 | 139 | }); |
140 | 140 | } |
141 | 141 |
|
| 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 | + |
142 | 162 | // contributor |
143 | 163 | if (item.contributor && Array.isArray(item.contributor)) { |
144 | 164 | entry.contributor = []; |
@@ -185,6 +205,21 @@ const formatAuthor = (author: Author) => { |
185 | 205 | return out; |
186 | 206 | }; |
187 | 207 |
|
| 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 | + |
188 | 223 | /** |
189 | 224 | * Returns a formatted category |
190 | 225 | * @param category |
|
0 commit comments