Skip to content

Commit 439b3cf

Browse files
authored
Add shortlink support (#114)
* Adds getAsShortUrl util function * Adds shortUrl property to interfaces with url property * Updates return for related shortUrl objects * Adds export for getAsShortUrl to the main index file
1 parent 1605bc6 commit 439b3cf

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export * from "./users";
22
export * from "./tags";
33
export * from "./works";
44
export * from "./series";
5-
5+
export { getAsShortUrl } from './urls';
66
export { setFetcher } from "./fetcher";

src/series/getters.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
getWorkTotalChapters,
1111
getWorkWordCount,
1212
} from "src/works/work-getters";
13-
import { getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
13+
import { getAsShortUrl, getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
1414

1515
const monthMap: { [month: string]: string } = {
1616
Jan: "01",
@@ -141,10 +141,13 @@ const getSeriesWork = (workHtml: string): SeriesWorkSummary => {
141141

142142
const url = $work("a[href*='/works/']").attr("href") as string;
143143
const id = getWorkDetailsFromUrl({ url }).workId;
144+
const workUrl = getWorkUrl({ workId: id });
145+
const shortUrl = getAsShortUrl({ url: workUrl });
144146

145147
return {
146148
id,
147-
url: getWorkUrl({ workId: id }),
149+
url: workUrl,
150+
shortUrl,
148151
title: getSeriesWorkTitle($work),
149152
updatedAt: getSeriesWorkUpdateDate($work),
150153

src/urls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export const getWorkUrl = ({
2222
return workUrl;
2323
};
2424

25+
export const getAsShortUrl = ({ url }: { url: string }) => url.replace(/archiveofourown/, 'ao3');
26+
2527
export const getUserProfileUrl = ({ username }: { username: string }) =>
2628
`https://archiveofourown.org/users/${encodeURI(username)}/profile`;
2729

src/users/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from "./getters";
1717

1818
import { User } from "types/entities";
19-
import { getUserProfileUrl } from "../urls";
19+
import { getAsShortUrl, getUserProfileUrl } from "../urls";
2020
import { loadUserProfilePage } from "../page-loaders";
2121

2222
export const getUser = async ({
@@ -25,7 +25,8 @@ export const getUser = async ({
2525
username: string;
2626
}): Promise<User> => {
2727
const profilePage = await loadUserProfilePage({ username });
28-
28+
const url = getUserProfileUrl({ username });
29+
const shortUrl = getAsShortUrl({ url });
2930
return {
3031
// We use this because capitalization might be different
3132
username: getUserProfileName(profilePage),
@@ -37,7 +38,8 @@ export const getUser = async ({
3738
header: getUserProfileHeader(profilePage),
3839
location: getUserProfileLocation(profilePage),
3940
birthday: getUserProfileBirthday(profilePage),
40-
url: getUserProfileUrl({ username }),
41+
url,
42+
shortUrl,
4143
works: getUserProfileWorks(profilePage),
4244
series: getUserProfileSeries(profilePage),
4345
bookmarks: getUserProfileBookmarks(profilePage),

src/works/chapter-getters.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Author, Chapter } from "types/entities";
2-
import { getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
2+
import { getAsShortUrl, getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
33

44
import { ChapterIndexPage } from "src/page-loaders";
55

@@ -18,7 +18,8 @@ export const getChaptersList = ($chapterIndexPage: ChapterIndexPage) => {
1818
const dateNode = $chapterIndexPage(
1919
$chapterIndexPage(li).find(".datetime")[0]
2020
);
21-
21+
const url = getWorkUrl({ workId, chapterId });
22+
const shortUrl = getAsShortUrl({ url });
2223
chapters.push({
2324
id: chapterId!,
2425
workId,
@@ -27,7 +28,8 @@ export const getChaptersList = ($chapterIndexPage: ChapterIndexPage) => {
2728
// Remove parenthesis from the date
2829
publishedAt: dateNode.text().replace(/[\(\)]/g, ""),
2930
// We rebuild the url so it gets the full path
30-
url: getWorkUrl({ workId, chapterId }),
31+
url,
32+
shortUrl
3133
});
3234
});
3335
return chapters;

types/entities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface User {
3333
username: string;
3434
pseuds: string;
3535
url: string;
36+
shortUrl: string;
3637
icon: string;
3738
header: string | null;
3839
joined: string;
@@ -59,6 +60,7 @@ export interface SeriesWorkSummary
5960
| "series"
6061
> {
6162
url: string;
63+
shortUrl: string;
6264
tags: Omit<WorkSummary["tags"], "warnings">;
6365
stats: Omit<WorkSummary["stats"], "comments">;
6466
}
@@ -182,4 +184,5 @@ export interface Chapter {
182184
title: string;
183185
publishedAt: string;
184186
url: string;
187+
shortUrl: string;
185188
}

0 commit comments

Comments
 (0)