-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nx-dev): Add post build script for blog og images
- Loading branch information
1 parent
55dc25f
commit b6956e2
Showing
3 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { workspaceRoot } from '@nx/devkit'; | ||
import { Node, parse, Tokenizer } from '@markdoc/markdoc'; | ||
import { load as yamlLoad } from '@zkochan/js-yaml'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as chalk from 'chalk'; | ||
|
||
const tokenizer = new Tokenizer({ | ||
allowComments: true, | ||
}); | ||
|
||
const parseMarkdown: (markdown: string) => Node = (markdown) => { | ||
const tokens = tokenizer.tokenize(markdown); | ||
return parse(tokens); | ||
}; | ||
|
||
export const extractFrontmatter = ( | ||
documentContent: string | ||
): Record<string, any> => { | ||
const ast = parseMarkdown(documentContent); | ||
const frontmatter = ast.attributes['frontmatter'] | ||
? (yamlLoad(ast.attributes['frontmatter']) as Record<string, any>) | ||
: {}; | ||
return frontmatter; | ||
}; | ||
|
||
function checkImageExistsSync(imagePath: string): boolean { | ||
try { | ||
fs.accessSync(imagePath, fs.constants.F_OK); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
console.log(`\n🔍 Checking blog OG images...\n`); | ||
|
||
// Main | ||
const blogRootPath = path.join(workspaceRoot, 'docs', 'blog'); | ||
|
||
const blogFiles: string[] = fs.readdirSync(blogRootPath); | ||
const errors: string[] = []; | ||
const allowedExtensions = ['.png', '.webp', '.jpg', '.jpeg']; | ||
|
||
for (const blogFile of blogFiles) { | ||
const blogFilePath = path.join(blogRootPath, blogFile); | ||
if (!blogFile.endsWith('.md')) { | ||
continue; | ||
} | ||
const blogContent = fs.readFileSync(blogFilePath, 'utf8'); | ||
const frontmatter = extractFrontmatter(blogContent); | ||
|
||
if (!frontmatter.cover_image) { | ||
console.warn( | ||
chalk.yellow( | ||
`⚠ Blog post: ${frontmatter.title} does not have a cover image\n` | ||
) | ||
); | ||
continue; | ||
} | ||
const coverImagePath = path.join( | ||
workspaceRoot, | ||
'docs', | ||
frontmatter.cover_image | ||
); | ||
const { ext, name, dir } = path.parse(coverImagePath); | ||
if (!checkImageExistsSync(coverImagePath)) { | ||
errors.push( | ||
`Cover image for blog post: ${frontmatter.title} does not exist at ${coverImagePath}` | ||
); | ||
} else if (!allowedExtensions.includes(ext)) { | ||
const hasAllowedImageType = allowedExtensions.some((allowedExt) => { | ||
const ogImagePath = `${path.resolve( | ||
workspaceRoot, | ||
path.join(dir, name) | ||
)}${allowedExt}`; | ||
return checkImageExistsSync(ogImagePath); | ||
}); | ||
if (!hasAllowedImageType) { | ||
errors.push( | ||
`OG image for blog post: ${ | ||
frontmatter.title | ||
} does not have a valid extension. Allowed extensions are: ${allowedExtensions.join( | ||
',' | ||
)}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (errors.length > 0) { | ||
console.error(chalk.red(`✖ Blog OG Images errors:\n`)); | ||
errors.forEach((error) => console.error(chalk.red(error))); | ||
process.exit(1); | ||
} | ||
|
||
console.log(chalk.green(`\n✔ Blog OG Images are valid`)); | ||
process.exit(0); |