chore: unify parsing components in md

This commit is contained in:
Roman Hotsiy 2018-08-16 12:37:39 +03:00
parent 0cf7191144
commit 4ef4a971ce
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
2 changed files with 18 additions and 38 deletions

View File

@ -12,7 +12,7 @@ marked.setOptions({
},
});
export const LEGACY_REGEXP = '^\\s*<!-- ReDoc-Inject:\\s+?{component}\\s+?-->\\s*$';
export const LEGACY_REGEXP = '^\\s*<!-- ReDoc-Inject:\\s+?<{component}\\s*?/?>\\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<MDXComponentMeta>,
components?: Dict<MDXComponentMeta>,
): Array<string | MDXComponentMeta> {
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, '(<?' + names + '.*?)'),
COMPONENT_REGEXP.replace(/{component}/g, '(' + names + ')'),
'gmi',
);
let match = anyCompRegexp.exec(rawText);
@ -157,10 +169,7 @@ export class MarkdownRenderer {
match = anyCompRegexp.exec(rawText);
}
const splitCompRegexp = new RegExp(
COMPONENT_REGEXP.replace(/{component}/g, names + '.*?'),
'mi',
);
const splitCompRegexp = new RegExp(COMPONENT_REGEXP.replace(/{component}/g, names), 'mi');
const htmlParts = rawText.split(splitCompRegexp);
const res: any[] = [];
for (let i = 0; i < htmlParts.length; i++) {
@ -189,10 +198,6 @@ function parseComponent(
componentName?: string;
attrs: any;
} {
if (htmlTag.startsWith('<')) {
return legacyParse(htmlTag);
}
const match = /([\w_-]+)(\s+[\w_-]+\s*={[^}]*?})*/.exec(htmlTag);
if (match === null || match.length <= 1) {
return { componentName: undefined, attrs: {} };
@ -216,20 +221,3 @@ function parseComponent(
attrs,
};
}
function legacyParse(
htmlTag: string,
): {
componentName?: string;
attrs: any;
} {
const match = /<([\w_-]+).*?>/.exec(htmlTag);
if (match === null || match.length <= 1) {
return { componentName: undefined, attrs: {} };
}
const componentName = match[1];
return {
componentName,
attrs: {}, // TODO
};
}

View File

@ -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}', '<security-definitions>'),
'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);
}