-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from PHS-TSA/dyamic-pages
Add solar page
- Loading branch information
Showing
7 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: Solar Energy Solutions | ||
description: Solar Energy is an undertapped energy resource. | ||
--- | ||
|
||
<!-- The website should provide basic information, cost, tax rebate information, and clean/green energy practices. --> | ||
|
||
## What is it? | ||
|
||
Solar panels are useful. They collect energy from the sun, and can be placed on roofs discreetly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Head } from "$fresh/runtime.ts"; | ||
import type { Handlers, PageProps } from "$fresh/server.ts"; | ||
import { render } from "$gfm"; | ||
import type { FunctionalComponent } from "preact"; | ||
import IconSolarPanel from "tabler_icons_tsx/solar-panel.tsx"; | ||
import Cover from "../../components/Cover.tsx"; | ||
import Meta from "../../components/Meta.tsx"; | ||
import { type SolutionPage, solutions } from "../../utils/posts.ts"; | ||
|
||
interface SolutionProps { | ||
page: SolutionPage; | ||
} | ||
|
||
const handler: Handlers<SolutionProps> = { | ||
GET(_req, ctx) { | ||
const solution = solutions.find(({ slug }) => slug === ctx.params["slug"]); | ||
|
||
if (solution === undefined) { | ||
return ctx.renderNotFound(); | ||
} | ||
|
||
return ctx.render({ page: solution }); | ||
}, | ||
}; | ||
|
||
const Solution: FunctionalComponent<PageProps<SolutionProps>> = ({ data }) => { | ||
const _pageTitle = data.page.data["title"]; | ||
const pageTitle = typeof _pageTitle === "string" ? _pageTitle : ""; | ||
const _description = data.page.data["description"]; | ||
const description = typeof _description === "string" ? _description : ""; | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<Meta title={pageTitle} /> | ||
</Head> | ||
<main> | ||
<Cover | ||
title={pageTitle} | ||
icon={() => ( | ||
<IconSolarPanel | ||
class="size-52 text-yellow-200 dark:text-yellow-400" | ||
aria-hidden="true" | ||
/> | ||
)} | ||
> | ||
<p class="my-4 dark:text-white">{description}</p> | ||
</Cover> | ||
<article | ||
class="p-10 prose prose-lg dark:prose-invert max-w-none prose-headings:flex prose-headings:flex-row prose-headings:items-center bg-slate-200" | ||
// biome-ignore lint/security/noDangerouslySetInnerHtml: required for markdown w/out a custom renderer | ||
dangerouslySetInnerHTML={{ | ||
__html: render(data?.page.markdown ?? ""), | ||
}} | ||
/> | ||
</main> | ||
</> | ||
); | ||
}; | ||
|
||
export { Solution as default, handler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Extract } from "$std/front_matter/create_extractor.ts"; | ||
import { extract } from "$std/front_matter/yaml.ts"; | ||
import { join } from "$std/path/posix/join.ts"; | ||
|
||
interface SolutionPage { | ||
slug: string; | ||
markdown: string; | ||
data: Record<string, unknown>; | ||
} | ||
|
||
const dir = "src/content"; | ||
|
||
const solutions = await getPosts(); | ||
|
||
/** Get all solutions. */ | ||
export async function getPosts(): Promise<SolutionPage[]> { | ||
const files = Deno.readDir(dir); | ||
const promises = []; | ||
for await (const file of files) { | ||
const slug = file.name.replace(".md", ""); | ||
promises.push(getPost(slug)); | ||
} | ||
|
||
const solutions = await Promise.all(promises); // TODO: validate w/Zod. | ||
|
||
return solutions as SolutionPage[]; | ||
} | ||
|
||
/** Get a solution. */ | ||
export async function getPost(slug: string): Promise<SolutionPage | null> { | ||
let extracted: Extract<Record<string, unknown>>; | ||
try { | ||
const markdown = await Deno.readTextFile(join(dir, `${slug}.md`)); | ||
extracted = extract(markdown); | ||
} catch (_) { | ||
return null; | ||
} | ||
return { markdown: extracted.body, data: extracted.attrs, slug }; | ||
} | ||
|
||
export { solutions, type SolutionPage }; |