feat: add render function (#2454)

This commit is contained in:
Luca Casonato
2024-05-24 18:31:34 +02:00
committed by GitHub
parent 82d027cbcd
commit f2162d24bf
14 changed files with 123 additions and 64 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
import { asset } from "@fresh/core/runtime";
import { df } from "../utils/state.ts";
import { define } from "../utils/state.ts";
export default df.page(function App({ Component, state, url }) {
export default define.page(function App({ Component, state, url }) {
return (
<html lang="en">
<head>
+20 -21
View File
@@ -1,4 +1,4 @@
import { type Handlers, HttpError, type PageProps } from "@fresh/core";
import { HttpError, page } from "@fresh/core";
import { asset, Partial } from "@fresh/core/runtime";
import { SidebarCategory } from "../../components/DocsSidebar.tsx";
import Footer from "../../components/Footer.tsx";
@@ -15,6 +15,7 @@ import toc from "../../../docs/toc.ts";
import { TableOfContents } from "../../islands/TableOfContents.tsx";
import SearchButton from "../../islands/SearchButton.tsx";
import VersionSelect from "../../islands/VersionSelect.tsx";
import { define } from "../../utils/state.ts";
interface Data {
page: Page;
@@ -43,7 +44,7 @@ interface Page extends TableOfContentsEntry {
const pattern = new URLPattern({ pathname: "/:version/:page*" });
export const handler: Handlers<Data> = {
export const handler = define.handlers<Data>({
async GET(ctx) {
const slug = ctx.params.slug;
@@ -61,20 +62,20 @@ export const handler: Handlers<Data> = {
throw new HttpError(404);
}
let { version, page = "" } = match.pathname.groups;
let { version, page: path = "" } = match.pathname.groups;
if (!version) {
throw new HttpError(404);
}
// Latest version doesn't show up in the url
if (!TABLE_OF_CONTENTS[version]) {
page = version + (page ? "/" + page : "");
path = version + (path ? "/" + path : "");
version = LATEST_VERSION;
}
// Check if the page exists
const currentToc = TABLE_OF_CONTENTS[version];
const entry = currentToc[page];
const entry = currentToc[path];
if (!entry) {
throw new HttpError(404);
}
@@ -83,7 +84,7 @@ export const handler: Handlers<Data> = {
const versionLinks: VersionLink[] = [];
for (const version in TABLE_OF_CONTENTS) {
const label = toc[version].label;
const maybeEntry = TABLE_OF_CONTENTS[version][page];
const maybeEntry = TABLE_OF_CONTENTS[version][path];
// Check if the same page is available for this version and
// link to that. Pick the index page for that version if an
@@ -128,23 +129,21 @@ export const handler: Handlers<Data> = {
: "Fresh Document";
ctx.state.ogImage = new URL(asset("/og-image.webp"), ctx.url).href;
return {
data: {
page: {
...entry,
markdown: body,
data: attrs ?? {},
versionLinks,
version,
prevNav,
nextNav,
},
return page({
page: {
...entry,
markdown: body,
data: attrs ?? {},
versionLinks,
version,
prevNav,
nextNav,
},
};
});
},
};
});
export default function DocsPage(props: PageProps<Data>) {
export default define.page<typeof handler>(function DocsPage(props) {
const { page } = props.data;
const { html, headings } = renderMarkdown(page.markdown);
@@ -241,7 +240,7 @@ export default function DocsPage(props: PageProps<Data>) {
</div>
</div>
);
}
});
function MobileSidebar({ page }: { page: Page }) {
return (
+7 -5
View File
@@ -1,5 +1,5 @@
import { asset } from "@fresh/core/runtime";
import type { Handlers, PageProps } from "@fresh/core";
import { page } from "@fresh/core";
import VERSIONS from "../../versions.json" with { type: "json" };
import Footer from "../components/Footer.tsx";
import Header from "../components/Header.tsx";
@@ -12,9 +12,9 @@ import { FormsSection } from "../components/homepage/FormsSection.tsx";
import { Simple } from "../components/homepage/Simple.tsx";
import { SocialProof } from "../components/homepage/SocialProof.tsx";
import { DenoSection } from "../components/homepage/DenoSection.tsx";
import { df } from "../utils/state.ts";
import { define } from "../utils/state.ts";
export const handler = df.handlers({
export const handler = define.handlers({
GET(ctx) {
const { req } = ctx;
const accept = req.headers.get("accept");
@@ -32,6 +32,8 @@ export const handler = df.handlers({
ctx.state.description =
"Fresh features just-in-time edge rendering, island based interactivity, and zero-configuration TypeScript support. Fast to write; fast to run.";
ctx.state.ogImage = new URL(asset("/og-image.webp"), ctx.url).href;
return page();
},
async POST(ctx) {
const headers = new Headers();
@@ -45,7 +47,7 @@ export const handler = df.handlers({
},
});
export default function MainPage(props: PageProps) {
export default define.page<typeof handler>(function MainPage(props) {
const origin = `${props.url.protocol}//${props.url.host}`;
return (
@@ -68,7 +70,7 @@ export default function MainPage(props: PageProps) {
<Footer class="!mt-0" />
</div>
);
}
});
function HelloBar() {
return (
+4 -3
View File
@@ -1,7 +1,8 @@
import type { Handlers, RouteConfig } from "@fresh/core";
import type { RouteConfig } from "@fresh/core";
import { format, parse } from "$std/semver/mod.ts";
import VERSIONS from "../../versions.json" with { type: "json" };
import { extname } from "$std/path/mod.ts";
import { define } from "../utils/state.ts";
const BASE_URL = "https://raw.githubusercontent.com/denoland/fresh/";
@@ -15,7 +16,7 @@ const contentTypes = new Map([
[".wasm", "application/wasm"],
]);
export const handler: Handlers = {
export const handler = define.handlers({
async GET(ctx) {
const accept = ctx.req.headers.get("Accept");
const isHTML = accept?.includes("text/html");
@@ -54,7 +55,7 @@ export const handler: Handlers = {
return response;
},
};
});
export const config: RouteConfig = {
routeOverride: "/@:version/:path*",
+2 -2
View File
@@ -1,6 +1,6 @@
import { df } from "../../utils/state.ts";
import { define } from "../../utils/state.ts";
export const handler = df.defineMiddleware((ctx) => {
export const handler = define.middleware((ctx) => {
ctx.state.noIndex = true;
return ctx.next();
});
+6 -5
View File
@@ -1,23 +1,24 @@
import { asset } from "@fresh/core/runtime";
import type { PageProps } from "@fresh/core";
import { page } from "@fresh/core";
import Projects, { type Project } from "../components/Projects.tsx";
import Header from "../components/Header.tsx";
import Footer from "../components/Footer.tsx";
import projects from "../data/showcase.json" with { type: "json" };
import { df } from "../utils/state.ts";
import { define } from "../utils/state.ts";
const TITLE = "Showcase | Fresh";
const DESCRIPTION = "Selection of projects that have been built with Fresh.";
export const handler = df.handlers({
export const handler = define.handlers({
GET(ctx) {
ctx.state.title = TITLE;
ctx.state.description = DESCRIPTION;
ctx.state.ogImage = new URL(asset("/og-image.webp"), ctx.url).href;
return page();
},
});
export default function ShowcasePage(props: PageProps) {
export default define.page<typeof handler>(function ShowcasePage() {
return (
<>
<Header title="showcase" active="/showcase" />
@@ -71,7 +72,7 @@ export default function ShowcasePage(props: PageProps) {
</div>
</>
);
}
});
function Showcase({ items }: { items: Project[] }) {
return (
+6 -5
View File
@@ -1,20 +1,21 @@
import { page } from "@fresh/core";
import { PageSection } from "../components/PageSection.tsx";
import { DemoBox } from "../components/homepage/DemoBox.tsx";
import { df } from "../utils/state.ts";
import { define } from "../utils/state.ts";
export const handler = df.handlers({
export const handler = define.handlers({
GET(ctx) {
const url = new URL(ctx.req.url);
const search = new URLSearchParams(url.search);
const vote = search.get("vote");
return { data: { vote } };
return page({ vote });
},
});
export default df.page<typeof handler>(function ThanksForSubscribing(
export default define.page<typeof handler>(function ThanksForSubscribing(
props,
) {
const vote = props?.data?.vote ? props.data.vote.replaceAll(/-/g, " ") : null;
const vote = props.data.vote ? props.data.vote.replaceAll(/-/g, " ") : null;
return (
<>
<PageSection>
+3 -3
View File
@@ -1,7 +1,7 @@
import type { Handlers } from "@fresh/core";
import { define } from "../utils/state.ts";
import VERSIONS from "../../versions.json" with { type: "json" };
export const handler: Handlers = {
export const handler = define.handlers({
GET({ req }) {
const accept = req.headers.get("accept");
let path = "/docs/concepts/updating";
@@ -13,4 +13,4 @@ export const handler: Handlers = {
status: 307,
});
},
};
});
+1 -1
View File
@@ -7,4 +7,4 @@ export interface State {
noIndex?: boolean;
}
export const df = createDefine<State>();
export const define = createDefine<State>();