Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mdx/content/blog/hello-world.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Hello world!
description: Example blog post using MDX on Hono webserver
---

This is markdown, but it has JSX components!

<HonoInfoCard />
12 changes: 12 additions & 0 deletions mdx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "mdx",
"license": "MIT",
"scripts": {
"dev": "bun run --hot src/index.tsx"
},
"dependencies": {
"hono": "4.2.4",
"mdx-bundler": "10.1.0",
"esbuild": "0.24.0"
}
}
32 changes: 32 additions & 0 deletions mdx/server/getContent.ts
Original file line number Diff line number Diff line change
@@ -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[]) {
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 };
15 changes: 15 additions & 0 deletions mdx/src/components/HonoInfoCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const HonoInfoCard = () => {
return (<dl>
<dt>Hono</dt>
<dd>Web application framework</dd>

<dt>Benefits</dt>
<dd>Fast</dd>
<dd>Lightweight</dd>
<dd>Built on Web Standards</dd>
<dd>Support for any JavaScript runtime</dd>

<dt>Interested?</dt>
<dd><a href="https://hono.dev/" rel="noreferrer nofollow">Click here to learn more</a></dd>
</dl>)
}
28 changes: 28 additions & 0 deletions mdx/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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/jsx";
import { HonoInfoCard } from "./components/HonoInfoCard";

const app = new Hono();

const jsxComponents = {
HonoInfoCard
}

app.get("/", (c) => {
return c.json({ foo: "Bar" });
}).get("/blog/:slug", async (c) => {
const { code, frontmatter } = await getContent("blog", c.req.param("slug"));
const Component = getMDXComponent(code, { HonoJSX, HonoDOM, _jsx_runtime });

return c.render(<article>
<h2>{frontmatter.title}</h2>
<Component components={jsxComponents} />
</article>);
});

export default app;
11 changes: 11 additions & 0 deletions mdx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx",
"typeRoots": ["./node_modules/@types"],
}
}
16 changes: 14 additions & 2 deletions pages-stack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"jsx": "react-jsx"
}
"jsx": "react-jsx",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"typeRoots": ["./node_modules/@types"],
"paths": {
"*": ["./node_modules/*"]
},
"baseUrl": ".",
"isolatedModules": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}