@@ -11,11 +11,6 @@ import { getMarkdownFiles } from '../../next.helpers.mjs';
11
11
// gets the current blog path based on local module path
12
12
const blogPath = join ( process . cwd ( ) , 'pages/en/blog' ) ;
13
13
14
- /**
15
- * This contains the metadata of all available blog categories
16
- */
17
- const blogCategories = new Set ( [ 'all' ] ) ;
18
-
19
14
/**
20
15
* This method parses the source (raw) Markdown content into Frontmatter
21
16
* and returns basic information for blog posts
@@ -39,12 +34,6 @@ const getFrontMatter = (filename, source) => {
39
34
// all = (all blog posts), publish year and the actual blog category
40
35
const categories = [ category , `year-${ publishYear } ` , 'all' ] ;
41
36
42
- // we add the year to the categories set
43
- blogCategories . add ( `year-${ publishYear } ` ) ;
44
-
45
- // we add the category to the categories set
46
- blogCategories . add ( category ) ;
47
-
48
37
// this is the url used for the blog post it based on the category and filename
49
38
const slug = `/blog/${ category } /${ basename ( filename , extname ( filename ) ) } ` ;
50
39
@@ -63,50 +52,58 @@ const generateBlogData = async () => {
63
52
'**/index.md' ,
64
53
] ) ;
65
54
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
- } ) ;
55
+ /**
56
+ * This contains the metadata of all available blog categories
57
+ */
58
+ const blogCategories = new Set ( [ 'all' ] ) ;
59
+
60
+ const posts = await Promise . all (
61
+ filenames . map (
62
+ filename =>
63
+ new Promise ( resolve => {
64
+ // We create a stream for reading a file instead of reading the files
65
+ const _stream = createReadStream ( join ( blogPath , filename ) ) ;
66
+
67
+ // We create a readline interface to read the file line-by-line
68
+ const _readLine = readline . createInterface ( { input : _stream } ) ;
69
+
70
+ let rawFrontmatter = '' ;
71
+ let frontmatterSeparatorsEncountered = 0 ;
72
+
73
+ // We read line by line
74
+ _readLine . on ( 'line' , line => {
75
+ rawFrontmatter += `${ line } \n` ;
76
+
77
+ // We observe the frontmatter separators
78
+ if ( line === '---' ) {
79
+ frontmatterSeparatorsEncountered ++ ;
80
+ }
81
+
82
+ // Once we have two separators we close the readLine and the stream
83
+ if ( frontmatterSeparatorsEncountered === 2 ) {
84
+ _readLine . close ( ) ;
85
+ _stream . close ( ) ;
86
+ }
87
+ } ) ;
88
+
89
+ // Then we parse gray-matter on the frontmatter
90
+ // This allows us to only read the frontmatter part of each file
91
+ // and optimise the read-process as we have thousands of markdown files
92
+ _readLine . on ( 'close' , ( ) => {
93
+ const frontMatterData = getFrontMatter ( filename , rawFrontmatter ) ;
94
+
95
+ frontMatterData . categories . forEach ( category => {
96
+ // we add the category to the categories set
97
+ blogCategories . add ( category ) ;
98
+ } ) ;
99
+
100
+ resolve ( frontMatterData ) ;
101
+ } ) ;
102
+ } )
103
+ )
104
+ ) ;
105
+
106
+ return { categories : [ ...blogCategories ] , posts } ;
110
107
} ;
111
108
112
109
export default generateBlogData ;
0 commit comments