Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/blakewilson/markdown-posts
A simple API to transform a directory of markdown files with frontmatter into data
https://github.com/blakewilson/markdown-posts
markdown matter next remark
Last synced: 15 days ago
JSON representation
A simple API to transform a directory of markdown files with frontmatter into data
- Host: GitHub
- URL: https://github.com/blakewilson/markdown-posts
- Owner: blakewilson
- License: mit
- Created: 2021-05-16T04:30:33.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-05-16T17:30:39.000Z (over 3 years ago)
- Last Synced: 2025-01-09T05:38:30.922Z (18 days ago)
- Topics: markdown, matter, next, remark
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/markdown-posts
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Markdown Posts
A simple API to transform a directory of markdown files with frontmatter into data.
## Usage
### Installation
Install the package:
```bash
npm install markdown-posts
```### Usage
By default, `markdown-posts` looks for all markdown files in the `posts` directory.
Below is a basic example in Next.js:
List all posts on home page:
**`pages/index.tsx`**
```tsx
import { getAllPosts } from "markdown-posts";export default function Home({ posts }) {
return (
- {post.title}
{posts.map((post, index) => (
))}
);
}
export async function getStaticProps() {
const posts = await getAllPosts(["title", "date", "slug"]);
return {
props: {
posts,
},
};
}
```
Display a single post:
**`pages/[slug].tsx`**
```tsx
import { getPostBySlug, getAllPosts } from "markdown-posts";
export default function SinglePost({ post }) {
return (
{post.title}
);
}
export async function getStaticProps({ params }) {
const { slug } = params;
const post = await getPostBySlug(slug, ["title", "date", "slug"]);
return {
props: {
post,
},
};
}
export async function getStaticPaths() {
const posts = await getAllPosts(["slug"]);
return {
paths: posts.map((post) => {
return {
params: {
slug: post.slug,
},
};
}),
fallback: false,
};
}
```
## License
This project is licensed under the [MIT license](https://github.com/blakewilson/markdown-posts/blob/master/LICENSE)