1- // TODO
21'use strict' ;
32
43import { createReadStream } from 'node:fs' ;
5- import { basename , extname , join } from 'node:path' ;
4+ import { basename , join } from 'node:path' ;
65import readline from 'node:readline' ;
76
87import graymatter from 'gray-matter' ;
98
10- import { getMarkdownFiles } from '#site/next.helpers.mjs' ;
9+ import {
10+ DEFAULT_LOCALE_ROOT ,
11+ getAllPages ,
12+ pathToRoute ,
13+ } from '#site/util/router.mjs' ;
1114
1215// gets the current blog path based on local module path
13- const blogPath = join ( process . cwd ( ) , 'pages/en/ blog' ) ;
16+ const blogPath = join ( DEFAULT_LOCALE_ROOT , 'blog' ) ;
1417
1518/**
1619 * This method parses the source (raw) Markdown content into Frontmatter
1720 * and returns basic information for blog posts
1821 *
19- * @param {string } filename the filename related to the blogpost
22+ * @param {string } route the filename related to the blogpost
2023 * @param {string } source the source markdown content of the blog post
2124 */
22- const getFrontMatter = ( filename , source ) => {
25+ const getFrontMatter = ( route , source ) => {
2326 const {
2427 title = 'Untitled' ,
2528 author = 'The Node.js Project' ,
@@ -36,7 +39,7 @@ const getFrontMatter = (filename, source) => {
3639 const categories = [ category , `year-${ publishYear } ` , 'all' ] ;
3740
3841 // this is the url used for the blog post it based on the category and filename
39- const slug = `/blog/${ category } /${ basename ( filename , extname ( filename ) ) } ` ;
42+ const slug = `/blog/${ category } /${ basename ( route ) } ` ;
4043
4144 return {
4245 title,
@@ -56,59 +59,64 @@ const getFrontMatter = (filename, source) => {
5659 */
5760const generateBlogData = async ( ) => {
5861 // We retrieve the full pathnames of all Blog Posts to read each file individually
59- const filenames = await getMarkdownFiles ( process . cwd ( ) , 'pages/en/blog' , [
60- '**/index.md' ,
61- ] ) ;
62+ const filenames = await getAllPages ( {
63+ cwd : blogPath ,
64+ } ) ;
6265
6366 /**
6467 * This contains the metadata of all available blog categories
6568 */
6669 const blogCategories = new Set ( [ 'all' ] ) ;
6770
6871 const posts = await Promise . all (
69- filenames . map (
70- filename =>
71- new Promise ( resolve => {
72- // We create a stream for reading a file instead of reading the files
73- const _stream = createReadStream ( join ( blogPath , filename ) ) ;
74-
75- // We create a readline interface to read the file line-by-line
76- const _readLine = readline . createInterface ( { input : _stream } ) ;
77-
78- let rawFrontmatter = '' ;
79- let frontmatterSeparatorsEncountered = 0 ;
80-
81- // We read line by line
82- _readLine . on ( 'line' , line => {
83- rawFrontmatter += `${ line } \n` ;
84-
85- // We observe the frontmatter separators
86- if ( line === '---' ) {
87- frontmatterSeparatorsEncountered ++ ;
88- }
89-
90- // Once we have two separators we close the readLine and the stream
91- if ( frontmatterSeparatorsEncountered === 2 ) {
92- _readLine . close ( ) ;
93- _stream . close ( ) ;
94- }
95- } ) ;
96-
97- // Then we parse gray-matter on the frontmatter
98- // This allows us to only read the frontmatter part of each file
99- // and optimise the read-process as we have thousands of markdown files
100- _readLine . on ( 'close' , ( ) => {
101- const frontMatterData = getFrontMatter ( filename , rawFrontmatter ) ;
102-
103- frontMatterData . categories . forEach ( category => {
104- // we add the category to the categories set
105- blogCategories . add ( category ) ;
72+ filenames
73+ . filter ( n => n !== 'page.md' )
74+ . map (
75+ filename =>
76+ new Promise ( resolve => {
77+ // We create a stream for reading a file instead of reading the files
78+ const _stream = createReadStream ( join ( blogPath , filename ) ) ;
79+
80+ // We create a readline interface to read the file line-by-line
81+ const _readLine = readline . createInterface ( { input : _stream } ) ;
82+
83+ let rawFrontmatter = '' ;
84+ let frontmatterSeparatorsEncountered = 0 ;
85+
86+ // We read line by line
87+ _readLine . on ( 'line' , line => {
88+ rawFrontmatter += `${ line } \n` ;
89+
90+ // We observe the frontmatter separators
91+ if ( line === '---' ) {
92+ frontmatterSeparatorsEncountered ++ ;
93+ }
94+
95+ // Once we have two separators we close the readLine and the stream
96+ if ( frontmatterSeparatorsEncountered === 2 ) {
97+ _readLine . close ( ) ;
98+ _stream . close ( ) ;
99+ }
106100 } ) ;
107101
108- resolve ( frontMatterData ) ;
109- } ) ;
110- } )
111- )
102+ // Then we parse gray-matter on the frontmatter
103+ // This allows us to only read the frontmatter part of each file
104+ // and optimise the read-process as we have thousands of markdown files
105+ _readLine . on ( 'close' , ( ) => {
106+ const frontMatterData = getFrontMatter (
107+ pathToRoute ( filename ) ,
108+ rawFrontmatter
109+ ) ;
110+
111+ frontMatterData . categories . forEach ( category => {
112+ // we add the category to the categories set
113+ blogCategories . add ( category ) ;
114+ } ) ;
115+
116+ resolve ( frontMatterData ) ;
117+ } ) ;
118+ } )
119+ )
112120 ) ;
113121
114122 return {
0 commit comments