Custom input formats
You can define custom input formats and extend existing formats by modifying your configuration file. This allows you to support new file types or customize test detection for your specific documentation patterns.
Creating a custom file type
To define a custom file type, add an entry to the fileTypes array in your configuration:
Configuration properties
Required properties:
name: A descriptive name for the file typeextensions: Array of file extensions to process with this configuration
Optional properties for inline tests:
testStartStatementOpen: Pattern that starts a test declaration (for example,"<!-- test ")testStartStatementClose: Pattern that ends a test declaration (for example," -->")testEndStatementOpen: Pattern that marks the end of a test (for example,"<!-- test end")testEndStatementClose: Pattern that closes the test end marker (for example," -->")stepStatementOpen: Pattern that starts a step declaration (for example,"<!-- step ")stepStatementClose: Pattern that ends a step declaration (for example," -->")testIgnoreStatement: Pattern for ignoring blocks
Optional properties for detected tests:
markup: Array of markup pattern objects that define how to extract tests from content
Adding custom markup patterns
Markup patterns define how Doc Detective extracts tests from your documentation content. Each pattern includes:
name: Descriptive name for the patternregex: Array of regular expressions to match contentactions: Array of actions to generate when pattern matches
Example: Custom Markdown pattern
Add a pattern to detect custom syntax for API calls:
This pattern matches content like:
And generates:
Using capture groups
Regular expression capture groups (numbered 2, etc.) are substituted into the action definition:
Matches “Find the button element” and generates:
Default actions
For basic patterns, you can use action shortcuts. Doc Detective substitutes the first capture group (or entire match) into the default location:
This is equivalent to:
Extending existing formats
You can extend existing formats by adding patterns to the default configuration. Doc Detective merges your custom patterns with the built-in patterns.
Example: Enhanced Markdown
Add patterns for RST-style directives in Markdown:
Running files as shell commands
Not every custom file type contains inline tests or markup patterns. Sometimes the file itself is the test: a script, a notebook, or any file you’d normally run from a shell. For these cases, define an executable file type that maps an extension to a runShell command. Doc Detective turns each matching file into a single test with one runShell step. Pointing it at the file then runs the file and asserts that it exits cleanly.
An executable file type needs only two properties:
extensions: The file extensions to run with this command.runShell: TherunShellstep to perform. Use$1as a placeholder for the path of the matched file.
For example, this configuration runs every .js file with Node.js, every .py file with Python, and runs Jupyter notebooks through jupyter nbconvert:
The runShell value accepts the same fields as a runShell step, so you can assert on exit codes with exitCodes and on output with stdio. A file whose command exits with a non-zero code fails the test, as does one whose output doesn’t match. That mirrors a standalone runShell step.
A file type is either a markup file type or a runShell file type, not both. When a file type defines runShell, Doc Detective runs the file as a command and doesn’t parse it for inline tests or markup.
Example: Custom documentation format
Here’s a complete example for a hypothetical custom documentation format:
This configuration supports a custom format like:
Testing custom patterns
To test your custom patterns:
- Create a sample documentation file in your format
- Add test comments or content that matches your patterns
- Run Doc Detective with the
--inputflag pointing to your file - Check the generated test results to verify patterns were detected correctly
NPX
Docker
Best practices
- Start with basics: Begin with basic patterns and add complexity as needed
- Test thoroughly: Validate patterns with various input examples
- Use descriptive names: Give patterns descriptive names so you can understand their purpose
- Document patterns: Add comments in your config to explain custom patterns
- Consider escaping: Properly escape special characters in regex patterns
- Optimize regex: Use specific patterns to avoid unintended matches
- Version control: Track configuration changes to understand pattern evolution
Common use cases
Custom input formats are useful for:
- Proprietary formats: Internal documentation formats used by your organization
- Legacy systems: Older documentation systems with specific markup
- Domain-specific languages: DSLs for specialized documentation
- Enhanced detection: More sophisticated pattern matching for existing formats
- Tool integration: Custom formats from specific authoring tools
Next steps
- Review the configuration reference for all available options
- Study existing patterns in Markdown and DITA formats
- Learn about detected tests for automatic test generation
- See inline tests for manual test definition