markdown parsing with frontmatter and syntax highlight

This commit is contained in:
MapleLeaf
2022-01-03 02:33:53 -06:00
committed by Darius
parent d7be8dc5f8
commit 04e6a15b55
3 changed files with 29 additions and 16 deletions

View File

@@ -1,15 +1,15 @@
import grayMatter from "gray-matter"
import MarkdownIt from "markdown-it"
import prism from "markdown-it-prism"
import { readFile } from "node:fs/promises" import { readFile } from "node:fs/promises"
import rehypeStringify from "rehype-stringify"
import remarkParse from "remark-parse"
import remarkRehype from "remark-rehype"
import { unified } from "unified"
const processor = unified() const renderer = new MarkdownIt({
.use(remarkParse) html: true,
.use(remarkRehype) linkify: true,
.use(rehypeStringify) }).use(prism)
export async function renderMarkdownFile(filePath: string) { export async function renderMarkdownFile(filePath: string) {
const result = await processor.process(await readFile(filePath)) const { data, content } = grayMatter(await readFile(filePath, "utf8"))
return result.toString() const html = renderer.render(content)
return { html, data }
} }

View File

@@ -1,12 +1,15 @@
import compression from "compression" import compression from "compression"
import type { Request } from "express"
import express from "express" import express from "express"
import httpTerminator from "http-terminator" import httpTerminator from "http-terminator"
import pino from "pino" import pino from "pino"
import pinoHttp from "pino-http" import pinoHttp from "pino-http"
import * as React from "react" import * as React from "react"
import { renderMarkdownFile } from "./helpers/markdown"
import { sendJsx } from "./helpers/send-jsx" import { sendJsx } from "./helpers/send-jsx"
import { serveFile } from "./helpers/serve-file" import { serveFile } from "./helpers/serve-file"
import { serveTailwindCss } from "./helpers/tailwind" import { serveTailwindCss } from "./helpers/tailwind"
import DocsPage from "./pages/docs"
import { Landing } from "./pages/landing" import { Landing } from "./pages/landing"
const logger = pino() const logger = pino()
@@ -22,12 +25,22 @@ const app = express()
serveFile(new URL("./styles/prism-theme.css", import.meta.url).pathname), serveFile(new URL("./styles/prism-theme.css", import.meta.url).pathname),
) )
.get("/", (req, res) => { .get("/docs/*", async (req: Request<{ 0: string }>, res) => {
sendJsx(res, <Landing />) const { html, data } = await renderMarkdownFile(
`src/docs/${req.params[0]}.md`,
)
sendJsx(
res,
<DocsPage
title={data.title}
description={data.description}
html={html}
/>,
)
}) })
.get("/docs/:docPath+", (req: Request<{ docPath: string }>, res) => { .get("/", (req, res) => {
res.send("doc: " + req.params.docPath) sendJsx(res, <Landing />)
}) })
const server = app.listen(port, () => { const server = app.listen(port, () => {

View File

@@ -5,7 +5,7 @@ import { renderMarkdownFile } from "../helpers/markdown"
import { Html } from "../html" import { Html } from "../html"
import { maxWidthContainer } from "../styles/components" import { maxWidthContainer } from "../styles/components"
const exampleHtml = await renderMarkdownFile( const landingExample = await renderMarkdownFile(
new URL("../components/landing-example.md", import.meta.url).pathname, new URL("../components/landing-example.md", import.meta.url).pathname,
) )
@@ -21,7 +21,7 @@ export function Landing() {
<h1 className="text-6xl font-light">reacord</h1> <h1 className="text-6xl font-light">reacord</h1>
<section <section
className="mx-auto text-sm sm:text-base" className="mx-auto text-sm sm:text-base"
dangerouslySetInnerHTML={{ __html: exampleHtml }} dangerouslySetInnerHTML={{ __html: landingExample.html }}
/> />
<p className="text-2xl font-light">{packageJson.description}</p> <p className="text-2xl font-light">{packageJson.description}</p>
<a <a