feat(nx-dev): update workspace conformance rule to check md files in general

This commit is contained in:
Juri 2025-02-24 21:12:28 +01:00 committed by Juri Strumpflohner
parent c698b1ef9c
commit d428eec060
2 changed files with 26 additions and 20 deletions

View File

@ -253,7 +253,7 @@
"rule": "@nx/workspace-plugin/conformance-rules/blog-description", "rule": "@nx/workspace-plugin/conformance-rules/blog-description",
"projects": ["docs"], "projects": ["docs"],
"options": { "options": {
"mdGlobPattern": "blog/**/*.md" "mdGlobPattern": "{blog,shared}/**/*.md"
} }
}, },
{ {

View File

@ -12,7 +12,7 @@ export default createConformanceRule<{ mdGlobPattern: string }>({
name: 'blog-description', name: 'blog-description',
category: 'consistency', category: 'consistency',
description: description:
'Ensures that blog posts have a description in their frontmatter', 'Ensures that markdown documentation files have a description in their frontmatter',
reporter: 'project-files-reporter', reporter: 'project-files-reporter',
implementation: async ({ projectGraph, ruleOptions }) => { implementation: async ({ projectGraph, ruleOptions }) => {
const violations: ProjectFilesViolation[] = []; const violations: ProjectFilesViolation[] = [];
@ -43,26 +43,32 @@ export default createConformanceRule<{ mdGlobPattern: string }>({
const content = readFileSync(file, 'utf-8'); const content = readFileSync(file, 'utf-8');
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/); const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
// Only check files with frontmatter if (!frontmatterMatch) {
if (frontmatterMatch) { violations.push({
try { message: 'Markdown documentation files must have frontmatter',
const frontmatter = yamlLoad(frontmatterMatch[1]) as Record< sourceProject: docsProject.name,
string, file: file,
unknown });
>; continue;
}
if (!frontmatter.description) { try {
violations.push({ const frontmatter = yamlLoad(frontmatterMatch[1]) as Record<
message: string,
'Blog posts with frontmatter must have a description field', unknown
sourceProject: docsProject.name, >;
file: file,
}); if (!frontmatter.description) {
} violations.push({
} catch (e) { message:
// If YAML parsing fails, we skip the file 'Markdown documentation files must have a description field in their frontmatter',
continue; sourceProject: docsProject.name,
file: file,
});
} }
} catch (e) {
// If YAML parsing fails, we skip the file
continue;
} }
} }