Skip to content

Commit 7d65c04

Browse files
committed
refactor: fetch markdown files remotely from GitHub for non-static locales
1 parent 9e3dc73 commit 7d65c04

File tree

1 file changed

+73
-12
lines changed

1 file changed

+73
-12
lines changed

src/lib/md/import.ts

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,87 @@
1-
import { DEFAULT_LOCALE } from "../constants"
1+
import {
2+
BASE_TIME_UNIT,
3+
DEFAULT_LOCALE,
4+
GITHUB_REPO_URL,
5+
STATIC_LOCALES,
6+
TIMEOUT_MS,
7+
} from "../constants"
8+
import { fetchWithTimeoutAndRevalidation } from "../utils/data/utils"
29

10+
const getGitHubRawUrl = (locale: string, slug: string): string => {
11+
const repoPath = GITHUB_REPO_URL.replace("https://github.com/", "").replace(
12+
/\/$/,
13+
""
14+
)
15+
const branch = "master"
16+
17+
if (locale === DEFAULT_LOCALE) {
18+
return `https://raw.githubusercontent.com/${repoPath}/${branch}/public/content/${slug}/index.md`
19+
}
20+
21+
return `https://raw.githubusercontent.com/${repoPath}/${branch}/public/content/translations/${locale}/${slug}/index.md`
22+
}
23+
24+
const fetchMarkdownFromGitHub = async (
25+
locale: string,
26+
slug: string
27+
): Promise<string> => {
28+
const url = getGitHubRawUrl(locale, slug)
29+
const response = await fetchWithTimeoutAndRevalidation(
30+
url,
31+
TIMEOUT_MS,
32+
BASE_TIME_UNIT * 24
33+
)
34+
35+
if (!response.ok) {
36+
throw new Error(
37+
`Failed to fetch markdown from GitHub: ${response.status} ${response.statusText}`
38+
)
39+
}
40+
41+
return await response.text()
42+
}
43+
44+
// Imports markdown content either locally (for STATIC_LOCALES) or remotely from GitHub
345
export const importMd = async (locale: string, slug: string) => {
46+
const isStaticLocale = STATIC_LOCALES.includes(locale)
47+
448
let markdown = ""
549

650
if (locale === DEFAULT_LOCALE) {
751
markdown = (await import(`../../../public/content/${slug}/index.md`))
852
.default
953
} else {
1054
try {
11-
markdown = (
12-
await import(
13-
`../../../public/content/translations/${locale}/${slug}/index.md`
14-
)
15-
).default
55+
if (isStaticLocale) {
56+
markdown = (
57+
await import(
58+
`../../../public/content/translations/${locale}/${slug}/index.md`
59+
)
60+
).default
61+
} else {
62+
markdown = await fetchMarkdownFromGitHub(locale, slug)
63+
}
1664
} catch (error) {
17-
const markdown = (
18-
await import(`../../../public/content/${slug}/index.md`)
19-
).default
65+
// Fallback to default locale - try local first, then remote
66+
try {
67+
const markdown = (
68+
await import(`../../../public/content/${slug}/index.md`)
69+
).default
2070

21-
return {
22-
markdown,
23-
isTranslated: false,
71+
return {
72+
markdown,
73+
isTranslated: false,
74+
}
75+
} catch (localError) {
76+
try {
77+
const markdown = await fetchMarkdownFromGitHub(DEFAULT_LOCALE, slug)
78+
return {
79+
markdown,
80+
isTranslated: false,
81+
}
82+
} catch (githubError) {
83+
throw error
84+
}
2485
}
2586
}
2687
}

0 commit comments

Comments
 (0)