diff --git a/src/services/MarkdownRenderer.ts b/src/services/MarkdownRenderer.ts index acb3520f..bb83efb7 100644 --- a/src/services/MarkdownRenderer.ts +++ b/src/services/MarkdownRenderer.ts @@ -12,7 +12,7 @@ marked.setOptions({ }, }); -export const LEGACY_REGEXP = '^\\s*\\s*$'; +export const LEGACY_REGEXP = '^\\s*\\s*$'; export const MDX_COMPONENT_REGEXP = '^\\s*<{component}\\s*?/>\\s*$'; export const COMPONENT_REGEXP = '(?:' + LEGACY_REGEXP + '|' + MDX_COMPONENT_REGEXP + ')'; @@ -35,6 +35,14 @@ export function buildComponentComment(name: string) { } export class MarkdownRenderer { + static containsComponent(rawText: string, componentName: string) { + const anyCompRegexp = new RegExp( + COMPONENT_REGEXP.replace(/{component}/g, componentName), + 'gmi', + ); + return anyCompRegexp.test(rawText); + } + headings: MarkdownHeading[] = []; currentTopHeading: MarkdownHeading; @@ -142,13 +150,17 @@ export class MarkdownRenderer { // Use marked ecosystem renderMdWithComponents( rawText: string, - components: Dict, + components?: Dict, ): Array { + if (!components || Object.keys(components).length === 0) { + return [this.renderMd(rawText)]; + } + const componentDefs: string[] = []; const names = '(?:' + Object.keys(components).join('|') + ')'; const anyCompRegexp = new RegExp( - COMPONENT_REGEXP.replace(/{component}/g, '(/.exec(htmlTag); - if (match === null || match.length <= 1) { - return { componentName: undefined, attrs: {} }; - } - const componentName = match[1]; - return { - componentName, - attrs: {}, // TODO - }; -} diff --git a/src/services/OpenAPIParser.ts b/src/services/OpenAPIParser.ts index 05cdf77d..654cfcdb 100644 --- a/src/services/OpenAPIParser.ts +++ b/src/services/OpenAPIParser.ts @@ -5,7 +5,7 @@ import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types'; import { appendToMdHeading, IS_BROWSER } from '../utils/'; import { JsonPointer } from '../utils/JsonPointer'; import { isNamedDefinition } from '../utils/openapi'; -import { buildComponentComment, COMPONENT_REGEXP, MDX_COMPONENT_REGEXP } from './MarkdownRenderer'; +import { buildComponentComment, MarkdownRenderer } from './MarkdownRenderer'; import { RedocNormalizedOptions } from './RedocNormalizedOptions'; export type MergedOpenAPISchema = OpenAPISchema & { parentRefs?: string[] }; @@ -74,15 +74,7 @@ export class OpenAPIParser { ) { // Automatically inject Authentication section with SecurityDefinitions component const description = spec.info.description || ''; - const legacySecurityRegexp = new RegExp( - COMPONENT_REGEXP.replace('{component}', ''), - 'mi', - ); - const securityRegexp = new RegExp( - MDX_COMPONENT_REGEXP.replace('{component}', 'security-definitions'), - 'mi', - ); - if (!legacySecurityRegexp.test(description) && !securityRegexp.test(description)) { + if (!MarkdownRenderer.containsComponent(description, 'security-definitions')) { const comment = buildComponentComment('security-definitions'); spec.info.description = appendToMdHeading(description, 'Authentication', comment); }