MDX Validation
MDX Validation
Section titled “MDX Validation”This guide explains how to validate MDX files in your project to prevent build errors before deployment.
Overview
Section titled “Overview”We’ve implemented a comprehensive validation system to catch MDX syntax errors before they cause build failures on Vercel. The system includes:
- Custom MDX Validator - Catches common syntax errors
- Build Validation - Ensures files actually build correctly
- Agent Self-Validation - tribe-content-builder agent validates its own output
Validation Commands
Section titled “Validation Commands”Quick Validation
Section titled “Quick Validation”npm run validate:mdxRuns syntax checks on all MDX files in the project.
Complete Validation (Recommended)
Section titled “Complete Validation (Recommended)”npm run validate:allRuns MDX validation AND attempts to build the project. This is the most thorough check as it catches both syntax errors and build-time issues.
Common Issues Detected
Section titled “Common Issues Detected”1. Unclosed or Mismatched Tags
Section titled “1. Unclosed or Mismatched Tags”The validator checks for:
- Unclosed HTML/JSX tags
- Mismatched opening/closing tags
- Duplicate wrapper elements
Example of error:
🚫 Error: Line 25: Unclosed tag: <div>2. JSX Syntax Issues
Section titled “2. JSX Syntax Issues”- Using
classinstead ofclassName - Unescaped braces in JSX
- Invalid JSX expressions
Example of warning:
⚠️ Warning: Line 15: Found 'class=' attribute - use 'className=' for JSX3. Style Block Issues
Section titled “3. Style Block Issues”When using <style> blocks in MDX, they must be properly escaped:
{/* ❌ This will cause build errors */}<style> .my-class { color: red; }</style>
{/* ✅ Correct way */}<style>{` .my-class { color: red; }`}</style>Tribe Content Builder Agent
Section titled “Tribe Content Builder Agent”The tribe-content-builder agent has been updated with self-validation capabilities. It will:
- Check for proper tag matching
- Verify no duplicate wrapper elements
- Validate frontmatter syntax
- Ensure proper JSX usage
The agent includes a built-in checklist that runs before returning any MDX content.
Pre-commit Validation (Optional)
Section titled “Pre-commit Validation (Optional)”To automatically run validation before each commit, you can add a pre-commit hook:
# Install husky (optional)npm install --save-dev husky
# Add pre-commit hooknpx husky add .husky/pre-commit "npm run validate:mdx"Fixing Common Issues
Section titled “Fixing Common Issues”Duplicate Wrapper Elements
Section titled “Duplicate Wrapper Elements”When using wrapper divs, ensure they’re only opened once:
{/* ❌ Wrong - duplicate wrapper */}<div className="survey-page"> {/* content */}</div>
<div className="survey-page"> {/* more content */}</div>
{/* ✅ Correct - single wrapper */}<div className="survey-page"> {/* all content */}</div>Frontmatter Validation
Section titled “Frontmatter Validation”Ensure frontmatter is valid YAML:
---title: "My Page"description: "Page description"# ❌ Missing quote after descriptiontitle: "Unclosed string
# ✅ Correct YAMLtitle: "My Page"description: "Page description"---Best Practices
Section titled “Best Practices”- Always run
npm run validate:allbefore deploying - Fix warnings to improve code quality
- Use the tribe-content-builder agent for new MDX files - it has built-in validation
- Test style blocks carefully - they often cause build issues
Validation Output
Section titled “Validation Output”The validator provides clear feedback:
🔍 Validating MDX files...
📄 /path/to/file.mdx ✅ No issues found
📄 /path/to/problem.mdx ❌ Issues found: 🚫 Error: Line 15: Unclosed tag <div> ⚠️ Warning: Line 20: Found 'class=' attribute
📊 Validation Summary: Errors: 1 Warnings: 1
❌ Validation failed with errors!- ✅ = No issues
- 🚫 = Error (must fix)
- ⚠️ = Warning (recommended to fix)
Troubleshooting
Section titled “Troubleshooting”Build Passes but Validator Shows Errors
Section titled “Build Passes but Validator Shows Errors”The custom validator may have false positives. If the build succeeds, the file is likely fine.
Validator Misses an Error
Section titled “Validator Misses an Error”The validator catches common issues but may not catch everything. Always run the full build with npm run validate:all.
Style Block Errors
Section titled “Style Block Errors”Style blocks in MDX can be tricky. The safest approach is to use template literals:
<style>{` .class { property: value; }`}</style>Integration with CI/CD
Section titled “Integration with CI/CD”To add validation to your CI/CD pipeline:
# Example GitHub Actions- name: Validate MDX run: npm run validate:allThis ensures no broken MDX files reach production.
Technical Details
Section titled “Technical Details”Validation Script Location
Section titled “Validation Script Location”The custom validation script is located at: /scripts/validate-mdx.js
It checks for:
- Tag matching and nesting
- Duplicate wrapper elements
- Proper JSX syntax
- Style block formatting
- Frontmatter validation
Configuration Files
Section titled “Configuration Files”eslint.config.js- ESLint configuration for MDX.markdownlint-cli2.jsonc- Markdown linting rulespackage.json- Contains validation scripts
Real-World Example
Section titled “Real-World Example”The client survey page (/protected/member/client-survey.mdx) demonstrates the importance of validation. It initially failed due to:
- Unclosed
<div>tags - Improperly formatted style blocks
These issues were caught and fixed before deployment, preventing build failures on Vercel.
Pro Tip: Always run
npm run validate:allbefore pushing changes. This catches both syntax errors and build-time issues that might only appear during the actual build process.