import fs from "fs" ; import path from "path" ; import matter from "gray-matter" ; import remarkToc from "remark-toc" ; import remarkParse from "remark-parse" ; import { unified } from "unified" ; import remarkRehype from "remark-rehype" ; import rehypeFormat from "rehype-format" ; import rehypeStringify from "rehype-stringify" ; import slug from "rehype-slug" ; import rehypeHighlight from "rehype-highlight" ; /**
 * The LocalFiles service provide a way to get the contents of the files on the local system
 * for it to be processed by the rest of the application
 * More specifically, it provides an interface to get the articles written as markdown in the
 * src/articles/ folder  != ...
 */ const articlesDir = path . join ( process . cwd ( ) , "src" , "articles" ) ; export function getArticles ( ) { const filenames = fs . readdirSync ( articlesDir ) ; const articlesData = filenames . map ( name => { const id = name . replace ( /\.md$/ , '' ) ; const contents = fs . readFileSync ( path . join ( articlesDir , name ) , "utf-8" ) ; const metadata = matter ( contents ) ; return { id , ... metadata . data } ; } ) ; return articlesData ; } export function getArticleIds ( ) { const filenames = fs . readdirSync ( articlesDir ) ; return filenames . map ( name => { return { params : { id : name . replace ( /\.md$/ , "" ) } } ; } ) ; } export async function getArticleData ( id ) { const fullPath = path . join ( articlesDir , `${ id }.md` ) ; const fileContents = fs . readFileSync ( fullPath , 'utf8' ) ; const matterResult = matter ( fileContents ) ; const processed = await unified ( ) . use ( remarkParse ) . use ( remarkToc , { maxDepth : 1 , } ) . use ( remarkRehype ) . use ( slug ) . use ( rehypeHighlight ) . use ( rehypeFormat ) . use ( rehypeStringify ) . process ( matterResult . content ) ; const contentHtml = processed . toString ( ) ; return { id , ... matterResult . data , contentHtml } ; }