|
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" |
2 | 9 |
|
| 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 |
3 | 45 | export const importMd = async (locale: string, slug: string) => { |
| 46 | + const isStaticLocale = STATIC_LOCALES.includes(locale) |
| 47 | + |
4 | 48 | let markdown = "" |
5 | 49 |
|
6 | 50 | if (locale === DEFAULT_LOCALE) { |
7 | 51 | markdown = (await import(`../../../public/content/${slug}/index.md`)) |
8 | 52 | .default |
9 | 53 | } else { |
10 | 54 | 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 | + } |
16 | 64 | } 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 |
20 | 70 |
|
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 | + } |
24 | 85 | } |
25 | 86 | } |
26 | 87 | } |
|
0 commit comments