@scarlet-mesh/mcp-rhds
Version:
RHDS MCP Server - All-in-One Model Context Protocol server for Red Hat Design System components with manifest discovery, HTML validation, and developer tooling
47 lines (46 loc) • 2.09 kB
JavaScript
import { BaseService } from './BaseService.js';
import { RHDS_STANDARDS } from '../constants/index.js';
export class IdGeneratorService extends BaseService {
/**
* Generates a unique ID for a component based on its type, purpose, and optional context.
* The ID is formatted in kebab-case, ensuring it contains only lowercase letters, numbers, and hyphens.
* It removes any invalid characters and ensures no leading or trailing hyphens.
*/
generateId(componentType, purpose, context) {
const parts = [componentType, purpose];
if (context)
parts.push(context);
return parts
.join('-')
.toLowerCase()
.replace(/[^a-z0-9-]/g, '')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '');
}
/**
* Validates the format of a given ID based on RHDS standards.
* Checks if the ID follows kebab-case format and includes specific keywords for form and navigation components.
* Returns an object indicating whether the ID is valid, a suggestion for a valid ID, and any errors found.
*/
validateIdFormat(id, componentType) {
const errors = [];
let suggestion;
if (!RHDS_STANDARDS.idPatterns.component.test(id)) {
errors.push('ID must follow kebab-case format (lowercase letters, numbers, and hyphens only)');
suggestion = this.generateId(componentType, 'element');
}
if (componentType.includes('form') && !RHDS_STANDARDS.idPatterns.form.test(id)) {
errors.push('Form-related components should include form/input/field in the ID');
suggestion = this.generateId(componentType, 'form');
}
if (componentType.includes('nav') && !RHDS_STANDARDS.idPatterns.navigation.test(id)) {
errors.push('Navigation components should include nav/menu/link in the ID');
suggestion = this.generateId(componentType, 'nav');
}
return {
isValid: errors.length === 0,
suggestion,
errors
};
}
}