feat: add "carnet" and "portfolio" support

This commit is contained in:
Julien Oculi 2024-02-20 11:47:52 +01:00
parent 812da2a857
commit 2b66f76966
4 changed files with 121 additions and 14 deletions

View file

@ -9,7 +9,8 @@ import * as $blog_index from './routes/blog/index.tsx'
import * as $index from './routes/index.tsx'
import * as $machines_id_ from './routes/machines/[id].tsx'
import * as $machines_index from './routes/machines/index.tsx'
import * as $membres_id_ from './routes/membres/[id].tsx'
import * as $membres_id_index from './routes/membres/[id]/index.tsx'
import * as $membres_id_portfolio_path_ from './routes/membres/[id]/portfolio/[...path].tsx'
import * as $membres_index from './routes/membres/index.tsx'
import * as $profil_admin from './routes/profil/admin.tsx'
import * as $profil_index from './routes/profil/index.tsx'
@ -30,7 +31,9 @@ const manifest = {
'./routes/index.tsx': $index,
'./routes/machines/[id].tsx': $machines_id_,
'./routes/machines/index.tsx': $machines_index,
'./routes/membres/[id].tsx': $membres_id_,
'./routes/membres/[id]/index.tsx': $membres_id_index,
'./routes/membres/[id]/portfolio/[...path].tsx':
$membres_id_portfolio_path_,
'./routes/membres/index.tsx': $membres_index,
'./routes/profil/admin.tsx': $profil_admin,
'./routes/profil/index.tsx': $profil_index,

View file

@ -1,12 +0,0 @@
import { PageProps } from '$fresh/server.ts'
import { MemberCard, memberMock } from '../../components/MemberCard.tsx'
export default function Member({ params }: PageProps) {
const Member = memberMock.at(Number(params.id))
return (
Member
? MemberCard(Member)
: <h3>Membre inconnu, peut être serai vous le prochain</h3>
)
}

View file

@ -0,0 +1,66 @@
import { PageProps } from '$fresh/server.ts'
import { MemberCard, memberMock } from '../../../components/MemberCard.tsx'
import { CSS, render as renderMd } from 'gfm'
function Markdown({ children }: { children: string }) {
return (
<>
<style dangerouslySetInnerHTML={{ __html: CSS }}></style>
<div
class='markdown-body'
dangerouslySetInnerHTML={{ __html: renderMd(children) }}
>
</div>
</>
)
}
const db = [
'julien.oculi',
]
async function getCarnet(user: string): Promise<string> {
try {
const response = await fetch(
`https://git.cohabit.fr/${user}/.carnet/raw/branch/main/index.md`,
)
return response.text()
} catch (error) {
return 'Carnet introuvable\n```js\nString(error)\n```'
}
}
export default async function Member(_: Request, { params }: PageProps) {
const id = Number(params.id)
const Member = memberMock.at(id)
if (!Member) {
return <h3>Membre inconnu, peut être serai vous le prochain</h3>
}
const carnet = await getCarnet(db.at(id)!)
return (
<div
style={{
display: 'grid',
gridTemplateColumns: 'auto 1fr',
gap: 'var(--_gap)',
justifyContent: 'center',
}}
>
<div>
{MemberCard(Member)}
<br/>
<a
href={`/membres/${id}/portfolio/index.html`}
class='cta'
target='_blank'
>
Portfolio
</a>
</div>
<Markdown>{carnet}</Markdown>
</div>
)
}

View file

@ -0,0 +1,50 @@
import { Handlers, RouteConfig } from '$fresh/server.ts'
import { contentType } from '$std/media_types/mod.ts'
import { parse } from '$std/path/mod.ts'
const db = [
'julien.oculi',
]
async function getPortfolio(
user: string,
pathname: string,
): Promise<Response> {
const url = new URL(
pathname,
`https://git.cohabit.fr/${user}/.portfolio/raw/branch/main/`,
)
const { ext } = parse(pathname)
try {
const response = await fetch(url)
if (response.ok) {
return new Response(response.body, {
headers: {
'Content-Type': contentType(ext) ?? 'text/plain; charset=utf-8',
},
})
}
throw new Error(response.statusText)
} catch (error) {
return new Response(
'Portfolio introuvable\n```js\n' + String(error) + '\n```',
)
}
}
export const config: RouteConfig = {
skipAppWrapper: true,
}
export const handler: Handlers = {
GET(req, _ctx) {
const url = new URL(req.url)
const id = Number(url.pathname.split('/')[2])
const user = db[id]
const query = url.pathname.split('/').slice(4).join('/')
return getPortfolio(user, query === '' ? 'index.html' : query)
},
}