-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.ts
40 lines (37 loc) 路 1 KB
/
convert.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
import { stringify } from "@libs/xml";
import { parse } from "@std/toml";
import { transcodeXmlUrl } from "./sites.ts";
import type {
Feed,
List,
Lists,
OPMLOutline,
} from "@5ouma/opml-generator/types";
export function convertFromTOML(data: string): Lists {
const lists: Lists = parse(data) as Lists;
lists.lists.map((list: List) => {
list.feeds.map((feed: Feed) => {
feed.xmlUrl = feed.xmlUrl
? new URL(feed.xmlUrl)
: transcodeXmlUrl(feed.title, feed.type, feed.id);
});
});
return lists;
}
export function convertToOPML(list: List): string {
const body = {
outline: list.feeds.map((feed: Feed): OPMLOutline => {
return {
"@title": feed.title,
"@text": feed.title,
"@xmlUrl": feed.xmlUrl
? new URL(feed.xmlUrl)
: transcodeXmlUrl(feed.title, feed.type, feed.id),
"@type": "rss",
};
}),
};
return `<?xml version="1.0" encoding="UTF-8"?>
${stringify({ opml: { "@version": "2.0", body: [body] } })}
`;
}