Skip to content

Commit 9c371c6

Browse files
authored
Merge pull request #12562 from quarto-dev/refactor/website-giscus
refactor: website-giscus.ts
2 parents a396be4 + 4fa5273 commit 9c371c6

File tree

8 files changed

+860
-631
lines changed

8 files changed

+860
-631
lines changed

src/project/types/website/website-giscus.ts

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,59 @@ export async function resolveFormatForGiscus(
3030
project: ProjectContext,
3131
format: Format,
3232
) {
33-
const comments = format.metadata[kComments] as Record<string, unknown>;
34-
if (comments) {
35-
const giscusOptions = comments[kGiscus] as Record<string, unknown>;
36-
if (giscusOptions) {
37-
// Giscus is configured, so we need to check whether the
38-
// the repo-id and category-id are set. If they are not,
39-
// we need to fetch them and populate them into the format
40-
if (
41-
giscusOptions[kGiscusRepoId] === undefined ||
42-
giscusOptions[kGiscusCategoryId] === undefined
43-
) {
44-
const repo = giscusOptions.repo as string;
45-
const fileName = `${repo}.json`;
33+
const comments = format.metadata[kComments] as
34+
| Record<string, unknown>
35+
| false
36+
| undefined;
37+
if (!comments) return;
4638

47-
let giscusMeta: GithubDiscussionMetadata;
48-
// The scratch directory used to cache metadata
49-
const giscusPath = projectScratchPath(project.dir, kGiscusDir);
50-
ensureDirSync(giscusPath);
39+
const giscusOptions = comments[kGiscus] as
40+
| Record<string, unknown>
41+
| undefined;
42+
if (!giscusOptions) return;
5143

52-
// The path to the metadata file for this repo
53-
const path = join(
54-
giscusPath,
55-
fileName.replace("/", "."),
56-
);
44+
// Giscus is configured, so we need to check whether the
45+
// the repo-id and category-id are set. If they are not,
46+
// we need to fetch them and populate them into the format
47+
if (
48+
giscusOptions[kGiscusRepoId] !== undefined &&
49+
giscusOptions[kGiscusCategoryId] !== undefined
50+
) return;
5751

58-
try {
59-
// Try to read the cached data and use that
60-
const giscusJson = Deno.readTextFileSync(path);
61-
giscusMeta = JSON.parse(giscusJson);
62-
} catch {
63-
// Couldn't read the cached metadata, fetch it
64-
removeIfExists(path);
65-
giscusMeta = await getGithubDiscussionsMetadata(repo);
66-
const jsonStr = JSON.stringify(giscusMeta, undefined, 2);
67-
Deno.writeTextFileSync(path, jsonStr);
68-
}
52+
const repo = giscusOptions.repo as string;
53+
const fileName = `${repo}.json`;
6954

70-
// Populate the ids if need be
71-
if (giscusOptions[kGiscusRepoId] === undefined) {
72-
giscusOptions[kGiscusRepoId] = giscusMeta.repositoryId;
73-
}
74-
if (giscusOptions[kGiscusCategoryId] === undefined) {
75-
giscusOptions[kGiscusCategoryId] = getDiscussionCategoryId(
76-
(giscusOptions[kGiscusCategoryId] || "") as string,
77-
giscusMeta,
78-
);
79-
}
80-
}
81-
}
55+
let giscusMeta: GithubDiscussionMetadata;
56+
// The scratch directory used to cache metadata
57+
const giscusPath = projectScratchPath(project.dir, kGiscusDir);
58+
ensureDirSync(giscusPath);
59+
60+
// The path to the metadata file for this repo
61+
const path = join(
62+
giscusPath,
63+
fileName.replace("/", "."),
64+
);
65+
66+
try {
67+
// Try to read the cached data and use that
68+
const giscusJson = Deno.readTextFileSync(path);
69+
giscusMeta = JSON.parse(giscusJson);
70+
} catch {
71+
// Couldn't read the cached metadata, fetch it
72+
removeIfExists(path);
73+
giscusMeta = await getGithubDiscussionsMetadata(repo);
74+
const jsonStr = JSON.stringify(giscusMeta, undefined, 2);
75+
Deno.writeTextFileSync(path, jsonStr);
76+
}
77+
78+
// Populate the ids if need be
79+
if (giscusOptions[kGiscusRepoId] === undefined) {
80+
giscusOptions[kGiscusRepoId] = giscusMeta.repositoryId;
81+
}
82+
if (giscusOptions[kGiscusCategoryId] === undefined) {
83+
giscusOptions[kGiscusCategoryId] = getDiscussionCategoryId(
84+
(giscusOptions[kGiscusCategoryId] || "") as string,
85+
giscusMeta,
86+
);
8287
}
8388
}

0 commit comments

Comments
 (0)