generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenn_link.ts
43 lines (37 loc) · 881 Bytes
/
zenn_link.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
import { ZENN_ROOT } from "./zenn_api.ts";
import {
implementsZennArticle,
implementsZennBook,
implementsZennTopic,
implementsZennUser,
ZennArticle,
ZennBook,
ZennScrap,
ZennTopic,
ZennUser,
} from "./types.ts";
/**
* Generate URL to Zenn by given object.
* @param object Zenn object
* @return Zenn URL
*/
export function zennLink(
object: ZennArticle | ZennBook | ZennScrap | ZennUser | ZennTopic,
): string {
if (!object) {
return ZENN_ROOT;
}
if (implementsZennUser(object)) {
return ZENN_ROOT + object.username;
}
if (implementsZennTopic(object)) {
return ZENN_ROOT + object.name;
}
const username = object.user.username;
const resourceName = implementsZennArticle(object)
? "/articles/"
: implementsZennBook(object)
? "/book/"
: "/scrap/";
return ZENN_ROOT + username + resourceName + object.slug;
}