-
Notifications
You must be signed in to change notification settings - Fork 4
/
editorAdapter.ts
154 lines (135 loc) · 4.35 KB
/
editorAdapter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { BookData, LinkData } from "./interfaces";
import { OPDSEntry, OPDSLink } from "opds-feed-parser";
import {
PER_LIBRARY_SUPPRESS_REL,
PER_LIBRARY_UNSUPPRESS_REL,
} from "./features/book/bookEditorSlice";
/** Convert an OPDS link to a LinkData object. */
const opdsLinkToLinkData = (link: OPDSLink | undefined): LinkData => {
if (!link) {
return link;
}
const {
href,
rel,
role = undefined,
title = undefined,
type = undefined,
} = link;
return { href, rel, title, type, role };
};
/** Extract metadata and links from an OPDS entry for use on the
book details page. */
export default function adapter(data: OPDSEntry): BookData {
const hideLink = data.links.find((link) => {
return link.rel === "http://librarysimplified.org/terms/rel/hide";
});
const restoreLink = data.links.find((link) => {
return link.rel === "http://librarysimplified.org/terms/rel/restore";
});
const suppressPerLibraryLink = data.links.find((link) => {
return link.rel === PER_LIBRARY_SUPPRESS_REL;
});
const unsuppressPerLibraryLink = data.links.find((link) => {
return link.rel === PER_LIBRARY_UNSUPPRESS_REL;
});
const refreshLink = data.links.find((link) => {
return link.rel === "http://librarysimplified.org/terms/rel/refresh";
});
const editLink = data.links.find((link) => {
return link.rel === "edit";
});
const issuesLink = data.links.find((link) => {
return link.rel === "issues";
});
const changeCoverLink = data.links.find((link) => {
return link.rel === "http://librarysimplified.org/terms/rel/change_cover";
});
const audience = data.categories.find((category) => {
return category.scheme === "http://schema.org/audience";
});
const targetAgeCategory = data.categories.find((category) => {
return category.scheme === "http://schema.org/typicalAgeRange";
});
let targetAgeRange = [];
if (targetAgeCategory && /\d\d?-?\d?\d?/.test(targetAgeCategory.term)) {
targetAgeRange = targetAgeCategory.term.split("-");
}
const fictionCategory = data.categories.find((category) => {
return category.scheme === "http://librarysimplified.org/terms/fiction/";
});
let fiction;
if (fictionCategory) {
fiction = fictionCategory.label === "Fiction";
}
const categories = data.categories.map((category) => category.label);
let medium;
try {
medium = data.unparsed["$"]["schema:additionalType"]["value"];
} catch (e) {
medium = null;
}
let imprint;
try {
imprint = data.unparsed["bib:publisherImprint"][0]["_"];
} catch (e) {
imprint = null;
}
const authors = [];
for (const author of data.authors) {
authors.push(Object.assign({}, author, { role: "aut" }));
}
const contributors = data.contributors?.map((contributor) => ({
name: contributor?.name,
role: contributor?.role,
uri: contributor?.uri,
}));
let rating;
try {
const ratings = data.unparsed["schema:Rating"];
for (const ratingTag of ratings) {
if (
ratingTag["$"]["schema:additionalType"]["value"] ===
"http://schema.org/ratingValue"
) {
rating = ratingTag["$"]["schema:ratingValue"]["value"];
break;
}
}
} catch (e) {
rating = null;
}
const imageLink = data.links.find((link) => {
return link.rel === "http://opds-spec.org/image";
});
const coverUrl: string | undefined = imageLink?.href;
return {
id: data.id,
title: data.title,
authors: authors,
contributors: contributors,
subtitle: data.subtitle,
summary: data.summary.content,
audience: audience && audience.term,
targetAgeRange: targetAgeRange,
fiction: fiction,
categories: categories,
hideLink: opdsLinkToLinkData(hideLink),
restoreLink: opdsLinkToLinkData(restoreLink),
refreshLink: opdsLinkToLinkData(refreshLink),
suppressPerLibraryLink: opdsLinkToLinkData(suppressPerLibraryLink),
unsuppressPerLibraryLink: opdsLinkToLinkData(unsuppressPerLibraryLink),
editLink: opdsLinkToLinkData(editLink),
issuesLink: opdsLinkToLinkData(issuesLink),
changeCoverLink: opdsLinkToLinkData(changeCoverLink),
series: data.series && data.series.name,
seriesPosition: data.series && data.series.position,
medium: medium,
language: data.language,
publisher: data.publisher,
imprint: imprint,
issued: data.issued,
rating: rating,
coverUrl: coverUrl,
};
}