diff --git a/mdx/content/blog/hello-world.mdx b/mdx/content/blog/hello-world.mdx new file mode 100644 index 0000000..aa28409 --- /dev/null +++ b/mdx/content/blog/hello-world.mdx @@ -0,0 +1,8 @@ +--- +title: Hello world! +description: Example blog post using MDX on Hono webserver +--- + +This is markdown, but it has JSX components! + + \ No newline at end of file diff --git a/mdx/package.json b/mdx/package.json new file mode 100644 index 0000000..6da5a80 --- /dev/null +++ b/mdx/package.json @@ -0,0 +1,12 @@ +{ + "name": "mdx", + "license": "MIT", + "scripts": { + "dev": "bun run --hot src/index.tsx" + }, + "dependencies": { + "hono": "4.2.4", + "mdx-bundler": "10.0.3", + "esbuild": "0.24.0" + } +} diff --git a/mdx/server/getContent.ts b/mdx/server/getContent.ts new file mode 100644 index 0000000..b75f55c --- /dev/null +++ b/mdx/server/getContent.ts @@ -0,0 +1,32 @@ +import { bundleMDX } from "mdx-bundler"; +import { readFile } from "node:fs/promises"; +import { join } from "node:path"; + + +async function getContent(...params: string[]): Promise { + const file_path = join(process.cwd(), "content", ...params.slice(0, -1), `${params.at(-1)}.mdx`); + const [source] = await Promise.all([readFile(file_path, "utf-8")]); + + const post = await bundleMDX({ + source, + cwd: process.cwd(), + jsxConfig: { + JsxLib: { + varName: 'HonoJSX', + package: 'hono/jsx', + }, + JsxDom: { + varName: 'HonoDOM', + package: 'hono/jsx/dom', + }, + jsxRuntime: { + varName: '_jsx_runtime', + package: 'hono/jsx/jsx-runtime', + }, + } + }); + + return post; +} + +export { getContent }; diff --git a/mdx/src/components/HonoInfoCard.tsx b/mdx/src/components/HonoInfoCard.tsx new file mode 100644 index 0000000..8eb5ce7 --- /dev/null +++ b/mdx/src/components/HonoInfoCard.tsx @@ -0,0 +1,15 @@ +export const HonoInfoCard = () => { + return (
+
Hono
+
Web application framework
+ +
Benefits
+
Fast
+
Lightweight
+
Built on Web Standards
+
Support for any JavaScript runtime
+ +
Interested?
+
Click here to learn more
+
) +} \ No newline at end of file diff --git a/mdx/src/index.tsx b/mdx/src/index.tsx new file mode 100644 index 0000000..2515566 --- /dev/null +++ b/mdx/src/index.tsx @@ -0,0 +1,29 @@ +import { Hono } from "hono"; +import { getContent } from "../server/getContent"; + +import * as HonoJSX from "hono/jsx"; +import * as HonoDOM from "hono/jsx/dom"; +import * as _jsx_runtime from "hono/jsx/jsx-runtime"; +import { getMDXComponent } from "mdx-bundler/client"; +import { HonoInfoCard } from "./components/HonoInfoCard"; + +const app = new Hono(); + +const jsxComponents = { + HonoInfoCard +} + +app.get("/", (c) => { + return c.json({ foo: "Bar" }); +}).get("/blog/:slug", (c) => { + const { code, frontmater } = getContent("blog", c.req.param("slug")); + const Component = getMDXComponent(code, { HonoJSX, HonoDOM, _jsx_runtime }); + + return c.render(
+

{frontmater.title}

+ +
+ ) +}); + +export default app; diff --git a/mdx/tsconfig.json b/mdx/tsconfig.json new file mode 100644 index 0000000..9e267ec --- /dev/null +++ b/mdx/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "Bundler", + "strict": true, + "jsx": "react-jsx", + "jsxImportSource": "hono/jsx" + } +}