(filePath)
| 106 | |
| 107 | // Function to process a single .mdx/.md file |
| 108 | async function processMdxFile(filePath) { |
| 109 | const fileContent = fs.readFileSync(filePath, "utf8"); |
| 110 | const { data, content } = matter(fileContent); |
| 111 | |
| 112 | // Determine card text: Use 'title' if available; otherwise, fallback to 'description' |
| 113 | const cardText = data.title || data.description; |
| 114 | |
| 115 | // Skip file if neither found |
| 116 | if (!cardText) { |
| 117 | console.log( |
| 118 | `Skipping ${filePath} as it lacks both 'title' and 'description'.`, |
| 119 | ); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | const sanitizedSlug = sanitizeSlug(cardText); |
| 124 | const outputPath = path.join(imgPath, `${sanitizedSlug}.jpg`); |
| 125 | |
| 126 | // Generate social card using the selected text. |
| 127 | await generateSocialCard(cardText, outputPath); |
| 128 | |
| 129 | // Update the file's front matter to include the generated image path |
| 130 | const updatedFrontMatter = { |
| 131 | ...data, |
| 132 | image: `/img/socialCards/${sanitizedSlug}.jpg`, |
| 133 | }; |
| 134 | const updatedContent = matter.stringify(content, updatedFrontMatter); |
| 135 | |
| 136 | fs.writeFileSync(filePath, updatedContent); |
| 137 | console.log(`Updated ${filePath} with social card image.`); |
| 138 | } |
| 139 | |
| 140 | // Recursive function to find and process all MDX files |
| 141 | function processDirectory(directory) { |
no test coverage detected