Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: feat: added MDX example #223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.0.3",
"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[]): Promise<any> {
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>)
}
29 changes: 29 additions & 0 deletions mdx/src/index.tsx
Original file line number Diff line number Diff line change
@@ -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(<article>
<h2>{frontmater.title}</h2>
<Component components={jsxComponents} />
</article>
)
});

export default app;
10 changes: 10 additions & 0 deletions mdx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
}
}