-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
45 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 |
---|---|---|
|
@@ -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) => { | ||
|
@@ -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; |
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 |
---|---|---|
|
@@ -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; |