This repository was archived by the owner on Oct 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33 */
44
55import { lstatSync } from "node:fs" ;
6- import { readFile } from "node:fs/promises" ;
6+ import { readFile , readdir } from "node:fs/promises" ;
77import path from "node:path" ;
88
99import slug from "slug" ;
@@ -28,6 +28,12 @@ class BlogPost {
2828 */
2929 postContent = { } ;
3030
31+ /**
32+ * A list of media co-located with the post.
33+ * @type {Array }
34+ */
35+ postMedia = [ ] ;
36+
3137 /**
3238 * The content of the new post including front matter and content.
3339 * @type {object }
@@ -95,6 +101,20 @@ class BlogPost {
95101 } else {
96102 this . postContent = TomlMatter . parse ( postFileContent ) ;
97103 }
104+
105+ this . postMedia = await readdir (
106+ this . postPath
107+ ) ;
108+
109+ this . postMedia = this . postMedia . filter ( mediaFile => {
110+ if ( path . extname ( mediaFile ) === ".jpeg" ) {
111+ return true ;
112+ } else if ( path . extname ( mediaFile ) === ".jpg" ) {
113+ return true ;
114+ } else {
115+ return false ;
116+ }
117+ } ) ;
98118 }
99119
100120 /**
@@ -127,6 +147,7 @@ class BlogPost {
127147 this . newPostContent . data = frontMatter ;
128148
129149 }
150+
130151}
131152
132153export default BlogPost ;
Original file line number Diff line number Diff line change @@ -28,6 +28,9 @@ const testPassNewFrontMatter = [
2828 "taxonomies" ,
2929 "extra"
3030] ;
31+ const testMediaCountOne = 1 ;
32+ const testMediaCountTwo = 1 ;
33+ const testMediaCountThree = 4 ;
3134
3235describe ( "BlogPost" , ( ) => {
3336
@@ -189,6 +192,34 @@ describe( "BlogPost", () => {
189192
190193 } ) ;
191194
195+ it ( "should load the correct number of media files" , async ( ) => {
196+
197+ let blogPost = new BlogPost ( testPassPathOne ) ;
198+ await blogPost . loadPost ( ) ;
199+
200+ assert . ok (
201+ blogPost . postMedia . length ,
202+ testMediaCountOne
203+ ) ;
204+
205+ blogPost = new BlogPost ( testPassPathTwo ) ;
206+ await blogPost . loadPost ( ) ;
207+
208+ assert . ok (
209+ blogPost . postMedia . length ,
210+ testMediaCountTwo
211+ ) ;
212+
213+ blogPost = new BlogPost ( testPassPathThree ) ;
214+ await blogPost . loadPost ( ) ;
215+
216+ assert . ok (
217+ blogPost . postMedia . length ,
218+ testMediaCountThree
219+ ) ;
220+
221+ } ) ;
222+
192223 } ) ;
193224
194225 describe ( "convertPost" , async ( ) => {
You can’t perform that action at this time.
0 commit comments