diff options
| author | mihna123 <mihailonvojinovic@gmail.com> | 2026-06-08 14:19:32 +0200 |
|---|---|---|
| committer | mihna123 <mihailonvojinovic@gmail.com> | 2026-06-08 14:19:32 +0200 |
| commit | 67eb91ad6dccf2a14e3f8d3b285c75d251322973 (patch) | |
| tree | 648fc2de5568ef8f541e6c7033e3c1a0a953a92e /src/lib | |
| parent | 3f513363524c7cfd9d7fb573454090d2ba01bda9 (diff) | |
| download | invoice-67eb91ad6dccf2a14e3f8d3b285c75d251322973.tar.gz invoice-67eb91ad6dccf2a14e3f8d3b285c75d251322973.zip | |
feat(blogs): implement blogging functionality
- Implement static blog generation from markdown files
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/blogs/api.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/blogs/api.ts b/src/lib/blogs/api.ts new file mode 100644 index 0000000..3fcdd15 --- /dev/null +++ b/src/lib/blogs/api.ts @@ -0,0 +1,42 @@ +import path from 'path'; +import fs from 'fs'; +import matter from 'gray-matter'; +import { remark } from 'remark'; +import html from 'remark-html'; + +const blogsDir = path.join(process.cwd(), '_blogs'); + +export interface Blog { + author: { + name: string; + social: string; + }; + date: Date; + title: string; + slug: string; + excerpt: string; + content: string; +} + +export const getBlogFromSlug = (slug: string): Blog => { + const fullPath = path.join(blogsDir, `${slug}.md`); + const file = fs.readFileSync(fullPath, 'utf8'); + const { data, content } = matter(file); + return { + ...data, + date: new Date(data.date), + content, + slug, + } as Blog; +}; + +export const getAllBlogs = (): Blog[] => { + const rawMdFiles = fs.readdirSync(blogsDir); + const slugs = rawMdFiles.map((file) => file.replace(/\.md$/, '')); + return slugs.map(getBlogFromSlug); +}; + +export const markdownToHtml = async (markdown: string): Promise<string> => { + const htmlResult = await remark().use(html).process(markdown); + return htmlResult.toString(); +}; |
