-
-
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.
feat: create app/docs/[...categories]/page.jsx
- Loading branch information
1 parent
875dbab
commit 240c276
Showing
1 changed file
with
46 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { promises as fs } from 'fs'; | ||
import parse from 'html-react-parser'; | ||
|
||
import { REPOSITORY } from '@/constants/github'; | ||
import Article from '@/layouts/Article'; | ||
|
||
export default async function Page({ params }) { | ||
const markdown = await fs.readFile( | ||
`${process.cwd()}/src/docs/${params.categories.join('/')}.md`, | ||
'utf-8', | ||
); | ||
|
||
return <Article>{await markdownToJsx(markdown)}</Article>; | ||
} | ||
|
||
async function markdownToHtml(markdown) { | ||
const response = await fetch('https://api.github.com/markdown', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/vnd.github+json', | ||
'Content-Type': 'application/json', | ||
'X-GitHub-Api-Version': '2022-11-28', | ||
}, | ||
body: JSON.stringify({ | ||
text: markdown, | ||
mode: 'gfm', | ||
context: REPOSITORY.fullName, | ||
}), | ||
}); | ||
|
||
return response.text(); | ||
} | ||
|
||
function htmlToJsx(html) { | ||
return parse(html, { | ||
replace: ({ name, attribs }) => { | ||
if (name === 'img' && attribs.src.startsWith('/public')) { | ||
attribs.src = attribs.src.replace(/^\/public/, ''); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
async function markdownToJsx(markdown) { | ||
return htmlToJsx(await markdownToHtml(markdown)); | ||
} |