-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
json.ts
95 lines (80 loc) 路 2.09 KB
/
json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Extension, Item, Author } from "./typings";
import { Feed } from "./feed";
export default (ins: Feed) => {
const { options, items, extensions } = ins;
let feed: any = {
version: "https://jsonfeed.org/version/1",
title: options.title
};
if (options.link) {
feed.home_page_url = options.link;
}
if (options.feedLinks && options.feedLinks.json) {
feed.feed_url = options.feedLinks.json;
}
if (options.description) {
feed.description = options.description;
}
if (options.image) {
feed.icon = options.image;
}
if (options.author) {
feed.author = {};
if (options.author.name) {
feed.author.name = options.author.name;
}
if (options.author.link) {
feed.author.url = options.author.link;
}
}
extensions.map((e: Extension) => {
feed[e.name] = e.objects;
});
feed.items = items.map((item: Item) => {
let feedItem: any = {
id: item.id,
// json_feed distinguishes between html and text content
// but since we only take a single type, we'll assume HTML
content_html: item.content
};
if (item.link) {
feedItem.url = item.link;
}
if (item.title) {
feedItem.title = item.title;
}
if (item.description) {
feedItem.summary = item.description;
}
if (item.image) {
feedItem.image = item.image;
}
if (item.date) {
feedItem.date_modified = item.date.toISOString();
}
if (item.published) {
feedItem.date_published = item.published.toISOString();
}
if (item.author) {
let author: Author | Author[] = item.author;
if (author instanceof Array) {
// json feed only supports 1 author per post
author = author[0];
}
feedItem.author = {};
if (author.name) {
feedItem.author.name = author.name;
}
if (author.link) {
feedItem.author.url = author.link;
}
}
if (item.extensions) {
item.extensions.map((e: Extension) => {
feedItem[e.name] = e.objects;
});
}
return feedItem;
});
return JSON.stringify(feed, null, 4);
};