-
Notifications
You must be signed in to change notification settings - Fork 25
/
util.ts
128 lines (112 loc) · 3.08 KB
/
util.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Giscus, Session } from './types'
export const GISCUS_SESSION_KEY = 'giscus-session'
export const GISCUS_ORIGIN = 'https://giscus.app'
const ERROR_SUGGESTION = `Please consider reporting this error at https://github.com/laymonage/giscus/issues/new.`
export function formatError(message: string) {
return `[giscus] An error occurred. Error message: "${message}".`
}
export function getOgMetaContent(property: string) {
const element = document.querySelector(
`meta[property='og:${property}'],meta[name='${property}']`
) as HTMLMetaElement
return element ? element.content : ''
}
export function getIframeSrc({
repo,
repoId,
category = '',
categoryId = '',
mapping,
term = '',
theme = 'light',
reactionsEnabled = '1',
emitMetadata = '0',
session
}: Giscus & Session) {
const origin = location.href
const description = getOgMetaContent('description')
const params: Record<string, string> = {
origin,
session,
theme,
reactionsEnabled,
emitMetadata,
repo,
repoId,
category,
categoryId,
description
}
switch (mapping) {
case 'url':
params.term = location.href
break
case 'title':
params.term = document.title
break
case 'og:title':
params.term = getOgMetaContent('title')
break
case 'specific':
params.term = term
break
case 'number':
params.number = term
break
case 'pathname':
default:
params.term =
location.pathname.length < 2
? 'index'
: location.pathname.substr(1).replace(/\.\w+$/, '')
break
}
return `${GISCUS_ORIGIN}/widget?${new URLSearchParams(params)}`
}
export function addDefaultStyles() {
const style =
document.getElementById('giscus-css') || document.createElement('style')
style.id = 'giscus-css'
style.textContent = `
.giscus, .giscus-frame {
width: 100%;
}
.giscus-frame {
border: none;
color-scheme: auto;
}
`
document.head.prepend(style)
}
export function createErrorMessageListener(resetSession: () => void) {
return function (event: MessageEvent) {
if (event.origin !== GISCUS_ORIGIN) return
const { data } = event
if (!(typeof data === 'object' && data?.giscus?.error)) return
const message: string = data.giscus.error
if (
message.includes('Bad credentials') ||
message.includes('Invalid state value')
) {
// Might be because token is expired or other causes
if (localStorage.getItem(GISCUS_SESSION_KEY) !== null) {
localStorage.removeItem(GISCUS_SESSION_KEY)
resetSession()
console.warn(`${formatError(message)} Session has been cleared.`)
return
}
console.error(
`${formatError(
message
)} No session is stored initially. ${ERROR_SUGGESTION}`
)
}
if (message.includes('Discussion not found')) {
console.warn(
`[giscus] ${message}. A new discussion will be created if a comment/reaction is submitted.`
)
return
}
console.error(`${formatError(message)} ${ERROR_SUGGESTION}`)
}
}