-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Check MDX frontmatter * set up secret * test .mdx * updated descriptions and new script * one more mdx * mdxDir * test * added promises * long descriptions generated based on descriptions not the articles * removed description genetarion * limit changed * limit changed * Update .github/scripts/check-mdx-frontmatter.js Co-authored-by: Shubham Shukla <[email protected]> Signed-off-by: Kate Sv <[email protected]> --------- Signed-off-by: Kate Sv <[email protected]> Co-authored-by: Shubham Shukla <[email protected]>
- Loading branch information
1 parent
c822e9c
commit 9acfe5c
Showing
5 changed files
with
218 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const fs = require("fs").promises; | ||
const path = require("path"); | ||
const matter = require("gray-matter"); | ||
|
||
const mdxDir = path.join(__dirname, "../../fern/pages"); | ||
const filePattern = /\.mdx$/; | ||
|
||
// Counters | ||
let totalFilesChecked = 0; | ||
let totalFilesValid = 0; | ||
let totalFilesInvalid = 0; | ||
|
||
// List of folders to exclude (relative to mdxDir) | ||
const excludedFolders = ["-ARCHIVE-", "api-reference", "llm-university"]; | ||
|
||
function shouldExcludeFolder(dirPath) { | ||
return excludedFolders.some((excludedFolder) => { | ||
return path.relative(mdxDir, dirPath).startsWith(excludedFolder); | ||
}); | ||
} | ||
|
||
async function shouldExcludeFile(filePath) { | ||
try { | ||
const fileContent = await fs.readFile(filePath, "utf8"); | ||
const { data } = matter(fileContent); | ||
return data.hidden === true; | ||
} catch (error) { | ||
console.error(`Error reading file "${filePath}":`, error); | ||
return false; // In case of error, don't exclude the file | ||
} | ||
} | ||
|
||
async function checkDescriptionLength(filePath) { | ||
totalFilesChecked++; | ||
const fileContent = await fs.readFile(filePath, "utf8"); | ||
const { data } = matter(fileContent); | ||
|
||
if (!data.description) { | ||
console.log(`File "${filePath}" is missing a description.`); | ||
totalFilesInvalid++; | ||
return false; | ||
} | ||
|
||
const descriptionLength = data.description.length; | ||
|
||
if (descriptionLength < 50 || descriptionLength > 160) { | ||
console.log( | ||
`File "${filePath}" has an invalid description length: ${descriptionLength} characters.` | ||
); | ||
totalFilesInvalid++; | ||
return false; | ||
} | ||
|
||
totalFilesValid++; | ||
return true; | ||
} | ||
|
||
async function checkMDXFiles(dirPath) { | ||
let allFilesValid = true; | ||
const files = await fs.readdir(dirPath); | ||
|
||
for (const file of files) { | ||
const fullPath = path.join(dirPath, file); | ||
const stat = await fs.lstat(fullPath); | ||
|
||
if (stat.isDirectory()) { | ||
if (shouldExcludeFolder(fullPath)) { | ||
console.log(`Skipping excluded directory: ${fullPath}`); | ||
continue; | ||
} | ||
const isValid = await checkMDXFiles(fullPath); | ||
if (!isValid) { | ||
allFilesValid = false; | ||
} | ||
} else if (filePattern.test(file)) { | ||
if (await shouldExcludeFile(fullPath)) { | ||
console.log(`Skipping excluded file: ${fullPath}`); | ||
continue; | ||
} | ||
const isValid = await checkDescriptionLength(fullPath); | ||
if (!isValid) { | ||
allFilesValid = false; | ||
} | ||
} | ||
} | ||
|
||
return allFilesValid; | ||
} | ||
|
||
(async () => { | ||
const allFilesValid = await checkMDXFiles(mdxDir); | ||
|
||
// Summary report | ||
console.log(`\nSummary Report:`); | ||
console.log(`Total files checked: ${totalFilesChecked}`); | ||
console.log(`Total valid files: ${totalFilesValid}`); | ||
console.log(`Total invalid files: ${totalFilesInvalid}`); | ||
|
||
if (!allFilesValid) { | ||
console.error( | ||
"Some files have invalid or missing descriptions. Meta description needing to be 50-160 characters" | ||
); | ||
process.exit(1); // Fail if any file is invalid | ||
} else { | ||
console.log( | ||
"All files have a valid description length in the frontmatter." | ||
); | ||
} | ||
})(); |
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,29 @@ | ||
name: check-mdx-frontmatter | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- 'fern/pages/**/*.mdx' | ||
- 'fern/pages/**/**/*.mdx' | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
permissions: write-all | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup pnpm | ||
uses: pnpm/action-setup@v2 | ||
with: | ||
version: 8 | ||
|
||
- name: Install Dependencies | ||
shell: bash | ||
run: pnpm install | ||
|
||
- name: Run MDX frontmatter check | ||
run: node .github/scripts/check-mdx-frontmatter.js |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.