40 lines
871 B
TypeScript
40 lines
871 B
TypeScript
import fs from 'fs'
|
|
import path from 'path'
|
|
import matter from 'gray-matter'
|
|
|
|
export interface MarkdownContent {
|
|
frontmatter: {
|
|
[key: string]: any
|
|
}
|
|
content: string
|
|
}
|
|
|
|
export function getMarkdownContent(filePath: string): MarkdownContent {
|
|
try {
|
|
const fullPath = path.join(process.cwd(), filePath)
|
|
|
|
// Ensure file exists
|
|
if (!fs.existsSync(fullPath)) {
|
|
console.warn(`File not found: ${filePath}`)
|
|
return {
|
|
frontmatter: {},
|
|
content: 'Content not found.'
|
|
}
|
|
}
|
|
|
|
const fileContents = fs.readFileSync(fullPath, 'utf8')
|
|
const { data, content } = matter(fileContents)
|
|
|
|
return {
|
|
frontmatter: data,
|
|
content,
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error reading markdown file: ${filePath}`, error)
|
|
return {
|
|
frontmatter: {},
|
|
content: 'Error loading content.'
|
|
}
|
|
}
|
|
}
|