Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions .github/scripts/check-mdx-frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ let totalFilesChecked = 0;
let totalFilesValid = 0;
let totalFilesInvalid = 0;

// List of validators to run
const validators = [checkDescriptionLength, checkTitleLength];
Copy link
Collaborator Author

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.


// List of folders to exclude (relative to mdxDir)
const excludedFolders = ["-ARCHIVE-", "api-reference", "llm-university"];

Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
If he is not ready to change everything immediately, I would modify the code to trough the warning with the link to the invalid file.

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);
Expand All @@ -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++;
}
}
}
Expand All @@ -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."
);
}
})();
Loading