Skip to content

Commit

Permalink
refactor: Migrate route
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Nov 22, 2024
1 parent 6a99817 commit a6ce4bc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
51 changes: 51 additions & 0 deletions src/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
* This module is to manage routes for Web API (not render html and images).
*/
import { Hono } from 'hono';
import { html, raw } from 'hono/html';
import { Octokit } from '@octokit/rest';
import { fetchContent } from './client';
import { Input } from './components';
import { parseSlug } from './models';
import type { ZennContent } from './models';
import { parseContentMarkdown } from './parser';

const app = new Hono();
app.use(async (c, next) => {
Expand Down Expand Up @@ -88,4 +94,49 @@ app.get('/', (c) => {
);
});

app.get('/:slug', async (c) => {
let props: ZennContent;
const octokit = new Octokit({
auth: c.env.REPO_PAT,
});
const addr = parseSlug(c.req.param('slug'));
try {
const md = await fetchContent(octokit, addr);
props = parseContentMarkdown(md);
return c.html(
html`
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>Zenn article</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.css"
/>
</head>
<body>
<div>
<h1>
${props.frontMatter?.title}
<br />
${props.frontMatter?.emoji}
</h1>
</div>
<hr />
<hr />
<div class="znc">${raw(props.body)}</div>
</body>
</html>
`,
);
} catch (error) {
console.error(error);
if (error.status) {
c.status(404);
return c.text('Content is not found.');
}
}
});

export default app;
45 changes: 0 additions & 45 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,4 @@ app.use('*', (c: Context, next: any) => {
app.route('/api', api);
app.route('/', htmlRouter);

app.get('/:slug', async (c) => {
let props: ZennContent;
const octokit = new Octokit({
auth: c.env.REPO_PAT,
});
const addr = parseSlug(c.req.param('slug'));
try {
const md = await fetchContent(octokit, addr);
props = parseContentMarkdown(md);
return c.html(
html`
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>Zenn article</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.css"
/>
</head>
<body>
<div>
<h1>
${props.frontMatter?.title}
<br />
${props.frontMatter?.emoji}
</h1>
</div>
<hr />
<hr />
<div class="znc">${raw(props.body)}</div>
</body>
</html>
`,
);
} catch (error) {
console.error(error);
if (error.status) {
c.status(404);
return c.text('Content is not found.');
}
}
});

export default app;

0 comments on commit a6ce4bc

Please sign in to comment.