[howto/markdown][xs]: rplc @flowershow/markdowndb with mddb

This commit is contained in:
olayway
2023-06-12 16:40:06 +02:00
parent bdfdb2e6a5
commit 038427874a

View File

@@ -13,7 +13,7 @@ mkdir content
Install [MarkdownDB](https://github.com/datopian/markdowndb) package: Install [MarkdownDB](https://github.com/datopian/markdowndb) package:
``` ```
npm i @flowershow/markdowndb npm i mddb
``` ```
And add the following to your `package.json`: And add the following to your `package.json`:
@@ -23,7 +23,7 @@ And add the following to your `package.json`:
"scripts": { "scripts": {
"mddb": "mddb <path-to-your-content-folder>", "mddb": "mddb <path-to-your-content-folder>",
"prebuild": "npm run mddb" "prebuild": "npm run mddb"
}, }
} }
``` ```
@@ -33,13 +33,13 @@ Now, once the data is in the database, you can add the following script to your
```ts ```ts
// lib/mddb.ts // lib/mddb.ts
import { MarkdownDB } from "@flowershow/markdowndb"; import { MarkdownDB } from 'mddb;
// path to the markdown.db file created by the mddb script // path to the markdown.db file created by the mddb script
const dbPath = "markdown.db"; const dbPath = 'markdown.db';
const client = new MarkdownDB({ const client = new MarkdownDB({
client: "sqlite3", client: 'sqlite3',
connection: { connection: {
filename: dbPath, filename: dbPath,
}, },
@@ -53,12 +53,12 @@ export default clientPromise;
Now you can import it across your project to query the database, e.g.: Now you can import it across your project to query the database, e.g.:
```ts ```ts
import clientPromise from "@/lib/mddb"; import clientPromise from '@/lib/mddb';
const mddb = await clientPromise; const mddb = await clientPromise;
const blogs = await mddb.getFiles({ const blogs = await mddb.getFiles({
folder: "blog", folder: 'blog',
extensions: ["md", "mdx"], extensions: ['md', 'mdx'],
}); });
``` ```
@@ -73,10 +73,9 @@ npm i next-mdx-remote
Create the following basic parser for your markdown files, e.g. in `/lib/markdown.ts`: Create the following basic parser for your markdown files, e.g. in `/lib/markdown.ts`:
```ts ```ts
import matter from "gray-matter"; import matter from 'gray-matter';
import remarkGfm from "remark-gfm"; import remarkGfm from 'remark-gfm';
import { serialize } from "next-mdx-remote/serialize"; import { serialize } from 'next-mdx-remote/serialize';
const parse = async function (source) { const parse = async function (source) {
const { content } = matter(source); const { content } = matter(source);
@@ -93,17 +92,16 @@ const parse = async function (source) {
// ... your plugins // ... your plugins
], ],
format, format,
} },
} }
); );
return { return {
mdxSource mdxSource,
}; };
}; };
export default parse; export default parse;
``` ```
## Import, parse and render your markdown files ## Import, parse and render your markdown files
@@ -111,12 +109,11 @@ export default parse;
Create a page in the `/pages` folder that will render your markdown content, e.g. `pages/blog/[[...slug]].tsx`: Create a page in the `/pages` folder that will render your markdown content, e.g. `pages/blog/[[...slug]].tsx`:
```tsx ```tsx
import fs from "fs"; import fs from 'fs';
import { MdxRemote } from "next-mdx-remote";
import clientPromise from "@/lib/mddb.mjs";
import parse from "@/lib/markdown";
import { MdxRemote } from 'next-mdx-remote';
import clientPromise from '@/lib/mddb.mjs';
import parse from '@/lib/markdown';
export default function Page({ source }) { export default function Page({ source }) {
source = JSON.parse(source); source = JSON.parse(source);
@@ -130,15 +127,15 @@ export default function Page({ source }) {
// Import metadata of a file matching the static path and return its parsed source and frontmatter object // Import metadata of a file matching the static path and return its parsed source and frontmatter object
export const getStaticProps = async ({ params }) => { export const getStaticProps = async ({ params }) => {
const urlPath = params?.slug ? (params.slug as string[]).join("/") : "/"; const urlPath = params?.slug ? (params.slug as string[]).join('/') : '/';
const mddb = await clientPromise; const mddb = await clientPromise;
const dbFile = await mddb.getFileByUrl(urlPath); const dbFile = await mddb.getFileByUrl(urlPath);
const filePath = dbFile!.file_path; const filePath = dbFile!.file_path;
// const frontMatter = dbFile!.metadata ?? {}; // const frontMatter = dbFile!.metadata ?? {};
const source = fs.readFileSync(filePath, { encoding: "utf-8" }); const source = fs.readFileSync(filePath, { encoding: 'utf-8' });
const { mdxSource } = await parse(source, "mdx", {}); const { mdxSource } = await parse(source, 'mdx', {});
return { return {
props: { props: {
@@ -148,15 +145,13 @@ export const getStaticProps = async ({ params }) => {
}; };
}; };
// Import metadata of your markdown files from MarkdownDB and return a list of static paths // Import metadata of your markdown files from MarkdownDB and return a list of static paths
export const getStaticPaths = async () => { export const getStaticPaths = async () => {
const mddb = await clientPromise; const mddb = await clientPromise;
const allDocuments = await mddb.getFiles({ extensions: ["md", "mdx"] }); const allDocuments = await mddb.getFiles({ extensions: ['md', 'mdx'] });
const paths = allDocuments const paths = allDocuments.map((page) => {
.map((page) => {
const url = decodeURI(page.url_path); const url = decodeURI(page.url_path);
const parts = url.split("/"); const parts = url.split('/');
return { params: { slug: parts } }; return { params: { slug: parts } };
}); });
@@ -166,4 +161,3 @@ export const getStaticPaths = async () => {
}; };
}; };
``` ```