Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/giscus.ts
3557 views
1
/*
2
* giscus.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
8
export interface GithubDiscussionMetadata {
9
repositoryId: string;
10
categories: Array<{
11
emoji: string;
12
id: string;
13
name: string;
14
}>;
15
}
16
17
export function getDiscussionCategoryId(
18
categoryName: string,
19
metadata: GithubDiscussionMetadata,
20
) {
21
// Fetch category info
22
if (metadata.categories) {
23
for (const category of metadata.categories) {
24
if (category.name === categoryName) {
25
return category.id;
26
}
27
}
28
}
29
return undefined;
30
}
31
32
export async function getGithubDiscussionsMetadata(repo: string) {
33
const url = encodeURI(
34
`https://giscus.app/api/discussions/categories?repo=${repo}`,
35
);
36
37
// Fetch repo info
38
const response = await fetch(url);
39
const jsonObj = await response.json();
40
return jsonObj as GithubDiscussionMetadata;
41
}
42
43
export type GiscusThemeToggleRecord = {
44
baseTheme: string;
45
altTheme: string;
46
}
47
48
export type GiscusTheme = {
49
light?: string;
50
dark?: string;
51
} | string;
52
53
enum GiscusThemeDefault {
54
light = "light",
55
dark = "dark",
56
}
57
58
export const buildGiscusThemeKeys = (
59
darkModeDefault: boolean,
60
theme: GiscusTheme
61
): GiscusThemeToggleRecord => {
62
if (typeof theme === "string") {
63
if (theme.length > 0) {
64
return { baseTheme: theme, altTheme: theme };
65
} else {
66
theme = { light: GiscusThemeDefault.light, dark: GiscusThemeDefault.dark };
67
}
68
}
69
70
const themeRecord: { light: string; dark: string } = theme as {
71
light: string;
72
dark: string;
73
};
74
const result = {
75
baseTheme: themeRecord.light ?? GiscusThemeDefault.light,
76
altTheme: themeRecord.dark ?? GiscusThemeDefault.dark,
77
};
78
79
if (darkModeDefault) {
80
[result.baseTheme, result.altTheme] = [result.altTheme, result.baseTheme];
81
}
82
83
return result;
84
};
85