@@ -63,50 +63,46 @@ const generateBlogData = async () => {
63
63
'**/index.md' ,
64
64
] ) ;
65
65
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
+ if ( line === '---' ) {
85
+ frontmatterSeparatorsEncountered ++ ;
86
+ }
87
+
88
+ // Once we have two separators we close the readLine and the stream
89
+ if ( frontmatterSeparatorsEncountered === 2 ) {
90
+ _readLine . close ( ) ;
91
+ _stream . close ( ) ;
92
+ }
93
+ } ) ;
94
+
95
+ // Then we parse gray-matter on the frontmatter
96
+ // This allows us to only read the frontmatter part of each file
97
+ // and optimise the read-process as we have thousands of markdown files
98
+ _readLine . on ( 'close' , ( ) => {
99
+ resolve ( getFrontMatter ( filename , rawFrontmatter ) ) ;
100
+ } ) ;
101
+ } )
102
+ )
103
+ ) ;
104
+
105
+ return { categories : [ ...blogCategories ] , posts } ;
110
106
} ;
111
107
112
108
export default generateBlogData ;
0 commit comments