Skip to content

Commit 823dde4

Browse files
committed
feat(github_loader): allow retrieving posts from any branch
1 parent 5bf02bd commit 823dde4

4 files changed

Lines changed: 29 additions & 26 deletions

File tree

src/content/EntriesGitHubLoader.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ import {
88
} from "../helpers/github.ts";
99
import matter from "gray-matter";
1010

11-
export function entriesGitHubLoader(repository: string, directory: string): Loader {
11+
export function entriesGitHubLoader(repository: string, branch: string, directory: string): Loader {
1212
return {
1313
name: 'github-entries-loader',
1414
load: async (context: LoaderContext): Promise<void> => {
1515
const regex = new RegExp(`^${directory}/[^/]+/entry\.mdx?$`);
16-
await loadFilesWithRegex(repository, regex, context);
16+
await loadFilesWithRegex(repository, branch, regex, context);
1717
}
1818
};
1919
}
2020

21-
export function simpleGitHubLoader(repository: string, directory: string): Loader {
21+
export function simpleGitHubLoader(repository: string, branch: string, directory: string): Loader {
2222
return {
2323
name: 'github-simple-loader',
2424
load: async (context: LoaderContext): Promise<void> => {
2525
const regex = new RegExp(`^${directory}/[^/]+\.mdx?$`);
26-
await loadSimpleFilesWithRegex(repository, directory, regex, context);
26+
await loadSimpleFilesWithRegex(repository, branch, directory, regex, context);
2727
}
2828
};
2929
}
3030

31-
export function kvpGitHubLoader(repository: string, file: string, idKey: string, valueKey: string): Loader {
31+
export function kvpGitHubLoader(repository: string, branch: string, file: string, idKey: string, valueKey: string): Loader {
3232
return {
3333
name: 'github-kvp-loader',
3434
load: async (context: LoaderContext): Promise<void> => {
35-
let kvp = await getKeyValueList(repository, file);
35+
let kvp = await getKeyValueList(repository, branch, file);
3636

3737
let toStore = Object.entries(kvp).map(([key, value]) => ({
3838
id: key,
@@ -47,14 +47,14 @@ export function kvpGitHubLoader(repository: string, file: string, idKey: string,
4747
};
4848
}
4949

50-
async function loadFilesWithRegex(repository: string, regex: RegExp, context: LoaderContext): Promise<void> {
51-
const tree = await fetchRepoTree(repository);
50+
async function loadFilesWithRegex(repository: string, branch: string, regex: RegExp, context: LoaderContext): Promise<void> {
51+
const tree = await fetchRepoTree(repository, branch);
5252
const entryFiles = tree.filter(file =>
5353
file.type === 'blob' && regex.test(file.path)
5454
);
5555

5656
for (const file of entryFiles) {
57-
const rawContent = await fetchFileContent(repository, file.path);
57+
const rawContent = await fetchFileContent(repository, branch, file.path);
5858
const {data, content} = matter(rawContent);
5959
const id = file.path.replace('/entry.mdx', '').replace('entry.md', '')
6060

@@ -74,14 +74,14 @@ async function loadFilesWithRegex(repository: string, regex: RegExp, context: Lo
7474
}
7575
}
7676

77-
async function loadSimpleFilesWithRegex(repository: string, directory: string, regex: RegExp, context: LoaderContext): Promise<void> {
78-
const tree = await fetchRepoTree(repository);
77+
async function loadSimpleFilesWithRegex(repository: string, branch: string, directory: string, regex: RegExp, context: LoaderContext): Promise<void> {
78+
const tree = await fetchRepoTree(repository, branch);
7979
const entryFiles = tree.filter(file =>
8080
file.type === 'blob' && regex.test(file.path)
8181
);
8282

8383
for (const file of entryFiles) {
84-
const rawContent = await fetchFileContent(repository, file.path);
84+
const rawContent = await fetchFileContent(repository, branch, file.path);
8585
const {data, content} = matter(rawContent);
8686
const id = file.path.replace('.mdx', '').replace('.md', '').replace('/entry.mdx', '').replace(`${directory}/`, '')
8787

src/content/config.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {entriesGitHubLoader, kvpGitHubLoader, simpleGitHubLoader} from "./Entrie
33
import {isDevelopment} from "../helpers/environment.ts";
44

55
export const repository: string = isDevelopment() ? 'iokode/blog-dev' : 'iokode/blog';
6+
export const branch: string = 'master';
67

78
export const defaultLicense = {
89
code: 'CC BY 4.0',
@@ -11,7 +12,7 @@ export const defaultLicense = {
1112

1213
export const collections = {
1314
posts: defineCollection({
14-
loader: entriesGitHubLoader(repository, 'posts'),
15+
loader: entriesGitHubLoader(repository, branch, 'posts'),
1516
schema: z.object({
1617
title: z.string(),
1718
slug: z.string(),
@@ -26,38 +27,38 @@ export const collections = {
2627
}),
2728

2829
pages: defineCollection({
29-
loader: simpleGitHubLoader(repository, 'pages'),
30+
loader: simpleGitHubLoader(repository, branch, 'pages'),
3031
schema: z.object({
3132
title: z.string(),
3233
slug: z.string(),
3334
}),
3435
}),
3536

3637
licenses: defineCollection({
37-
loader: kvpGitHubLoader(repository, 'licenses.json', 'code', 'url'),
38+
loader: kvpGitHubLoader(repository, branch, 'licenses.json', 'code', 'url'),
3839
schema: z.object({
3940
code: z.string(),
4041
url: z.string().url(),
4142
}),
4243
}),
4344

4445
recommendedSites: defineCollection({
45-
loader: kvpGitHubLoader(repository, 'recommended-sites.json', 'name', 'url'),
46+
loader: kvpGitHubLoader(repository, branch, 'recommended-sites.json', 'name', 'url'),
4647
schema: z.object({
4748
name: z.string(),
4849
url: z.string().url()
4950
})
5051
}),
5152

5253
tags: defineCollection({
53-
loader: simpleGitHubLoader(repository, 'tags'),
54+
loader: simpleGitHubLoader(repository, branch, 'tags'),
5455
schema: z.object({
5556
name: z.string(),
5657
description: z.string(),
5758
}),
5859
}),
5960

6061
authors: defineCollection({
61-
loader: simpleGitHubLoader(repository, 'authors')
62+
loader: simpleGitHubLoader(repository, branch, 'authors')
6263
})
6364
}

src/helpers/github.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,30 @@ export async function getGithubAvatar(username: string): Promise<GetImageResult>
5858
});
5959
}
6060

61-
export async function fetchRepoTree(repository: string): Promise<{
61+
export async function fetchRepoTree(repository: string, branch: string): Promise<{
6262
path: string;
6363
mode: string;
6464
type: string;
6565
sha: string;
6666
size: number;
6767
url: string;
6868
}[]> {
69-
const response = await authenticatedFetch(`https://api.github.com/repos/${repository}/git/trees/main?recursive=1`);
69+
const response = await authenticatedFetch(`https://api.github.com/repos/${repository}/git/trees/${branch}?recursive=1`);
7070
if (!response.ok) {
7171
throw new Error(`Failed to fetch repository tree: ${response.statusText}`);
7272
}
7373
const data = await response.json();
7474
return data.tree; // Array of all files and directories in the repo
7575
}
7676

77-
export async function fetchFileContent(repository: string, filePath: string): Promise<string> {
78-
const rawUrl = `https://raw.githubusercontent.com/${repository}/main/${filePath}`;
77+
export async function fetchFileContent(repository: string, branch: string, filePath: string): Promise<string> {
78+
const rawUrl = `https://raw.githubusercontent.com/${repository}/${branch}/${filePath}`;
7979
const response = await authenticatedFetch(rawUrl);
8080
if (!response.ok) {
8181
throw new Error(`Failed to fetch file ${filePath}: ${response.statusText}`);
8282
}
83+
84+
console.log(filePath);
8385
return await response.text();
8486
}
8587

@@ -138,8 +140,8 @@ export async function getRawContent(repository: string, filePath: string): Promi
138140
return Buffer.from(data.content, 'base64');
139141
}
140142

141-
export async function getKeyValueList(repository: string, file: string): Promise<Record<string, string>> {
142-
let response = await authenticatedFetch(`https://raw.githubusercontent.com/${repository}/refs/heads/main/${file}`);
143+
export async function getKeyValueList(repository: string, branch: string, file: string): Promise<Record<string, string>> {
144+
let response = await authenticatedFetch(`https://raw.githubusercontent.com/${repository}/refs/heads/${branch}/${file}`);
143145
return await response.json();
144146
}
145147

src/pages/posts/assets/[slug]/[file].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {getCollection} from "astro:content";
22
import {fetchRepoTree, getRawContent} from "../../../../helpers/github.ts";
3-
import {repository} from "../../../../content/config.ts";
3+
import {branch, repository} from "../../../../content/config.ts";
44

55
export async function getStaticPaths() {
66
const posts = await getCollection('posts');
7-
const tree = await fetchRepoTree(repository);
7+
const tree = await fetchRepoTree(repository, branch);
88

99
const paths: { params: { slug: string; file: string }; props: { filePath: string } }[] = [];
1010

0 commit comments

Comments
 (0)