]> Untitled Git - lemmy.git/blob - ui/src/utils.ts
Adding emoji support
[lemmy.git] / ui / src / utils.ts
1 import { UserOperation, Comment, User, SortType, ListingType } from './interfaces';
2 import * as markdown_it from 'markdown-it';
3 declare var markdownitEmoji: any;
4 import * as markdown_it_container from 'markdown-it-container';
5
6 export let repoUrl = 'https://github.com/dessalines/lemmy';
7
8 export function msgOp(msg: any): UserOperation {
9   let opStr: string = msg.op;
10   return UserOperation[opStr];
11 }
12
13 var md = new markdown_it({
14   html: true,
15   linkify: true,
16   typographer: true
17 }).use(markdown_it_container, 'spoiler', {
18   validate: function(params: any) {
19     return params.trim().match(/^spoiler\s+(.*)$/);
20   },
21
22   render: function (tokens: any, idx: any) {
23     var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
24
25     if (tokens[idx].nesting === 1) {
26       // opening tag
27       return '<details><summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n';
28
29     } else {
30       // closing tag
31       return '</details>\n';
32     }
33   }
34 }).use(markdownitEmoji);
35
36 export function hotRank(comment: Comment): number {
37   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
38
39   let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
40   let now: Date = new Date();
41   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
42
43   let rank = (10000 *  Math.log10(Math.max(1, 3 + comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
44
45   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
46
47   return rank;
48 }
49
50 export function mdToHtml(text: string) {
51   return {__html: md.render(text)};
52 }
53
54 export function getUnixTime(text: string): number { 
55   return text ? new Date(text).getTime()/1000 : undefined;
56 }
57
58 export function addTypeInfo<T>(arr: Array<T>, name: string): Array<{type_: string, data: T}> {  
59   return arr.map(e => {return {type_: name, data: e}});
60 }
61
62 export function canMod(user: User, modIds: Array<number>, creator_id: number): boolean {
63   // You can do moderator actions only on the mods added after you.
64   if (user) {
65     let yourIndex = modIds.findIndex(id => id == user.id);
66     if (yourIndex == -1) {
67       return false;
68     } else { 
69       modIds = modIds.slice(0, yourIndex+1); // +1 cause you cant mod yourself
70       return !modIds.includes(creator_id);
71     }
72   } else {
73     return false;
74   }
75 }
76
77 export function isMod(modIds: Array<number>, creator_id: number): boolean {
78   return modIds.includes(creator_id);
79 }
80
81
82 var imageRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`);
83
84 export function isImage(url: string) {
85   return imageRegex.test(url);
86 }
87
88 export let fetchLimit: number = 20;
89
90 export function capitalizeFirstLetter(str: string): string {
91   return str.charAt(0).toUpperCase() + str.slice(1);
92 }
93
94
95 export function routeSortTypeToEnum(sort: string): SortType {
96   if (sort == 'new') {
97     return SortType.New;
98   } else if (sort == 'hot') {
99     return SortType.Hot;
100   } else if (sort == 'topday') {
101     return SortType.TopDay;
102   } else if (sort == 'topweek') {
103     return SortType.TopWeek;
104   } else if (sort == 'topmonth') {
105     return SortType.TopMonth;
106   } else if (sort == 'topall') {
107     return SortType.TopAll;
108   }
109 }
110
111 export function routeListingTypeToEnum(type: string): ListingType {
112   return ListingType[capitalizeFirstLetter(type)];
113 }
114
115 export async function getPageTitle(url: string) {
116   let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
117   let data = await res.text();
118   return data;
119 }
120