-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
82 lines (78 loc) · 2.34 KB
/
utils.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import moment from 'moment'
import { highlightjs } from 'types'
export function serilization<T extends Record<string, any>>(obj: T): T {
let newObj: any = {}
Object.keys(obj).forEach((key) => {
let value = obj[key]
if (value !== null) {
// If array, loop...
if (Array.isArray(value)) {
value = value.map((item) => serilization(item))
}
// ...if property is date/time, stringify/parse...
else if (
typeof value === 'object' &&
typeof value.getMonth === 'function'
) {
const date = moment(value)
value = date.format('YYYY-MM-DDTHH:mm:ssZ')
}
// ...and if a deep object, loop.
else if (typeof value === 'object') {
value = serilization(value)
}
}
newObj[key] = value
})
return newObj
}
export const isServer = () => typeof window === 'undefined'
export const getPostHeadingImage = (id: number) => {
const index = ((id - 1) % 11) + 1
return `/static/post-heading-${index}.jpg`
}
export function hljsDefineGraphQL(fn: typeof highlightjs) {
return {
aliases: ['gql'],
keywords: {
keyword:
'query mutation subscription|10 type interface union scalar fragment|10 enum on ...',
literal: 'true false null',
},
contains: [
fn.HASH_COMMENT_MODE,
fn.QUOTE_STRING_MODE,
fn.NUMBER_MODE,
{
className: 'type',
begin: '[^\\w][A-Z][a-z]',
end: '\\W',
excludeEnd: true,
},
{
className: 'literal',
begin: '[^\\w][A-Z][A-Z]',
end: '\\W',
excludeEnd: true,
},
{
className: 'variable',
begin: '\\$',
end: '\\W',
excludeEnd: true,
},
{
className: 'keyword',
begin: '[.]{2}',
end: '\\.',
},
{
className: 'meta',
begin: '@',
end: '\\W',
excludeEnd: true,
},
],
illegal: /([;<']|BEGIN)/,
}
}