Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit bdf42c3

Browse files
committed
Add loading the list of co-located media
1 parent d888f4b commit bdf42c3

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/lib/blog-post.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import { lstatSync } from "node:fs";
6-
import { readFile } from "node:fs/promises";
6+
import { readFile, readdir } from "node:fs/promises";
77
import path from "node:path";
88

99
import 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

132153
export default BlogPost;

tests/lib/blog-post.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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

3235
describe( "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() => {

0 commit comments

Comments
 (0)