()
| 54 | * @return {Promise<import('../../types').BlogData>} |
| 55 | */ |
| 56 | const generateBlogData = async () => { |
| 57 | // We retrieve the full pathnames of all Blog Posts to read each file individually |
| 58 | const filenames = await getMarkdownFiles(process.cwd(), 'pages/en/blog', [ |
| 59 | '**/index.md', |
| 60 | ]); |
| 61 | |
| 62 | /** |
| 63 | * This contains the metadata of all available blog categories |
| 64 | */ |
| 65 | const blogCategories = new Set(['all']); |
| 66 | |
| 67 | const posts = await Promise.all( |
| 68 | filenames.map( |
| 69 | filename => |
| 70 | new Promise(resolve => { |
| 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 | let rawFrontmatter = ''; |
| 78 | let frontmatterSeparatorsEncountered = 0; |
| 79 | |
| 80 | // We read line by line |
| 81 | _readLine.on('line', line => { |
| 82 | rawFrontmatter += `${line}\n`; |
| 83 | |
| 84 | // We observe the frontmatter separators |
| 85 | if (line === '---') { |
| 86 | frontmatterSeparatorsEncountered++; |
| 87 | } |
| 88 | |
| 89 | // Once we have two separators we close the readLine and the stream |
| 90 | if (frontmatterSeparatorsEncountered === 2) { |
| 91 | _readLine.close(); |
| 92 | _stream.close(); |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | // Then we parse gray-matter on the frontmatter |
| 97 | // This allows us to only read the frontmatter part of each file |
| 98 | // and optimise the read-process as we have thousands of markdown files |
| 99 | _readLine.on('close', () => { |
| 100 | const frontMatterData = getFrontMatter(filename, rawFrontmatter); |
| 101 | |
| 102 | frontMatterData.categories.forEach(category => { |
| 103 | // we add the category to the categories set |
| 104 | blogCategories.add(category); |
| 105 | }); |
| 106 | |
| 107 | resolve(frontMatterData); |
| 108 | }); |
| 109 | }) |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | return { |
no test coverage detected