-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add a mdx test and example (#13)
* fix: tweak and add a mdx example * test: add MDX test
- Loading branch information
Showing
20 changed files
with
1,664 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
dist | ||
sandbox | ||
.hono | ||
|
||
# Cloudflare | ||
worker | ||
|
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,12 @@ | ||
// eslint-disable-next-line node/no-extraneous-import | ||
import 'hono' | ||
import type { Meta } from './types' | ||
|
||
declare module 'hono' { | ||
interface ContextRenderer { | ||
( | ||
content: string | Promise<string>, | ||
meta?: Meta & { frontmatter: Meta } | ||
): Response | Promise<Response> | ||
} | ||
} |
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,32 @@ | ||
import { jsxRenderer } from 'hono/jsx-renderer' | ||
|
||
export default jsxRenderer( | ||
({ children, title, frontmatter }) => { | ||
return ( | ||
<html lang='en'> | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> | ||
{<title>{title ?? frontmatter?.title ?? 'My Blog'}</title>} | ||
<link rel='stylesheet' href='/static/style.css' /> | ||
</head> | ||
<body> | ||
<header> | ||
<h1> | ||
<a href='/'>My Blog</a> | ||
</h1> | ||
</header> | ||
<main> | ||
<article>{children}</article> | ||
</main> | ||
<footer> | ||
<p>© 2024 My Blog. All rights reserved.</p> | ||
</footer> | ||
</body> | ||
</html> | ||
) | ||
}, | ||
{ | ||
docType: true, | ||
} | ||
) |
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,23 @@ | ||
import type { Meta } from '../types' | ||
|
||
export default async function Top() { | ||
const posts = import.meta.glob<{ frontmatter: Meta }>('./posts/*.mdx', { | ||
eager: true, | ||
}) | ||
return ( | ||
<div> | ||
<h2>Posts</h2> | ||
<ul class='article-list'> | ||
{Object.entries(posts).map(async ([id, module]) => { | ||
if (module.frontmatter) { | ||
return ( | ||
<li> | ||
<a href={`${id.replace(/\.mdx$/, '')}`}>{module.frontmatter.title}</a> | ||
</li> | ||
) | ||
} | ||
})} | ||
</ul> | ||
</div> | ||
) | ||
} |
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,7 @@ | ||
--- | ||
title: Hello World | ||
--- | ||
|
||
## Hollo World | ||
|
||
This is a test |
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,8 @@ | ||
import { showRoutes } from 'hono/dev' | ||
import { createApp } from 'honox/server' | ||
|
||
const app = createApp() | ||
|
||
showRoutes(app) | ||
|
||
export default app |
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,3 @@ | ||
export type Meta = { | ||
title: string | ||
} |
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,21 @@ | ||
{ | ||
"name": "mdx", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "wrangler pages dev ./dist", | ||
"deploy": "$npm_execpath build && wrangler pages deploy ./dist" | ||
}, | ||
"private": true, | ||
"devDependencies": { | ||
"@mdx-js/rollup": "^3.0.0", | ||
"remark-frontmatter": "^5.0.0", | ||
"remark-mdx-frontmatter": "^4.0.0", | ||
"vite": "^5.0.12", | ||
"wrangler": "^3.23.0" | ||
}, | ||
"dependencies": { | ||
"honox": "workspace:^" | ||
} | ||
} |
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,44 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
line-height: 1.6; | ||
margin: 0; | ||
padding: 0; | ||
background: #f4f4f4; | ||
color: #333; | ||
} | ||
|
||
a:link, a:visited { | ||
color: #333; | ||
text-decoration: none; | ||
} | ||
|
||
header, nav, main, footer { | ||
padding: 20px; | ||
} | ||
|
||
header { | ||
text-align: center; | ||
} | ||
|
||
article { | ||
background: #fff; | ||
padding: 20px; | ||
margin-bottom: 20px; | ||
} | ||
|
||
footer { | ||
text-align: center; | ||
} | ||
|
||
.article-list li { | ||
margin: 10px 0; | ||
} | ||
|
||
.article-list li a { | ||
text-decoration: none; | ||
color: #333; | ||
} | ||
|
||
.article-list li a:hover { | ||
color: #0275d8; | ||
} |
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,73 @@ | ||
/** | ||
* This plugin will be improved and released as `@hono/vite-ssg-build`. | ||
*/ | ||
|
||
import fs from 'fs/promises' | ||
// eslint-disable-next-line node/no-extraneous-import | ||
import type { Hono } from 'hono' | ||
import { toSSG } from 'hono/ssg' | ||
import type { Plugin } from 'vite' | ||
import { createServer } from 'vite' | ||
|
||
type BuildConfig = { | ||
outDir?: string | ||
publicDir?: string | ||
} | ||
type SSGOptions = { | ||
entry?: string | ||
outDir?: string | ||
build?: BuildConfig | ||
} | ||
|
||
export const defaultOptions: Required<SSGOptions> = { | ||
entry: './src/index.tsx', | ||
outDir: '.hono', | ||
build: { | ||
outDir: '../dist', | ||
publicDir: '../public', | ||
}, | ||
} | ||
|
||
const SSGBuild = (options?: SSGOptions): Plugin => { | ||
const entry = options?.entry ?? defaultOptions.entry | ||
const outDir = options?.outDir ?? defaultOptions.outDir | ||
return { | ||
name: 'hono-ssg-build', | ||
apply: 'build', | ||
config: async () => { | ||
// Create a server to load the module | ||
const server = await createServer({ | ||
plugins: [], | ||
build: { ssr: true }, | ||
}) | ||
const module = await server.ssrLoadModule(entry) | ||
server.close() | ||
|
||
const app = module['default'] as Hono | ||
|
||
if (!app) { | ||
throw new Error(`Failed to find a named export "default" from ${entry}`) | ||
} | ||
|
||
const result = await toSSG(app, fs, { dir: outDir }) | ||
|
||
if (!result.success) { | ||
throw result.error | ||
} | ||
|
||
return { | ||
root: outDir, | ||
publicDir: options?.build?.publicDir ?? defaultOptions.build.publicDir, | ||
build: { | ||
outDir: options?.build?.outDir ?? defaultOptions.build.outDir, | ||
rollupOptions: { | ||
input: result.files ? [...result.files] : [], | ||
}, | ||
emptyOutDir: true, | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export default SSGBuild |
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,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"lib": [ | ||
"esnext" | ||
], | ||
"types": [ | ||
"vite/client" | ||
], | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "hono/jsx" | ||
}, | ||
} |
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,23 @@ | ||
import devServer from '@hono/vite-dev-server' | ||
import mdx from '@mdx-js/rollup' | ||
import { islandComponents } from 'honox/vite' | ||
import remarkFrontmatter from 'remark-frontmatter' | ||
import remarkMdxFrontmatter from 'remark-mdx-frontmatter' | ||
import { defineConfig } from '../../node_modules/vite' | ||
import ssg from './src/vite-plugin' | ||
|
||
const entry = './app/server.ts' | ||
|
||
export default defineConfig(() => { | ||
return { | ||
plugins: [ | ||
islandComponents(), | ||
devServer({ entry }), | ||
ssg({ entry }), | ||
mdx({ | ||
jsxImportSource: 'hono/jsx', | ||
remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter], | ||
}), | ||
], | ||
} | ||
}) |
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 @@ | ||
# Hello MDX |
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
Oops, something went wrong.