Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 50 additions & 45 deletions src/project/types/website/website-giscus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,59 @@ export async function resolveFormatForGiscus(
project: ProjectContext,
format: Format,
) {
const comments = format.metadata[kComments] as Record<string, unknown>;
if (comments) {
const giscusOptions = comments[kGiscus] as Record<string, unknown>;
if (giscusOptions) {
// Giscus is configured, so we need to check whether the
// the repo-id and category-id are set. If they are not,
// we need to fetch them and populate them into the format
if (
giscusOptions[kGiscusRepoId] === undefined ||
giscusOptions[kGiscusCategoryId] === undefined
) {
const repo = giscusOptions.repo as string;
const fileName = `${repo}.json`;
const comments = format.metadata[kComments] as
| Record<string, unknown>
| false
| undefined;
if (!comments) return;

let giscusMeta: GithubDiscussionMetadata;
// The scratch directory used to cache metadata
const giscusPath = projectScratchPath(project.dir, kGiscusDir);
ensureDirSync(giscusPath);
const giscusOptions = comments[kGiscus] as
| Record<string, unknown>
| undefined;
if (!giscusOptions) return;

// The path to the metadata file for this repo
const path = join(
giscusPath,
fileName.replace("/", "."),
);
// Giscus is configured, so we need to check whether the
// the repo-id and category-id are set. If they are not,
// we need to fetch them and populate them into the format
if (
giscusOptions[kGiscusRepoId] !== undefined &&
giscusOptions[kGiscusCategoryId] !== undefined
) return;

try {
// Try to read the cached data and use that
const giscusJson = Deno.readTextFileSync(path);
giscusMeta = JSON.parse(giscusJson);
} catch {
// Couldn't read the cached metadata, fetch it
removeIfExists(path);
giscusMeta = await getGithubDiscussionsMetadata(repo);
const jsonStr = JSON.stringify(giscusMeta, undefined, 2);
Deno.writeTextFileSync(path, jsonStr);
}
const repo = giscusOptions.repo as string;
const fileName = `${repo}.json`;

// Populate the ids if need be
if (giscusOptions[kGiscusRepoId] === undefined) {
giscusOptions[kGiscusRepoId] = giscusMeta.repositoryId;
}
if (giscusOptions[kGiscusCategoryId] === undefined) {
giscusOptions[kGiscusCategoryId] = getDiscussionCategoryId(
(giscusOptions[kGiscusCategoryId] || "") as string,
giscusMeta,
);
}
}
}
let giscusMeta: GithubDiscussionMetadata;
// The scratch directory used to cache metadata
const giscusPath = projectScratchPath(project.dir, kGiscusDir);
ensureDirSync(giscusPath);

// The path to the metadata file for this repo
const path = join(
giscusPath,
fileName.replace("/", "."),
);

try {
// Try to read the cached data and use that
const giscusJson = Deno.readTextFileSync(path);
giscusMeta = JSON.parse(giscusJson);
} catch {
// Couldn't read the cached metadata, fetch it
removeIfExists(path);
giscusMeta = await getGithubDiscussionsMetadata(repo);
const jsonStr = JSON.stringify(giscusMeta, undefined, 2);
Deno.writeTextFileSync(path, jsonStr);
}

// Populate the ids if need be
if (giscusOptions[kGiscusRepoId] === undefined) {
giscusOptions[kGiscusRepoId] = giscusMeta.repositoryId;
}
if (giscusOptions[kGiscusCategoryId] === undefined) {
giscusOptions[kGiscusCategoryId] = getDiscussionCategoryId(
(giscusOptions[kGiscusCategoryId] || "") as string,
giscusMeta,
);
}
}
Loading
Loading