Skip to content

Commit

Permalink
feat(site): Choose site from a list for ease
Browse files Browse the repository at this point in the history
Users don't need to write a full URL for enumerated sites.
  • Loading branch information
5ouma committed Jun 14, 2024
1 parent 3ed4218 commit 970b89d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
24 changes: 18 additions & 6 deletions .github/assets/example/feeds.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@ name = "list 1"

[[lists.feeds]]
title = "feed 1"
xmlUrl = "https://feed1.com/rss"
type = "rss"
rssUrl = "https://feed1.com/rss"
[[lists.feeds]]
title = "feed 2"
xmlUrl = "https://feed2.com/rss"
type = "rss"
rssUrl = "https://feed2.com/rss"


[[lists]]
name = "list 2"

[[lists.feeds]]
title = "feed 1"
xmlUrl = "https://feed1.com/rss"
title = "bluesky"
type = "bluesky"
id = "username"
[[lists.feeds]]
title = "feed 2"
xmlUrl = "https://feed2.com/rss"
title = "note"
type = "note"
id = "username"
[[lists.feeds]]
title = "reddit"
type = "reddit"
id = "subreddit-name"
[[lists.feeds]]
title = "sizu.me"
type = "sizu.me"
id = "username"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
feeds.toml
outputs/
/coverage
/coverage.lcov
6 changes: 4 additions & 2 deletions src/libs/convert.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { stringify } from "@libs/xml";
import { parse } from "@std/toml";
import { transcodeXmlUrl } from "./sites.ts";
import type { Feed, List, Lists, OPMLOutline } from "../types/mod.ts";

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 = new URL(feed.xmlUrl);
feed.rssUrl = new URL(feed.rssUrl as URL);
feed.xmlUrl = transcodeXmlUrl(feed.type, feed.rssUrl, feed.id);
});
});
return lists;
Expand All @@ -18,7 +20,7 @@ export function convertToOPML(list: List): string {
return {
"@title": feed.title,
"@text": feed.title,
"@xmlUrl": feed.xmlUrl,
"@xmlUrl": transcodeXmlUrl(feed.type, feed.rssUrl, feed.id),
"@type": "rss",
};
}),
Expand Down
17 changes: 17 additions & 0 deletions src/libs/sites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { site } from "../types/mod.ts";

const sites: site[] = [
{ type: "bluesky", url: new URL("https://bsky.app/profile/{id}/rss") },
{ type: "note", url: new URL("https://note.com/{id}/rss") },
{ type: "reddit", url: new URL("https://www.reddit.com/r/{id}/.rss") },
{ type: "sizu.me", url: new URL("https://sizu.me/{id}/rss") },
];

export function transcodeXmlUrl(type: string, rssUrl?: URL, id?: string): URL {
if (type === "rss") {
return new URL(rssUrl as URL);
} else {
const url: URL = sites.find((site: site) => site.type === type)?.url as URL;
return new URL(url.href.replace(encodeURI("{id}"), id as string));
}
}
1 change: 1 addition & 0 deletions src/types/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./opml.ts";
export * from "./toml.ts";
export * from "./sites.ts";
4 changes: 4 additions & 0 deletions src/types/sites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type site = {
type: string;
url: URL;
};
5 changes: 4 additions & 1 deletion src/types/toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ export type List = {
};
export type Feed = {
title: string;
xmlUrl: URL;
type: string;
id?: string;
rssUrl?: URL;
xmlUrl?: URL;
};

0 comments on commit 970b89d

Please sign in to comment.