-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate title in mdx-frontmatter script #221
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ let totalFilesChecked = 0; | |
let totalFilesValid = 0; | ||
let totalFilesInvalid = 0; | ||
|
||
// List of validators to run | ||
const validators = [checkDescriptionLength, checkTitleLength]; | ||
|
||
// List of folders to exclude (relative to mdxDir) | ||
const excludedFolders = ["-ARCHIVE-", "api-reference", "llm-university"]; | ||
|
||
|
@@ -31,30 +34,53 @@ async function shouldExcludeFile(filePath) { | |
} | ||
|
||
async function checkDescriptionLength(filePath) { | ||
totalFilesChecked++; | ||
const fileContent = await fs.readFile(filePath, "utf8"); | ||
const { data } = matter(fileContent); | ||
const minDescriptionLength = 50; | ||
const maxDescriptionLength = 160; | ||
|
||
if (!data.description) { | ||
console.log(`File "${filePath}" is missing a description.`); | ||
totalFilesInvalid++; | ||
return false; | ||
} | ||
|
||
const descriptionLength = data.description.length; | ||
|
||
if (descriptionLength < 50 || descriptionLength > 160) { | ||
if (descriptionLength < minDescriptionLength || descriptionLength > maxDescriptionLength) { | ||
console.log( | ||
`File "${filePath}" has an invalid description length: ${descriptionLength} characters.` | ||
`File "${filePath}" has an invalid description length: ${descriptionLength} characters. ` + | ||
`Description should be between ${minDescriptionLength}-${maxDescriptionLength} characters.` | ||
); | ||
totalFilesInvalid++; | ||
return false; | ||
} | ||
|
||
totalFilesValid++; | ||
return true; | ||
} | ||
|
||
|
||
async function checkTitleLength(filePath) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue is that many titles need to be validated. I think it would be a good idea to discuss the changes introduced in this PR with Trent to determine if he is comfortable merging such a significant modification. After the merge, all content changes will be blocked if the descriptions are invalid. |
||
const fileContent = await fs.readFile(filePath, "utf8"); | ||
const { data } = matter(fileContent); | ||
const minTitleLength = 30; | ||
const maxTitleLength = 60; | ||
|
||
if (!data.title) { | ||
console.log(`File "${filePath}" is missing a title.`); | ||
return false; | ||
} | ||
|
||
const titleLength = data.title.length; | ||
if (titleLength < minTitleLength || titleLength > maxTitleLength) { | ||
console.log( | ||
`File "${filePath}" has an invalid title length: ${titleLength} characters. ` + | ||
`Title should be between ${minTitleLength}-${maxTitleLength} characters.` | ||
); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
async function checkMDXFiles(dirPath) { | ||
let allFilesValid = true; | ||
const files = await fs.readdir(dirPath); | ||
|
@@ -77,9 +103,22 @@ async function checkMDXFiles(dirPath) { | |
console.log(`Skipping excluded file: ${fullPath}`); | ||
continue; | ||
} | ||
const isValid = await checkDescriptionLength(fullPath); | ||
let isValid = true; | ||
|
||
for (const validate of validators) { | ||
const fileIsValid = await validate(fullPath); | ||
if (!fileIsValid) { | ||
isValid = false; | ||
} | ||
} | ||
|
||
totalFilesChecked++; | ||
if (!isValid) { | ||
allFilesValid = false; | ||
totalFilesInvalid++; | ||
} | ||
else { | ||
totalFilesValid++; | ||
} | ||
} | ||
} | ||
|
@@ -98,12 +137,12 @@ async function checkMDXFiles(dirPath) { | |
|
||
if (!allFilesValid) { | ||
console.error( | ||
"Some files have invalid or missing descriptions. Meta description needing to be 50-160 characters" | ||
"Some files have invalid or missing content." | ||
); | ||
process.exit(1); // Fail if any file is invalid | ||
} else { | ||
console.log( | ||
"All files have a valid description length in the frontmatter." | ||
"All files a valid for frontmatter." | ||
); | ||
} | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to create array with validators so we can easity estend the script by creating simple independent functions and add them to the array.