This guide provides best practices and instructions for developing this Prism.js plugin with Claude Code assistance.
This project is optimized for development with Claude Code, enabling AI-assisted coding for creating and maintaining the Prism.js language definition plugin.
- Type: Prism.js language definition plugin
- Purpose: Syntax highlighting for a FHIR Mapping Language (FML)
- Framework: Prism.js extension
- License: MIT
- Standards: GitHub best practices, conventional commits
- Prism.js Compatibility: Must follow Prism.js plugin architecture
- Performance: Regex patterns must avoid catastrophic backtracking
- Testing: Comprehensive test coverage required
- Documentation: Clear documentation for users and contributors
- Browser Support: ES6+ with fallbacks if needed
When creating the language definition with Claude:
Task: Create a Prism.js language definition for [LANGUAGE_NAME]
Context:
- Language paradigm: [procedural/functional/object-oriented]
- Similar to: [existing languages for reference]
- Key features: [list unique syntax features]
Requirements:
1. Define token patterns for:
- Comments (single-line, multi-line)
- Strings (with escape sequences)
- Keywords
- Operators
- Functions/methods
- Variables/identifiers
- Numbers (integers, floats, etc.)
- Special constructs
2. Follow Prism.js conventions:
- Use appropriate token names
- Implement 'greedy' flag where needed
- Handle edge cases
3. Optimize patterns for performanceWhen asking Claude to write regex patterns:
Task: Write a regex pattern for [FEATURE]
Requirements:
- Must handle: [list all cases]
- Must NOT match: [list exclusions]
- Performance: Avoid backtracking
- Use non-capturing groups where possible
Examples to match:
- [example 1]
- [example 2]
Examples to NOT match:
- [counter-example 1]
- [counter-example 2]For comprehensive test creation:
Task: Generate test cases for [LANGUAGE_NAME] syntax highlighting
Include tests for:
1. Basic tokens (keywords, operators, etc.)
2. Complex constructs (nested structures)
3. Edge cases (empty, malformed)
4. Performance cases (long strings, deep nesting)
5. Common patterns from real code
Format:
- Input code
- Expected token structure
- Visual verification HTMLWhen creating documentation with Claude:
Task: Document the [FEATURE/API/USAGE]
Include:
1. Purpose and overview
2. Syntax/API reference
3. Code examples (at least 3)
4. Common use cases
5. Troubleshooting
6. Performance considerations- Define the Language Specification
Claude, help me define the language specification for FHIR Mapping Language:
- Provide a comprehensive list of language features
- Identify unique syntax elements
- Suggest token categories needed- Create Core Language Definition
Claude, create the initial prism-lang-fml.js file with:
- Basic token definitions
- Pattern implementations
- Prism.js registration code- Generate Test Suite
Claude, generate comprehensive test cases covering:
- All token types
- Edge cases
- Real-world code examples- Pattern Refinement
Claude, this pattern [PATTERN] is matching incorrectly.
Current behavior: [description]
Expected behavior: [description]
Test case: [code sample]
Please provide a corrected pattern.- Performance Optimization
Claude, analyze this regex pattern for performance issues:
[PATTERN]
Suggest optimizations to prevent backtracking.- Feature Addition
Claude, add support for [FEATURE]:
- Syntax: [description]
- Examples: [code samples]
- Similar to: [reference from other language]When reviewing code with Claude, check:
- All token types are defined
- Patterns are efficient (no catastrophic backtracking)
- Greedy flag used appropriately
- Token precedence is correct
- Edge cases handled
- Unit tests cover all tokens
- Visual tests render correctly
- Performance tests pass
- Edge cases tested
- Real-world examples work
- README is complete
- API documentation is clear
- Examples are working
- Language spec is detailed
- Comments explain complex patterns
- ESLint passes
- Follows project conventions
- No console.log statements
- Error handling implemented
- Performance optimized
- Be Specific
❌ "Fix the string pattern"
✅ "The string pattern currently doesn't handle escaped quotes. Update it to match: 'It\'s working' and "She said \"Hello\""- Provide Context
✅ "In the context of a Prism.js plugin, create a pattern that matches function declarations in FHIR Mapping Language, similar to JavaScript but with 'fn' keyword instead of 'function'"- Include Examples
✅ "Create a pattern for comments that matches:
- Single line: // comment
- Multi-line: /_ comment _/
- Doc comments: /\*_ comment _/
But NOT: http://url (the // in URLs)"- Request Explanations
✅ "Explain why you chose this regex pattern and how it avoids backtracking"Claude, add a new token type for [TOKEN_NAME]:
1. Define the pattern
2. Set appropriate precedence
3. Add test cases
4. Update documentationClaude, resolve pattern conflict:
- Pattern A: [pattern] matches [examples]
- Pattern B: [pattern] matches [examples]
- Conflict: Both match [example]
- Desired behavior: [description]Claude, optimize the language definition:
1. Identify potential performance issues
2. Suggest pattern improvements
3. Implement lazy quantifiers where appropriate
4. Combine similar patterns if possibleClaude, generate documentation for:
1. Installation guide with all methods
2. Configuration options
3. API reference with examples
4. Troubleshooting section
5. Migration guide from similar pluginsWhen working with Claude, reference these:
- FHIR Mapping Language Specification
- FHIR Mapping Language Grammar
- FHIR Mapping Tutorial
- StructureMap Resource
- FHIRPath Specification
- HL7 FHIR Specification
- FHIR Mapping Language Implementation Guide
- Use non-capturing groups:
(?:...) - Prefer lazy quantifiers:
*?,+? - Avoid nested quantifiers
- Use atomic groups when possible
// String with escapes
string: {
pattern: /(["'])(?:\\.|(?!\1)[^\\\r\n])*\1/,
greedy: true
}
// Comments
comment: [
{
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
lookbehind: true,
greedy: true
},
{
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true
}
]
// Keywords
keyword: /\b(?:if|else|while|for|return|function)\b/When generating code with Claude:
// Preferred style
Prism.languages.fml = {
comment: {
pattern: /\/\/.*/,
greedy: true
},
string: {
pattern: /(["'])(?:\\.|(?!\1)[^\\\r\n])*\1/,
greedy: true
},
keyword: /\b(?:if|else|while)\b/,
operator: /[+\-*/%]=?|[<>]=?|==|&&|\|\|/,
punctuation: /[{}[\];(),.:]/
};describe('Language Definition', () => {
it('should highlight keywords', () => {
const code = 'if (condition) { return true; }';
const tokens = Prism.tokenize(code, Prism.languages.fml);
expect(tokens[0].type).toBe('keyword');
expect(tokens[0].content).toBe('if');
});
});When debugging issues:
Claude, debug this highlighting issue:
Input code:[paste code]
Current output:
[describe what tokens are generated]
Expected output:
[describe what should be generated]
Language definition:
```javascript
[paste relevant part of definition]
Please identify the issue and provide a fix.
## 📈 Performance Optimization
Ask Claude to optimize patterns:
```markdown
Claude, analyze these patterns for performance:
```javascript
[paste language definition]
Please:
- Identify patterns prone to backtracking
- Suggest optimized versions
- Explain the improvements
- Provide benchmark comparisons if possible
## 🔄 Iteration Guidelines
### Version 1: MVP
- Basic token support
- Core language features
- Minimal viable highlighting
### Version 2: Enhanced
- Advanced language features
- Improved pattern accuracy
- Performance optimizations
### Version 3: Production
- Complete language coverage
- Extensive testing
- Full documentation
- Theme support
## 💡 Tips for Success
1. **Start Simple**: Begin with basic tokens, add complexity gradually
2. **Test Continuously**: Verify each pattern with multiple examples
3. **Document Patterns**: Comment complex regex with examples
4. **Benchmark Performance**: Test with large files
5. **Iterate Based on Feedback**: Refine patterns based on real usage
## 🆘 Troubleshooting with Claude
Common issues and how to ask for help:
### Pattern Not Matching
```markdown
Pattern [PATTERN] isn't matching [EXAMPLE].
Debug info: [any console output]
Please fix and explain the issue.
The plugin is slow with [FILE SIZE] files.
Suspect patterns: [list patterns]
Please optimize for better performance.Tokens [TOKEN1] and [TOKEN2] are conflicting.
Example: [code that shows conflict]
How should precedence be handled?When ready to deploy, ask Claude:
Claude, prepare the project for deployment:
1. Verify all files are complete
2. Check package.json configuration
3. Ensure build process works
4. Validate documentation
5. Create release checklistnpm run dev # Development mode
npm test # Run tests
npm run build # Build for production
npm run lint # Check code style
npm run test:visual # Visual testing- Language definition:
src/prism-lang-fml.js - Tests:
test/prism-lang-fml.test.js - Examples:
examples/demo.html - Visual test:
test/visual.html
feat: add support for async functions
fix: correct string escaping issue
docs: update API documentation
test: add edge case tests
perf: optimize comment pattern
Remember: Claude Code is here to assist, but always review generated code, test thoroughly, and ensure it meets project standards!