Skip to content

chore: refactor the generateBlogData function #7607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 40 additions & 44 deletions apps/site/next-data/generators/blogData.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,46 @@ const generateBlogData = async () => {
'**/index.md',
]);

return new Promise(resolve => {
const posts = [];
const rawFrontmatter = [];

filenames.forEach(filename => {
// We create a stream for reading a file instead of reading the files
const _stream = createReadStream(join(blogPath, filename));

// We create a readline interface to read the file line-by-line
const _readLine = readline.createInterface({ input: _stream });

// Creates an array of the metadata based on the filename
// This prevents concurrency issues since the for-loop is synchronous
// and these event listeners are not
rawFrontmatter[filename] = [0, ''];

// We read line by line
_readLine.on('line', line => {
rawFrontmatter[filename][1] += `${line}\n`;

// We observe the frontmatter separators
if (line === '---') {
rawFrontmatter[filename][0] += 1;
}

// Once we have two separators we close the readLine and the stream
if (rawFrontmatter[filename][0] === 2) {
_readLine.close();
_stream.close();
}
});

// Then we parse gray-matter on the frontmatter
// This allows us to only read the frontmatter part of each file
// and optimise the read-process as we have thousands of markdown files
_readLine.on('close', () => {
posts.push(getFrontMatter(filename, rawFrontmatter[filename][1]));

if (posts.length === filenames.length) {
resolve({ categories: [...blogCategories], posts });
}
});
});
});
const posts = await Promise.all(
filenames.map(
filename =>
new Promise(resolve => {
// We create a stream for reading a file instead of reading the files
const _stream = createReadStream(join(blogPath, filename));

// We create a readline interface to read the file line-by-line
const _readLine = readline.createInterface({ input: _stream });

let rawFrontmatter = '';
let frontmatterSeparatorsEncountered = 0;

// We read line by line
_readLine.on('line', line => {
rawFrontmatter += `${line}\n`;

// We observe the frontmatter separators
if (line === '---') {
frontmatterSeparatorsEncountered++;
}

// Once we have two separators we close the readLine and the stream
if (frontmatterSeparatorsEncountered === 2) {
_readLine.close();
_stream.close();
}
});

// Then we parse gray-matter on the frontmatter
// This allows us to only read the frontmatter part of each file
// and optimise the read-process as we have thousands of markdown files
_readLine.on('close', () => {
resolve(getFrontMatter(filename, rawFrontmatter));
});
})
)
);

return { categories: [...blogCategories], posts };
};

export default generateBlogData;
Loading