1
1
// scripts/collectOgData.js
2
- const { join, dirname, basename } = require ( 'path' ) ; // Ensure basename is imported
2
+ const { join, dirname, basename } = require ( 'path' ) ;
3
3
const { readdir, readFile, stat, mkdir, writeFile } = require ( 'fs/promises' ) ;
4
4
const toml = require ( 'toml' ) ;
5
5
const mimeTypes = require ( 'mime-types' ) ;
@@ -49,19 +49,6 @@ async function ensureDir(dirPath) {
49
49
catch ( err ) { if ( err . code !== 'EEXIST' ) throw err ; debugLog ( `Directory already exists: ${ dirPath } ` ) ; }
50
50
}
51
51
52
- // slugify is kept in case it's needed elsewhere, but not for OG image name from content pages
53
- function slugify ( text ) {
54
- if ( ! text ) return 'untitled' ;
55
- return text
56
- . toString ( )
57
- . toLowerCase ( )
58
- . replace ( / \s + / g, '-' )
59
- . replace ( / [ ^ \w - ] + / g, '' )
60
- . replace ( / - - + / g, '-' )
61
- . replace ( / ^ - + / , '' )
62
- . replace ( / - + $ / , '' ) ;
63
- }
64
-
65
52
async function findMarkdownFiles ( dir ) {
66
53
let entries ;
67
54
try { entries = await readdir ( dir ) ; } catch ( err ) { console . warn ( `Could not read directory ${ dir } : ${ err . message } ` ) ; return [ ] ; }
@@ -89,7 +76,6 @@ function extractFrontMatter(content) {
89
76
const excludeMatch = fm . match ( / ^ e x c l u d e _ s i t e m a p : \s * ( t r u e ) \s * $ / im) ;
90
77
const imageMatch = fm . match ( / ^ (?: i m a g e | I m a g e ) : \s * " ? ( [ ^ " # \n ] + ) " ? \s * $ / m) ;
91
78
92
-
93
79
if ( titleMatch ) title = titleMatch [ 1 ] . split ( '#' ) [ 0 ] . trim ( ) . replace ( / ^ [ ' " ] | [ ' " ] $ / g, '' ) . replace ( / \\ " / g, '"' ) . replace ( / \\ ' / g, "'" ) . trim ( ) ;
94
80
if ( descMatch ) {
95
81
let raw = descMatch [ 1 ] . replace ( / \\ " / g, '"' ) . replace ( / \\ ' / g, "'" ) ;
@@ -125,26 +111,18 @@ async function getHugoSiteConfig() {
125
111
let paramsConfig = { } ;
126
112
127
113
try {
128
- debugLog ( `Reading Hugo config from: ${ HUGO_CONFIG_PATH } ` ) ;
129
114
if ( await pathExists ( HUGO_CONFIG_PATH ) ) {
130
115
const hugoContent = await readFile ( HUGO_CONFIG_PATH , 'utf8' ) ;
131
116
siteConfig = toml . parse ( hugoContent ) ;
132
- debugLog ( `Hugo config parsed.` ) ;
133
- } else {
134
- console . warn ( `⚠️ Hugo config file not found at ${ HUGO_CONFIG_PATH } ` ) ;
135
117
}
136
118
} catch ( err ) {
137
119
console . error ( `❌ Error parsing ${ HUGO_CONFIG_PATH } : ${ err . message } ` ) ;
138
120
}
139
121
140
122
try {
141
- debugLog ( `Reading params config from: ${ PARAMS_CONFIG_PATH } ` ) ;
142
123
if ( await pathExists ( PARAMS_CONFIG_PATH ) ) {
143
124
const paramsContent = await readFile ( PARAMS_CONFIG_PATH , 'utf8' ) ;
144
125
paramsConfig = toml . parse ( paramsContent ) ;
145
- debugLog ( `Params config parsed.` ) ;
146
- } else {
147
- console . warn ( `⚠️ Params config file not found at ${ PARAMS_CONFIG_PATH } ` ) ;
148
126
}
149
127
} catch ( err ) {
150
128
console . error ( `❌ Error parsing ${ PARAMS_CONFIG_PATH } : ${ err . message } ` ) ;
@@ -161,35 +139,19 @@ async function getHugoSiteConfig() {
161
139
async function getPaletteFromScss ( ) {
162
140
const palette = { ...DEFAULT_COLORS } ;
163
141
try {
164
- debugLog ( `Attempting to read SCSS variables from: ${ CUSTOM_SCSS_PATH } ` ) ;
165
142
if ( ! ( await pathExists ( CUSTOM_SCSS_PATH ) ) ) {
166
143
console . warn ( `⚠️ Custom SCSS file not found at ${ CUSTOM_SCSS_PATH } . Using default colors.` ) ;
167
144
return palette ;
168
145
}
169
146
const scssContent = await readFile ( CUSTOM_SCSS_PATH , 'utf8' ) ;
170
147
171
- let foundColorsCount = 0 ;
172
148
CSS_VARS_TO_EXTRACT . forEach ( cssVarName => {
173
149
const regex = new RegExp ( `--${ cssVarName } \\s*:\\s*([^;]+);` , 'm' ) ;
174
150
const match = scssContent . match ( regex ) ;
175
151
if ( match && match [ 1 ] ) {
176
- const colorValue = match [ 1 ] . trim ( ) ;
177
- if ( palette [ cssVarName ] !== colorValue || ! DEFAULT_COLORS . hasOwnProperty ( cssVarName ) ) {
178
- if ( palette [ cssVarName ] !== colorValue && DEFAULT_COLORS . hasOwnProperty ( cssVarName ) ) {
179
- debugLog ( `Updated CSS variable --${ cssVarName } from SCSS: ${ colorValue } (was ${ palette [ cssVarName ] } )` ) ;
180
- } else if ( ! DEFAULT_COLORS . hasOwnProperty ( cssVarName ) ) {
181
- debugLog ( `Found new CSS variable --${ cssVarName } from SCSS: ${ colorValue } ` ) ;
182
- }
183
- foundColorsCount ++ ;
184
- } else {
185
- debugLog ( `CSS variable --${ cssVarName } found in SCSS but value (${ colorValue } ) is same as default.` ) ;
186
- }
187
- palette [ cssVarName ] = colorValue ;
188
- } else {
189
- debugLog ( `CSS variable --${ cssVarName } not found or not in expected format in ${ CUSTOM_SCSS_PATH } . Will use default: ${ palette [ cssVarName ] } ` ) ;
152
+ palette [ cssVarName ] = match [ 1 ] . trim ( ) ;
190
153
}
191
154
} ) ;
192
- console . log ( `🎨 Extracted/Updated ${ foundColorsCount } colors from SCSS. Total in palette: ${ Object . keys ( palette ) . length } ` ) ;
193
155
return palette ;
194
156
} catch ( err ) {
195
157
console . error ( `❌ Error reading or parsing SCSS file ${ CUSTOM_SCSS_PATH } : ${ err . message } ` ) ;
@@ -204,42 +166,35 @@ async function collectData() {
204
166
const hugoConfig = await getHugoSiteConfig ( ) ;
205
167
const colorPalette = await getPaletteFromScss ( ) ;
206
168
207
- let absoluteLogoPath = '' ;
208
- if ( hugoConfig . logoPath ) {
209
- debugLog ( `Logo path from config: ${ hugoConfig . logoPath } ` ) ;
210
- if ( hugoConfig . logoPath . startsWith ( '/' ) ) {
211
- absoluteLogoPath = join ( STATIC_DIR , hugoConfig . logoPath . substring ( 1 ) ) ;
212
- debugLog ( `Trying static logo path (absolute): ${ absoluteLogoPath } ` ) ;
213
- } else {
169
+ // --- MODIFIED LOGO LOGIC ---
170
+ // Explicitly use assets/images/ONM-logo.png as the primary logo for OG images.
171
+ let absoluteLogoPath = join ( ASSETS_DIR , 'images' , 'ONM-logo.png' ) ;
172
+
173
+ if ( ! ( await pathExists ( absoluteLogoPath ) ) ) {
174
+ console . warn ( `⚠️ Preferred OG logo not found at ${ absoluteLogoPath } . Falling back to config logo.` ) ;
175
+ // Fallback to the logo from hugo config if the primary one is missing
176
+ if ( hugoConfig . logoPath ) {
214
177
const pathInAssets = join ( ASSETS_DIR , hugoConfig . logoPath ) ;
215
178
const pathInStatic = join ( STATIC_DIR , hugoConfig . logoPath ) ;
216
-
217
179
if ( await pathExists ( pathInAssets ) ) {
218
180
absoluteLogoPath = pathInAssets ;
219
- debugLog ( `Found logo in assets: ${ absoluteLogoPath } ` ) ;
220
181
} else if ( await pathExists ( pathInStatic ) ) {
221
182
absoluteLogoPath = pathInStatic ;
222
- debugLog ( `Found logo in static: ${ absoluteLogoPath } ` ) ;
223
183
} else {
224
- console . warn ( `⚠️ Logo path specified (${ hugoConfig . logoPath } ) but file not found in assets or static directory.` ) ;
225
184
absoluteLogoPath = '' ;
226
185
}
186
+ } else {
187
+ absoluteLogoPath = '' ;
227
188
}
228
- } else {
229
- console . warn ( `⚠️ No logo path specified in params.toml.` ) ;
230
189
}
231
-
190
+ // --- END MODIFIED LOGO LOGIC ---
191
+
232
192
const logoDataUri = await getImageDataUri ( absoluteLogoPath ) ;
233
193
if ( ! logoDataUri ) {
234
- if ( hugoConfig . logoPath && absoluteLogoPath ) {
235
- console . warn ( `⚠️ Logo data URI could not be generated. Attempted path: ${ absoluteLogoPath } ` ) ;
236
- } else if ( ! hugoConfig . logoPath ) {
237
- console . warn ( `⚠️ No logo path in config, so no logo data URI generated.` ) ;
238
- } else {
239
- console . warn ( `⚠️ Logo path ${ hugoConfig . logoPath } did not resolve to an existing file, no logo data URI generated.` ) ;
240
- }
194
+ console . error ( "❌ Could not generate a data URI for the logo. OG images will not have a logo." ) ;
241
195
}
242
196
197
+
243
198
const outputData = {
244
199
siteNameForDisplay : hugoConfig . siteName ,
245
200
logoDataUri : logoDataUri ,
@@ -250,26 +205,23 @@ async function collectData() {
250
205
// 1. Add Homepage Data
251
206
console . log ( '🏠 Processing Homepage...' ) ;
252
207
if ( hugoConfig . siteName && hugoConfig . siteDescription ) {
253
- const homepageOgDir = join ( STATIC_DIR , 'images' ) ; // OG images for homepage go to static/images
208
+ const homepageOgDir = join ( STATIC_DIR , 'images' ) ;
254
209
await ensureDir ( homepageOgDir ) ;
255
210
outputData . pages . push ( {
256
211
type : 'homepage' ,
257
212
title : hugoConfig . siteName ,
258
213
description : hugoConfig . siteDescription ,
259
- outputPath : join ( homepageOgDir , `og-image.${ OUTPUT_FORMAT } ` ) , // Consistent name for homepage OG
214
+ outputPath : join ( homepageOgDir , `og-image.${ OUTPUT_FORMAT } ` ) ,
260
215
tempHtmlPath : join ( TMP_DIR , `homepage-temp-og.html` ) ,
261
- pageSpecificLogoUri : null // Homepage uses the global logo
216
+ pageSpecificLogoUri : null
262
217
} ) ;
263
218
debugLog ( 'Added homepage data.' ) ;
264
- } else {
265
- console . warn ( '⚠️ Missing siteName or siteDescription for homepage OG image.' ) ;
266
219
}
267
220
268
221
// 2. Process Content Pages
269
222
console . log ( '📄 Processing content pages...' ) ;
270
223
const markdownFiles = await findMarkdownFiles ( CONTENT_ROOT_DIR ) ;
271
- let processedCount = 0 ;
272
- let skippedCount = 0 ;
224
+ let processedCount = 0 , skippedCount = 0 ;
273
225
274
226
for ( const mdFile of markdownFiles ) {
275
227
const pageDirectory = dirname ( mdFile ) ;
@@ -280,53 +232,39 @@ async function collectData() {
280
232
const { title, description, isDraft, excludeSitemap, image : pageImage } = extractFrontMatter ( mdContent ) ;
281
233
282
234
if ( isDraft || excludeSitemap || ! title || ! description ) {
283
- debugLog ( `Skipping ${ relativeMdPath } (Draft: ${ isDraft } , Exclude: ${ excludeSitemap } , NoTitle: ${ ! title } , NoDesc: ${ ! description } )` ) ;
284
235
skippedCount ++ ;
285
236
continue ;
286
237
}
287
238
288
239
let pageSpecificLogoUri = null ;
289
240
if ( pageImage ) {
290
241
let imageFullPath = join ( pageDirectory , pageImage ) ; // Check in bundle first
291
- if ( ! ( await pathExists ( imageFullPath ) ) ) {
242
+ if ( ! ( await pathExists ( imageFullPath ) ) ) {
292
243
const staticImageCand = join ( STATIC_DIR , pageImage . startsWith ( '/' ) ? pageImage . substring ( 1 ) : pageImage ) ;
293
- const assetImageCand = join ( ASSETS_DIR , pageImage . startsWith ( '/' ) ? pageImage . substring ( 1 ) : pageImage ) ;
294
- if ( await pathExists ( staticImageCand ) ) {
244
+ if ( await pathExists ( staticImageCand ) ) {
295
245
imageFullPath = staticImageCand ;
296
- debugLog ( `Found page image in static: ${ imageFullPath } ` ) ;
297
- } else if ( await pathExists ( assetImageCand ) ) {
298
- imageFullPath = assetImageCand ;
299
- debugLog ( `Found page image in assets: ${ imageFullPath } ` ) ;
300
246
} else {
301
- debugLog ( `Page specific image ${ pageImage } for ${ relativeMdPath } not found in bundle, static, or assets. Will use global logo if available, or no logo.` ) ;
302
247
imageFullPath = null ;
303
248
}
304
- } else {
305
- debugLog ( `Found page image in bundle: ${ imageFullPath } ` ) ;
306
249
}
307
-
308
- if ( imageFullPath ) {
250
+ if ( imageFullPath ) {
309
251
pageSpecificLogoUri = await getImageDataUri ( imageFullPath ) ;
310
- if ( pageSpecificLogoUri ) debugLog ( `Using page specific image for ${ relativeMdPath } : ${ pageImage } ` ) ;
311
- else debugLog ( `Failed to get Data URI for page image: ${ imageFullPath } ` ) ;
312
252
}
313
253
}
314
254
315
- // MODIFICATION: Use parent directory name as the slug for OG image
316
255
const parentDirName = basename ( pageDirectory ) ;
317
- const ogImageFilename = `${ parentDirName } -og.${ OUTPUT_FORMAT } ` ; // Use parentDirName as the slug
256
+ const ogImageFilename = `${ parentDirName } -og.${ OUTPUT_FORMAT } ` ;
318
257
319
258
outputData . pages . push ( {
320
259
type : 'content' ,
321
260
sourceMdPath : mdFile ,
322
261
title : title ,
323
262
description : description ,
324
- outputPath : join ( pageDirectory , ogImageFilename ) , // Image saved in the page's bundle
325
- tempHtmlPath : join ( TMP_DIR , `${ parentDirName } -${ Date . now ( ) } -temp-og.html` ) , // Temp HTML uses parentDirName
263
+ outputPath : join ( pageDirectory , ogImageFilename ) ,
264
+ tempHtmlPath : join ( TMP_DIR , `${ parentDirName } -${ Date . now ( ) } -temp-og.html` ) ,
326
265
pageSpecificLogoUri : pageSpecificLogoUri
327
266
} ) ;
328
267
processedCount ++ ;
329
- debugLog ( `Added data for: ${ relativeMdPath } , OG image: ${ ogImageFilename } (slug from dir: ${ parentDirName } )` ) ;
330
268
331
269
} catch ( err ) {
332
270
console . error ( `❌ Error processing ${ relativeMdPath } : ${ err . message } ` ) ;
@@ -336,17 +274,11 @@ async function collectData() {
336
274
337
275
// 3. Write JSON Output
338
276
console . log ( `💾 Writing data for ${ outputData . pages . length } pages to ${ OUTPUT_JSON_PATH } ...` ) ;
339
- try {
340
- await writeFile ( OUTPUT_JSON_PATH , JSON . stringify ( outputData , null , 2 ) ) ;
341
- console . log ( '✅ Data collection complete.' ) ;
342
- console . log ( `📊 Processed ${ processedCount } content pages, skipped ${ skippedCount } .` ) ;
343
- } catch ( err ) {
344
- console . error ( `❌ Error writing JSON output: ${ err . message } ` ) ;
345
- process . exit ( 1 ) ;
346
- }
277
+ await writeFile ( OUTPUT_JSON_PATH , JSON . stringify ( outputData , null , 2 ) ) ;
278
+ console . log ( '✅ Data collection complete.' ) ;
279
+ console . log ( `📊 Processed ${ processedCount } content pages, skipped ${ skippedCount } .` ) ;
347
280
}
348
281
349
- // --- Execution ---
350
282
( async ( ) => {
351
283
try {
352
284
await collectData ( ) ;
0 commit comments