Skip to content

Commit c497efa

Browse files
chore: refactor the generateBlogData function
1 parent 1b2acc4 commit c497efa

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

Diff for: apps/site/next-data/generators/blogData.mjs

+38-44
Original file line numberDiff line numberDiff line change
@@ -63,50 +63,44 @@ const generateBlogData = async () => {
6363
'**/index.md',
6464
]);
6565

66-
return new Promise(resolve => {
67-
const posts = [];
68-
const rawFrontmatter = [];
69-
70-
filenames.forEach(filename => {
71-
// We create a stream for reading a file instead of reading the files
72-
const _stream = createReadStream(join(blogPath, filename));
73-
74-
// We create a readline interface to read the file line-by-line
75-
const _readLine = readline.createInterface({ input: _stream });
76-
77-
// Creates an array of the metadata based on the filename
78-
// This prevents concurrency issues since the for-loop is synchronous
79-
// and these event listeners are not
80-
rawFrontmatter[filename] = [0, ''];
81-
82-
// We read line by line
83-
_readLine.on('line', line => {
84-
rawFrontmatter[filename][1] += `${line}\n`;
85-
86-
// We observe the frontmatter separators
87-
if (line === '---') {
88-
rawFrontmatter[filename][0] += 1;
89-
}
90-
91-
// Once we have two separators we close the readLine and the stream
92-
if (rawFrontmatter[filename][0] === 2) {
93-
_readLine.close();
94-
_stream.close();
95-
}
96-
});
97-
98-
// Then we parse gray-matter on the frontmatter
99-
// This allows us to only read the frontmatter part of each file
100-
// and optimise the read-process as we have thousands of markdown files
101-
_readLine.on('close', () => {
102-
posts.push(getFrontMatter(filename, rawFrontmatter[filename][1]));
103-
104-
if (posts.length === filenames.length) {
105-
resolve({ categories: [...blogCategories], posts });
106-
}
107-
});
108-
});
109-
});
66+
const posts = await Promise.all(
67+
filenames.map(
68+
filename =>
69+
new Promise(resolve => {
70+
// We create a stream for reading a file instead of reading the files
71+
const _stream = createReadStream(join(blogPath, filename));
72+
73+
// We create a readline interface to read the file line-by-line
74+
const _readLine = readline.createInterface({ input: _stream });
75+
76+
let rawFrontmatter = '';
77+
let frontmatterSeparatorsEncountered = 0;
78+
79+
// We read line by line
80+
_readLine.on('line', line => {
81+
rawFrontmatter += `${line}\n`;
82+
83+
// We observe the frontmatter separators
84+
frontmatterSeparatorsEncountered++;
85+
86+
// Once we have two separators we close the readLine and the stream
87+
if (frontmatterSeparatorsEncountered === 2) {
88+
_readLine.close();
89+
_stream.close();
90+
}
91+
});
92+
93+
// Then we parse gray-matter on the frontmatter
94+
// This allows us to only read the frontmatter part of each file
95+
// and optimise the read-process as we have thousands of markdown files
96+
_readLine.on('close', () => {
97+
resolve(getFrontMatter(filename, rawFrontmatter));
98+
});
99+
})
100+
)
101+
);
102+
103+
return { categories: [...blogCategories], posts };
110104
};
111105

112106
export default generateBlogData;

0 commit comments

Comments
 (0)