-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test: add markdown tests #3301
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
Merged
Merged
test: add markdown tests #3301
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
663e053
add check-md script
anshgoyalevil 25eb795
fix check-md script
anshgoyalevil 8f547bd
add md test
anshgoyalevil a5122fb
Merge branch 'master' into markdown_tests
anshgoyalevil a7d2172
change token name
anshgoyalevil 43a97b4
fix comment logic
anshgoyalevil 284fe3a
fix comment logic
anshgoyalevil 85f5fab
fix comment logic
anshgoyalevil 7288637
update output
anshgoyalevil 6a9904e
update output
anshgoyalevil bfa25f9
update output
anshgoyalevil 56eda46
update output
anshgoyalevil 3fad1fa
update output
anshgoyalevil 6b881a8
update output
anshgoyalevil c676e3d
update output
anshgoyalevil 1fb8b0e
add coderabbit suggestion
anshgoyalevil 607c9e9
fix formatting
anshgoyalevil 2fcb7f9
Merge branch 'master' into markdown_tests
anshgoyalevil 4cd9cfa
fix doc gray matter
anshgoyalevil c6642ee
Merge branch 'master' into markdown_tests
anshgoyalevil 69caa08
Merge branch 'master' into markdown_tests
anshgoyalevil a8e7404
Merge branch 'master' into markdown_tests
akshatnema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| --- | ||
| title: "Migrating to v3" | ||
| weight: 2 | ||
| --- | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,146 @@ | ||
| const fs = require('fs'); | ||
| const matter = require('gray-matter'); | ||
| const path = require('path'); | ||
|
|
||
| /** | ||
| * Checks if a given string is a valid URL. | ||
| * @param {string} str - The string to validate as a URL. | ||
| * @returns {boolean} True if the string is a valid URL, false otherwise. | ||
| */ | ||
| function isValidURL(str) { | ||
| try { | ||
| new URL(str); | ||
| return true; | ||
| } catch (err) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates the frontmatter of a blog post. | ||
| * @param {object} frontmatter - The frontmatter object to validate. | ||
| * @param {string} filePath - The path to the file being validated. | ||
| * @returns {string[]|null} An array of validation error messages, or null if no errors. | ||
| */ | ||
| function validateBlogs(frontmatter) { | ||
| const requiredAttributes = ['title', 'date', 'type', 'tags', 'cover', 'authors']; | ||
| const errors = []; | ||
|
|
||
| // Check for required attributes | ||
| requiredAttributes.forEach(attr => { | ||
| if (!frontmatter.hasOwnProperty(attr)) { | ||
anshgoyalevil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| errors.push(`${attr} is missing`); | ||
| } | ||
| }); | ||
|
|
||
| // Validate date format | ||
| if (frontmatter.date && Number.isNaN(Date.parse(frontmatter.date))) { | ||
| errors.push(`Invalid date format: ${frontmatter.date}`); | ||
| } | ||
|
|
||
| // Validate tags format (must be an array) | ||
| if (frontmatter.tags && !Array.isArray(frontmatter.tags)) { | ||
| errors.push(`Tags should be an array`); | ||
| } | ||
|
|
||
| // Validate cover is a string | ||
| if (frontmatter.cover && typeof frontmatter.cover !== 'string') { | ||
| errors.push(`Cover must be a string`); | ||
| } | ||
|
|
||
| // Validate authors (must be an array with valid attributes) | ||
| if (frontmatter.authors) { | ||
| if (!Array.isArray(frontmatter.authors)) { | ||
| errors.push('Authors should be an array'); | ||
| } else { | ||
| frontmatter.authors.forEach((author, index) => { | ||
| if (!author.name) { | ||
| errors.push(`Author at index ${index} is missing a name`); | ||
| } | ||
| if (author.link && !isValidURL(author.link)) { | ||
| errors.push(`Invalid URL for author at index ${index}: ${author.link}`); | ||
| } | ||
| if (!author.photo) { | ||
| errors.push(`Author at index ${index} is missing a photo`); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
anshgoyalevil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return errors.length ? errors : null; | ||
| } | ||
anshgoyalevil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Validates the frontmatter of a documentation file. | ||
| * @param {object} frontmatter - The frontmatter object to validate. | ||
| * @param {string} filePath - The path to the file being validated. | ||
| * @returns {string[]|null} An array of validation error messages, or null if no errors. | ||
| */ | ||
| function validateDocs(frontmatter) { | ||
| const errors = []; | ||
|
|
||
| // Check if title exists and is a string | ||
| if (!frontmatter.title || typeof frontmatter.title !== 'string') { | ||
| errors.push('Title is missing or not a string'); | ||
| } | ||
|
|
||
| // Check if weight exists and is a number | ||
| if (frontmatter.weight === undefined || typeof frontmatter.weight !== 'number') { | ||
| errors.push('Weight is missing or not a number'); | ||
| } | ||
|
|
||
| return errors.length ? errors : null; | ||
| } | ||
anshgoyalevil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Recursively checks markdown files in a folder and validates their frontmatter. | ||
| * @param {string} folderPath - The path to the folder to check. | ||
| * @param {Function} validateFunction - The function used to validate the frontmatter. | ||
| * @param {string} [relativePath=''] - The relative path of the folder for logging purposes. | ||
| */ | ||
| function checkMarkdownFiles(folderPath, validateFunction, relativePath = '') { | ||
| fs.readdir(folderPath, (err, files) => { | ||
| if (err) { | ||
| console.error('Error reading directory:', err); | ||
| return; | ||
| } | ||
anshgoyalevil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| files.forEach(file => { | ||
| const filePath = path.join(folderPath, file); | ||
| const relativeFilePath = path.join(relativePath, file); | ||
|
|
||
| // Skip the folder 'docs/reference/specification' | ||
| if (relativeFilePath.includes('reference/specification')) { | ||
| return; | ||
| } | ||
|
|
||
| fs.stat(filePath, (err, stats) => { | ||
| if (err) { | ||
| console.error('Error reading file stats:', err); | ||
| return; | ||
| } | ||
|
|
||
| // Recurse if directory, otherwise validate markdown file | ||
| if (stats.isDirectory()) { | ||
| checkMarkdownFiles(filePath, validateFunction, relativeFilePath); | ||
| } else if (path.extname(file) === '.md') { | ||
| const fileContent = fs.readFileSync(filePath, 'utf-8'); | ||
| const { data: frontmatter } = matter(fileContent); | ||
anshgoyalevil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const errors = validateFunction(frontmatter); | ||
| if (errors) { | ||
| console.log(`Errors in file ${relativeFilePath}:`); | ||
| errors.forEach(error => console.log(` - ${error}`)); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const docsFolderPath = path.resolve(__dirname, '../../markdown/docs'); | ||
| const blogsFolderPath = path.resolve(__dirname, '../../markdown/blog'); | ||
|
|
||
| checkMarkdownFiles(docsFolderPath, validateDocs); | ||
| checkMarkdownFiles(blogsFolderPath, validateBlogs); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.