Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic blog example in the readme #76

Merged
merged 10 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions examples/nextjs-blog/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel
132 changes: 132 additions & 0 deletions examples/nextjs-blog/README.md
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we follow the structure we set out in the issue ...

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# MarkdownDB Next.js Blog Tutorial

This tutorial guides you through creating a simple Next.js-based blog using MarkdownDB. MarkdownDB empowers you to treat markdown files as a database, simplifying content management and querying.
Copy link
Member

Choose a reason for hiding this comment

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

I'd add a bit more about the cool things it will allow us to do ...


## Step 1: Set Up a Next.js Project

Begin by creating a Next.js project. If Next.js is not installed, execute the following command:

```bash
npx create-next-app nextjs-blog
cd nextjs-blog
```

## Step 2: Create a Folder for Blog Posts

Establish a folder to store your blog posts within your project. For instance:

```bash
mkdir content
cd content
```

Inside the content folder, create three sample blog posts using markdown, such as:

```bash
- post-1.md
Copy link
Member

Choose a reason for hiding this comment

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

I'd just have 2 posts ...

And i'd add some content here ... (we are going to at least need the front matter ...)

- post-2.md
- post-3.md
```

## Step 3: Index Markdown Files into SQLite Database

After preparing markdown files, store their metadata in a database using the MarkdownDB CLI. Execute the following command:
Copy link
Member

Choose a reason for hiding this comment

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

I think we maybe want to go the simple json files route?


```bash
npx mddb ./content
```

This command generates a markdown.db file in the execution directory, alongside your project structure.

```plaintext
.
├── markdown.db
└── content
├── post-1.md
├── post-2.md
└── post-3.md
```

## Step 4: Explore the SQLite Database
Copy link
Member

Choose a reason for hiding this comment

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

This is irrelevant IMO ... ...


Explore the SQLite database using a viewer like [SQLite Browser](https://sqlitebrowser.org/). The database contains tables such as 'files,' 'file_tags,' 'links,' and 'tags,' storing metadata, tags, links, and tag information, respectively.

Additionally, install the `mddb` package:

```bash
npm install mddb
```

## Step 5: Load Blog Posts Using MarkdownDB

Create a file for your blog listing page, e.g., `pages/blog.js`. Use the following code snippet:

**Component 1: BlogList**
Copy link
Member

Choose a reason for hiding this comment

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

Use a heading rather than bold.


```jsx
// BlogList.js
import { useEffect, useState } from "react";
import { MarkdownDB } from "markdowndb";

const BlogList = () => {
const [posts, setPosts] = useState([]);

useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Surely we are doing getStaticProps ...

const loadPosts = async () => {
const mdDB = new MarkdownDB();
await mdDB.init();
await mdDB.indexFolder("./content");

// Load all blog posts
const blogPosts = await mdDB.getFiles("./content");
setPosts(blogPosts);
};

loadPosts();
}, []);

return (
<div>
<h1>Blog Posts</h1>
<ul>
{posts.map((post) => (
<li key={post._id}>{post.title}</li>
))}
</ul>
</div>
);
};

export default BlogList;
```

**Component 2: BlogItem**

```jsx
// BlogItem.js
import React from "react";

const BlogItem = ({ post }) => {
// Customize the display of individual blog items
return (
<div>
<h2>{post.title}</h2>
<p>{post.content}</p>
</div>
);
};

export default BlogItem;
```

## Step 6: Run Your Next.js App

Run your Next.js app to view your blog in action:

```bash
npm run dev
```

Visit http://localhost:3000/blog to see your blog posts listed.

Congratulations! You've successfully created a simple Next.js blog using MarkdownDB. Explore more features and customize your blog as needed.
7 changes: 7 additions & 0 deletions examples/nextjs-blog/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
6 changes: 6 additions & 0 deletions examples/nextjs-blog/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}

module.exports = nextConfig
Loading
Loading