🖼️ added images support

- Should investigate how to resize the image from .md specs
This commit is contained in:
RJ
2025-11-21 16:15:15 +02:00
parent 2580858ee8
commit a2bd055879
26 changed files with 871 additions and 274 deletions

View File

@@ -1,8 +1,11 @@
import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import { remark } from 'remark'
import remarkGfm from 'remark-gfm'
import { FrontMatter, Post } from './types/frontmatter'
import { generateExcerpt } from './utils'
import { remarkCopyImages } from './remark-copy-images'
const POSTS_PATH = path.join(process.cwd(), 'content', 'blog')
@@ -52,7 +55,7 @@ export function validateFrontmatter(data: any): FrontMatter {
}
}
export function getPostBySlug(slug: string | string[]): Post | null {
export async function getPostBySlug(slug: string | string[]): Promise<Post | null> {
const slugArray = Array.isArray(slug) ? slug : slug.split('/')
const sanitized = slugArray.map(s => sanitizePath(s))
const fullPath = path.join(POSTS_PATH, ...sanitized) + '.md'
@@ -65,19 +68,30 @@ export function getPostBySlug(slug: string | string[]): Post | null {
const { data, content } = matter(fileContents)
const frontmatter = validateFrontmatter(data)
const processed = await remark()
.use(remarkGfm)
.use(remarkCopyImages, {
contentDir: 'content/blog',
publicDir: 'public/blog',
currentSlug: sanitized.join('/'),
})
.process(content)
const processedContent = processed.toString()
return {
slug: sanitized.join('/'),
frontmatter,
content,
readingTime: calculateReadingTime(content),
excerpt: generateExcerpt(content),
content: processedContent,
readingTime: calculateReadingTime(processedContent),
excerpt: generateExcerpt(processedContent),
}
}
export function getAllPosts(includeContent = false): Post[] {
export async function getAllPosts(includeContent = false): Promise<Post[]> {
const posts: Post[] = []
function walkDir(dir: string, prefix = ''): void {
async function walkDir(dir: string, prefix = ''): Promise<void> {
const files = fs.readdirSync(dir)
for (const file of files) {
@@ -85,11 +99,11 @@ export function getAllPosts(includeContent = false): Post[] {
const stat = fs.statSync(filePath)
if (stat.isDirectory()) {
walkDir(filePath, prefix ? `${prefix}/${file}` : file)
await walkDir(filePath, prefix ? `${prefix}/${file}` : file)
} else if (file.endsWith('.md')) {
const slug = prefix ? `${prefix}/${file.replace(/\.md$/, '')}` : file.replace(/\.md$/, '')
try {
const post = getPostBySlug(slug.split('/'))
const post = await getPostBySlug(slug.split('/'))
if (post && !post.frontmatter.draft) {
posts.push(includeContent ? post : { ...post, content: '' })
}
@@ -101,7 +115,7 @@ export function getAllPosts(includeContent = false): Post[] {
}
if (fs.existsSync(POSTS_PATH)) {
walkDir(POSTS_PATH)
await walkDir(POSTS_PATH)
}
return posts.sort(
@@ -110,10 +124,10 @@ export function getAllPosts(includeContent = false): Post[] {
}
export async function getRelatedPosts(currentSlug: string, limit = 3): Promise<Post[]> {
const currentPost = getPostBySlug(currentSlug)
const currentPost = await getPostBySlug(currentSlug)
if (!currentPost) return []
const allPosts = getAllPosts(false)
const allPosts = await getAllPosts(false)
const { category, tags } = currentPost.frontmatter
const scored = allPosts