Skip to Content

verify-links.js Documentation

The verify-links.js script checks for broken internal links and missing assets in your Lab Notebook project. It scans MDX, Markdown, and code files for links, verifies their targets, and reports errors or warnings.

What It Does

  • Scans content, components, templates, features, lib, and app directories
  • Builds an index of existing pages and static assets
  • Extracts links from files (Markdown, JSX, object properties, image sources, etc.)
  • Verifies that each internal link points to an existing page or asset
  • Prints a summary of errors, warnings, and files checked

Usage

Run the script from the project root:

node scripts/verify-links.js

Example Output

Link Verification Starting... Found 42 content pages Found 18 static assets Scanning 120 files for links... Checked 5 links in content/docs.mdx Checked 2 links in components/FeatureCard.jsx Errors found: ✗ content/docs.mdx Asset not found: /images/featured/missing-image.png Link: /images/featured/missing-image.png All links verified successfully! Link Verification Results Files checked: 120 Errors: 1 Warnings: 0

How It Works

1. Scanning for Pages and Assets

  • The script uses glob patterns to find all MDX/Markdown files in content/, and all static assets in public/.
  • It builds a set of valid page URLs and asset URLs for later verification.
  • For each file, it removes comments and code blocks to avoid false positives.

  • It extracts links from:

    • Markdown: [text](url)

    • JSX: href="/path", src="/image.png"

    • Object properties: href: '/path', image: '/image.png'

    • Template literals:

      src={`/${VAR}/image.png`}
  • It resolves template variables like ${DOC_DIR} and ${IMAGE_DIR} if defined in the file.

  • Internal links (starting with / or relative) are checked against the set of known pages or assets.
  • Asset links are checked for file existence in public/.
  • Page links are checked for existence in the content index.
  • External links (http, mailto, etc.) are ignored.

4. Error and Warning Reporting

  • Errors are reported for missing pages or assets, with file and link details.
  • Warnings can be added for suspicious patterns or skipped files.
  • A summary is printed at the end, including total files checked, errors, and warnings.
  • Markdown: [label](url)
    • JSX:

      href="/path" src="/image.png" largeSrc={`/${VAR}/image.png`}
    • Object properties:

      href: '/path' image: '/image.png' src: '/image.png'
    • Template literals:

      ${DOC_DIR}/path ${IMAGE_DIR}/image.png

Customization Tips

  • Add new glob patterns to scan additional directories or file types.
  • Extend the extractLinks method to support new link formats.
  • Use the LinkVerifier class in other scripts for custom link checks.
  • Adjust asset extensions in isAssetPath to support more file types.

Troubleshooting

  • If you see errors for missing assets, check that the file exists in public/ and the path is correct.
  • For missing pages, ensure the MDX/MD file exists and is named correctly.
  • If the script misses links, update the extraction logic to cover your use case.

This tool helps maintain link integrity and asset availability across your documentation and codebase.

Last updated on