import fs from "fs" ; import path from "path" ; import matter from "gray-matter" ; import { v4 as uuid } from "uuid" ; import { exception } from "console" ; import remark from "remark" ; import html from "remark-html" ; import remarkPrism from "remark-prism" ; import { IContentData } from "../pages/articles/[id]" ; const workDirectory = path . join ( process . cwd ( ) , "content" , "work" ) ; const notesDirectory = path . join ( process . cwd ( ) , "content" , "notes" ) ; const articlesDirectory = path . join ( process . cwd ( ) , "content" , "articles" ) ; type IContentType = "articles" | "notes" | "work" ; param { string } contentType export const getAllContentIds = ( contentType ) => { let filenames ; let baseDir ; switch ( contentType ) { case "articles" : baseDir = articlesDirectory ; filenames = fs . readdirSync ( articlesDirectory ) ; break ; case "notes" : baseDir = notesDirectory ; filenames = fs . readdirSync ( notesDirectory ) ; break ; case "work" : baseDir = workDirectory ; filenames = fs . readdirSync ( workDirectory ) ; break ; default : throw new exception ( "You have to provide a content type" ) ; } return filenames . map ( ( filename ) => { const filePath = path . join ( baseDir , filename ) ; const fileContent = fs . readFileSync ( filePath , "utf-8" ) ; const matterResult = matter ( fileContent ) ; return { params : { id : matterResult . data . slug , } , } ; } ) ; } ; param { string } id param { string } contentType export const getContentData = async ( id , contentType ) => { let contentTypeDirectory ; let filenames ; switch ( contentType ) { case "articles" : filenames = fs . readdirSync ( articlesDirectory ) ; contentTypeDirectory = articlesDirectory ; break ; case "notes" : filenames = fs . readdirSync ( notesDirectory ) ; contentTypeDirectory = notesDirectory ; break ; case "work" : filenames = fs . readdirSync ( workDirectory ) ; contentTypeDirectory = workDirectory ; break ; default : throw new exception ( "You have to provide a content type" ) ; } const match = filenames . filter ( ( filename ) => { const filePath = path . join ( contentTypeDirectory , filename ) ; const fileContent = fs . readFileSync ( filePath , "utf-8" ) ; const matterResult = matter ( fileContent ) ; const { slug } = matterResult . data ; return slug === id ; } ) ; const fullPath = path . join ( contentTypeDirectory , match [ 0 ] ) ; const fileContents = fs . readFileSync ( fullPath , "utf-8" ) ; const matterResult = matter ( fileContents ) ; const processedContent = await remark ( ) . use ( html ) . use ( remarkPrism ) . process ( matterResult . content ) ; const contentHtml = processedContent . toString ( ) ; return { id , contentHtml , title : matterResult . data . title , date : matterResult . data . date , previewImage : matterResult . data . previewImage || "" , description : matterResult . data . description || "" , tags : matterResult . data . tags || [ ] , category : matterResult . data . category || "" , } ; } ; param { string } contentType export const getContentList = ( contentType ) => { let contentFiles ; let contentDir ; switch ( contentType ) { case "articles" : contentFiles = fs . readdirSync ( articlesDirectory ) ; contentDir = articlesDirectory ; break ; case "notes" : contentFiles = fs . readdirSync ( notesDirectory ) ; contentDir = notesDirectory ; break ; case "work" : contentFiles = fs . readdirSync ( workDirectory ) ; contentDir = workDirectory ; break ; } const content = contentFiles . filter ( ( content ) => content . endsWith ( ".md" ) ) . map ( ( content ) => { const path = `${ contentDir }/${ content }` ; const rawContent = fs . readFileSync ( path , { encoding : "utf-8" , } ) ; const { data } = matter ( rawContent ) ; return { ... data , previewImage : data . previewImage || "/images/image-placeholder.png" , id : uuid ( ) , } ; } ) ; return content . sort ( sortByDate ) ; } ; param { string } tag export const getContentWithTag = ( tag , contentType ) => { let contentDir ; let contentFiles ; switch ( contentType ) { case "articles" : contentDir = articlesDirectory ; break ; case "notes" : contentDir = notesDirectory ; break ; case "work" : contentDir = workDirectory ; break ; } contentFiles = fs . readdirSync ( contentDir ) ; let contentData = contentFiles . filter ( ( content ) => content . endsWith ( ".md" ) ) . map ( ( content ) => { const path = `${ contentDir }/${ content }` ; const rawContent = fs . readFileSync ( path , { encoding : "utf-8" , } ) ; const { data } = matter ( rawContent ) ; return { ... data , previewImage : data . previewImage || "/images/image-placeholder.png" , id : uuid ( ) , } ; } ) ; const filteredContent = contentData . filter ( ( content ) => { return content . tags && content . tags . includes ( tag ) ; } ) ; return filteredContent . sort ( sortByDate ) ; } ; param { string } tag export const getContentInCategory = ( category , contentType ) => { let contentDir ; let contentFiles ; switch ( contentType ) { case "articles" : contentDir = articlesDirectory ; break ; case "notes" : contentDir = notesDirectory ; break ; case "work" : contentDir = workDirectory ; break ; } contentFiles = fs . readdirSync ( contentDir ) ; let contentData = contentFiles . filter ( ( content ) => content . endsWith ( ".md" ) ) . map ( ( content ) => { const path = `${ contentDir }/${ content }` ; const rawContent = fs . readFileSync ( path , { encoding : "utf-8" , } ) ; const { data } = matter ( rawContent ) ; return { ... data , previewImage : data . previewImage || "/images/image-placeholder.png" , id : uuid ( ) , } ; } ) ; const filteredContent = contentData . filter ( ( content ) => { return content . category && content . category === category ; } ) ; return filteredContent . sort ( sortByDate ) ; } ; param a { Date } param b { Date } export const sortByDate = ( a , b ) => { if ( a . date > b . date ) { return - 1 ; } else if ( a . date < b . date ) { return 1 ; } else { return 0 ; } } ;