Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions examples/markdown-frontmatter.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update .vitepress/config.ts?

export const sidebarsExamples = (): DefaultTheme.SidebarItem[] => [

I think putting it into the Integrations section is proper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to Hono? I think this is just a description of Markdown and Frontmatter. Not integrated to Hono.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, skipped most important part (after multiple variations, my mind just skipped hono part) - showing example on Hono route. Will add.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Markdown + Frontmatter

Markdown is a popular lightweight markup language that is widely used for creating website content and documentation. Its simple and readable syntax makes it an excellent choice for writing web pages, blog posts, and technical documentation.

Here's an example of how to handle Markdown content with Hono:

## 🛠️ Installation
```sh
npm install unified remark-parse remark-extract-frontmatter remark-rehype remark-frontmatter rehype-stringify yaml
```

## 🚀 Setup

```ts
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import { parse as parseYaml } from "yaml";

const parse = (markdownContent: string) => {
const { value } = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process(markdownContent);
return { value };
}
```

## 📝 Frontmatter
Frontmatter is a section at the beginning of a Markdown file that contains metadata about the content. It's typically written in YAML format and enclosed between triple-dashes (`---`). This metadata can include information like title, date, author, tags, or any other custom fields you want to define.

Here's how you can extract Frontmatter

1. First install additionll dependencies
```sh
npm install remark-extract-frontmatter remark-frontmatter
```

2. Use `remark` plugins:
```ts
import remarkParse from "remark-parse";
import remarkExtractFrontmatter from "remark-extract-frontmatter";
import remarkFrontmatter from "remark-frontmatter";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import { parse as parseYaml } from "yaml";

const parse = (markdownContent: string) => {
const { data, value } = await unified()
.use(remarkParse)
.use(remarkExtractFrontmatter, { yaml: parseYaml })
.use(remarkFrontmatter)
.use(remarkRehype)
.use(rehypeStringify)
.process(markdownContent);
return { data, value }; // { data: {}; value: string }
}
```

## 💡 Example usage
```tsx
const markdownContent = `---
title: Hello World
description: Hello World example page for hono documentation
nested:
foo: bar
---

## Section A
Hello
`;

const parsedContent = parse(markdownContent);

console.log(parsedContent);
/* {
data: {
title: "Hello World",
description: "Hello World example page for hono documentation",
nested: {
foo: "bar",
},
},
value: "<h2>Section A</h2>\n<p>Hello</p>",
} */
```

## 🔗 See Also
- https://github.com/remarkjs/remark
- https://github.com/rehypejs/rehype
- https://github.com/unifiedjs/unified
- https://jekyllrb.com/docs/front-matter/