Files
hatchet/frontend/docs/components/code/codeData.tsx
Gabe Ruttner c88a18180a docs: git driven snips -- on failure page (#1043)
* feat: universal tabs

* fix: consistent tab naming

* feat: paths

* wip: chunk parser

* wip: dynamic snips

* wip: components

* docs: annotated go

* docs: improved annotations

* chore: fix

* feat: on-failure

* todo: switch branch to main

* chore: lint

* chore: lint

* fix: resolved theme

* cache

* fix: theme again...

* todo list

* docs: shadcn

* docs: code example uses shadcn

* fix: build

* python note

* chore: lint

* chore: swap main

* feat: local story

* fix: is dev

* revert the change that broke vercel

* fix: redirect?

* feat: seo title
2024-11-18 15:50:51 -08:00

100 lines
2.3 KiB
TypeScript

const defaultUser = 'hatchet-dev'
const defaultRepos = {
'ts': 'hatchet-typescript',
'py': 'hatchet-python',
'go': 'hatchet'
}
const localPaths = {
'ts': 'sdk-typescript',
'py': 'sdk-python',
'go': 'oss'
}
export const extToLanguage = {
'ts': 'typescript',
'py': 'python',
'go': 'go'
}
const defaultBranch = 'main'
export type RepoProps = {
user?: string
repo?: string
branch?: string
path: string
}
const getLocalUrl = (ext: string, { path }: RepoProps) => {
return `http://localhost:4001/${localPaths[ext]}/${path}`
}
const isDev = process?.env?.NODE_ENV === 'development'
const getRawUrl = ({ user, repo, branch, path }: RepoProps) => {
const ext = path.split('.').pop()
if (isDev) {
return getLocalUrl(ext, { path })
}
return `https://raw.githubusercontent.com/${user || defaultUser}/${repo || defaultRepos[ext]}/refs/heads/${branch || defaultBranch}/${path}`
}
const getUIUrl = ({ user, repo, branch, path }: RepoProps) => {
const ext = path.split('.').pop()
if (isDev) {
return getLocalUrl(ext, { path })
}
return `https://github.com/${user || defaultUser}/${repo || defaultRepos[ext]}/blob/${branch || defaultBranch}/${path}`
}
export type Src = {
raw: string
props: RepoProps
rawUrl: string
githubUrl: string
language?: string
}
export const getSnippets = (props: RepoProps[]): Promise<{ props: { ssg: { contents: Src[] } } }> => {
return Promise.all(
props.map(async (prop) => {
const rawUrl = getRawUrl(prop);
const githubUrl = getUIUrl(prop);
const fileExt = prop.path.split('.').pop() as keyof typeof extToLanguage;
const language = extToLanguage[fileExt];
try {
const response = await fetch(rawUrl);
const raw = await response.text();
return {
raw,
props: prop,
rawUrl,
githubUrl,
language
};
} catch (error) {
// Return object with empty raw content but preserve URLs on failure
return {
raw: '',
props: prop,
rawUrl,
githubUrl,
language
};
}
})
).then(results => ({
props: {
ssg: {
contents: results
},
// revalidate every 60 seconds
revalidate: 60
}
}));
}