This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Docusaurus plugin that generates JSON-LD structured data (schema.org) for SEO. It scans markdown/MDX files in a Docusaurus site for front matter schema definitions and generates a Root component that injects the appropriate structured data into each page.
# Build the TypeScript source
npm run build
# Watch mode for development (recompiles on file changes)
npm run watch
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage report
npm run test:coverageThe plugin compiles from src/ to lib/ as JavaScript and TypeScript declaration files. Tests are run with Jest and ts-jest for TypeScript support.
A pre-commit hook is configured with Husky to automatically run npm run build before each commit. This ensures that the compiled lib/ directory is always in sync with the source code.
Simply commit as usual:
git add src/
git commit -m "Your message"
# npm run build runs automaticallyThe build must succeed for the commit to complete. If the build fails, fix the TypeScript errors and try committing again.
-
Plugin Entry (
src/index.ts): Exports a Docusaurus plugin that registers a CLI commandgenerate-structured-data -
Generator (
src/generator.ts): The main logic that:- Scans markdown/MDX files in docs, blog, and src/pages directories (including i18n variants)
- Extracts front matter
schemadata from each file - Generates a React Root component that:
- Reads the current page URL from router
- Injects JSON-LD
<script type="application/ld+json">tags into the page - Enriches schema data with site-wide metadata (organization, person, website)
-
Data Extraction: Processes markdown front matter using
gray-matterto extractschema.*fields (type, headline, image, description, keywords, datePublished, dateModified) -
Schema Type Handling: Different schema.org types get different treatment:
Article,BlogPosting,CollectionPage,Blog,AboutPage: Get author/publisher referencesService,WebSite: Get provider referenceOrganization,Person: No automatic enrichment
-
i18n Support: Scans i18n locale directories under
i18n/{locale}/docusaurus-plugin-content-*and generates locale-specific schema data -
Output: Writes a
Root.jsfile (default:src/theme/Root.js) that serves as the Docusaurus Root component wrapper
The plugin accepts in docusaurus.config.js:
baseSchema: Default organization/person/website schema structurei18n: Locale-specific schema overridesoutputFile: Custom output path for Root.js (default:src/theme/Root.js)defaultImage: Fallback image URL for schemaverbose: Enable verbose logging during generationsrcDir: Custom path for pages directory (relative to siteDir, default:src/pages)blogDir: Custom path for blog directory (relative to siteDir, default:blog)docsDir: Custom path for docs directory (relative to siteDir, default:docs)sameAsDefault: Array of schema.org properties to use as defaults
Error Handling:
- Try/catch blocks around file operations (read, write, directory traversal)
- Automatic creation of output directory if it doesn't exist
- Graceful skipping of missing directories with logging
TypeScript Interfaces (since last refactor):
DocsaurusContext: Docusaurus runtime context with type safetyPluginOptions: All plugin configuration options typedFrontMatterSchema: Front matter schema field definitionsFrontMatterData: Complete front matter structure
Logging:
- Centralized
logger()function for consistent output - Info-level logs only show with
verbose: true - Warning and error logs always display
- Replaces all
console.log/warn/errorcalls
Code Organization:
- Removed 3 identical wrapper functions (replaced with direct
getData()calls) - Schema type handling uses array
includes()instead of long if/else chains - Configuration object
SUPPORTED_SCHEMA_TYPESdocuments all handled schema.org types
The project includes a comprehensive Jest test suite (src/generator.test.ts) with 14+ tests covering:
Core Functionality Tests:
- ✓ Directory creation when output path doesn't exist
- ✓ Root.js file generation with correct output
- ✓ Custom output file paths
- ✓ Custom directory paths (srcDir, blogDir, docsDir)
- ✓ Verbose logging flag behavior
- ✓ Schema configuration application
Error Handling Tests:
- ✓ Permission errors during directory creation
- ✓ Disk write failures
- ✓ Graceful handling of missing configs
Schema Generation Tests:
- ✓ Valid JSON-LD structured data output
- ✓ Article schema type handling
- ✓ All schema.org type handlers included
- ✓ i18n locale support
Test Framework: Jest with ts-jest for TypeScript support Mocking: fs and path modules are mocked to avoid file I/O during tests Coverage: Tests mock file system operations and validate generated code output