import { ScalarApiButton } from './ScalarApiButton'; import type { OpenAPIOperationData, OpenAPIContextProps } from './types'; /** * Display the path of an operation. */ export function OpenAPIPath(props: { data: OpenAPIOperationData; context: OpenAPIContextProps; }): JSX.Element { const { data, context } = props; const { method, path } = data; const { specUrl } = context; return (
{method}

{formatPath(path)}

{data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'] ? null : ( )}
); } // Format the path to highlight placeholders function formatPath(path: string) { // Matches placeholders like {id}, {userId}, etc. const regex = /\{(\w+)\}/g; const parts: (string | JSX.Element)[] = []; let lastIndex = 0; // Replace placeholders with tags path.replace(regex, (match, key, offset) => { parts.push(path.slice(lastIndex, offset)); parts.push({`{${key}}`}); lastIndex = offset + match.length; return match; }); // Push remaining text after the last placeholder parts.push(path.slice(lastIndex)); // Join parts with separators wrapped in const formattedPath = parts.reduce( (acc, part, index) => { if (typeof part === 'string' && index > 0 && part === '/') { return [ ...acc, / , part, ]; } return [...acc, part]; }, [] as (string | JSX.Element)[], ); return {formattedPath}; }