@@ -25,6 +25,7 @@ export interface MarkdownPost {
2525 author : string ;
2626 excerpt : string ;
2727 featuredImage : string ;
28+ featured ?: boolean ;
2829 categories : string [ ] ;
2930 tags : string [ ] ;
3031 seoTitle : string ;
@@ -39,10 +40,18 @@ export interface PostPreview {
3940 author : string ;
4041 excerpt : string ;
4142 featuredImage : string ;
43+ featured ?: boolean ;
4244 categories : string [ ] ;
4345 tags : string [ ] ;
4446}
4547
48+ function parsePostDate ( value : unknown ) : string {
49+ if ( value instanceof Date && ! Number . isNaN ( value . getTime ( ) ) ) {
50+ return value . toISOString ( ) . slice ( 0 , 10 ) ;
51+ }
52+ return typeof value === "string" ? value . trim ( ) : "" ;
53+ }
54+
4655function parsePostFile ( filename : string ) : MarkdownPost {
4756 const fullPath = path . join ( postsDirectory , filename ) ;
4857
@@ -61,10 +70,11 @@ function parsePostFile(filename: string): MarkdownPost {
6170 const post : MarkdownPost = {
6271 slug,
6372 title : data . title || "" ,
64- date : data . date || "" ,
73+ date : parsePostDate ( data . date ) ,
6574 author : data . author || "" ,
6675 excerpt : data . excerpt || "" ,
6776 featuredImage : data . cover || data . featuredImage || "" ,
77+ featured : Boolean ( data . featured ) ,
6878 categories : data . categories || [ ] ,
6979 tags : data . tags || [ ] ,
7080 seoTitle : data . seoTitle || data . title || "" ,
@@ -107,12 +117,33 @@ export function loadAllPosts(): MarkdownPost[] {
107117 return posts ;
108118}
109119
120+ export function getFeaturedPost ( ) : PostPreview | null {
121+ const allPosts = loadAllPosts ( ) ;
122+ const pinned = allPosts . find ( ( p ) => p . featured ) ;
123+ const target = pinned || allPosts [ 0 ] ;
124+ if ( ! target ) return null ;
125+ return {
126+ slug : target . slug ,
127+ title : target . title ,
128+ date : target . date ,
129+ author : target . author ,
130+ excerpt : target . excerpt ,
131+ featuredImage : target . featuredImage ,
132+ featured : Boolean ( target . featured ) ,
133+ categories : target . categories ,
134+ tags : target . tags ,
135+ } ;
136+ }
137+
110138export function getAllPosts (
111139 page = 1 ,
112140 limit = 10 ,
113- options ?: { firstPageLimit ?: number } ,
141+ options ?: { firstPageLimit ?: number ; excludeSlug ?: string } ,
114142) : { posts : PostPreview [ ] ; total : number ; pages : number } {
115- const allPosts = loadAllPosts ( ) ;
143+ let allPosts = loadAllPosts ( ) ;
144+ if ( options ?. excludeSlug ) {
145+ allPosts = allPosts . filter ( ( post ) => post . slug !== options . excludeSlug ) ;
146+ }
116147
117148 const total = allPosts . length ;
118149 const firstPageLimit = options ?. firstPageLimit ?? limit ;
@@ -140,6 +171,7 @@ export function getAllPosts(
140171 author : post . author ,
141172 excerpt : post . excerpt ,
142173 featuredImage : post . featuredImage ,
174+ featured : Boolean ( post . featured ) ,
143175 categories : post . categories ,
144176 tags : post . tags ,
145177 } ) ) ;
@@ -301,6 +333,10 @@ export function getAllPostSlugs(): string[] {
301333}
302334
303335export async function markdownToHtml ( markdown : string ) : Promise < string > {
336+ const normalizedMarkdown = markdown . replace (
337+ / ! \[ ( [ ^ \] ] * ) \] \s * ! \[ \] \( ( [ ^ ) ] + ) \) / g,
338+ "" ,
339+ ) ;
304340 const result = await unified ( )
305341 . use ( remarkParse )
306342 . use ( gfm )
@@ -316,6 +352,6 @@ export async function markdownToHtml(markdown: string): Promise<string> {
316352 strict : "error" ,
317353 } )
318354 . use ( rehypeStringify )
319- . process ( markdown ) ;
355+ . process ( normalizedMarkdown ) ;
320356 return result . toString ( ) ;
321357}
0 commit comments