mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-07 13:30:33 +03:00
chore: unify parsing components in md
This commit is contained in:
parent
0cf7191144
commit
4ef4a971ce
|
@ -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 MDX_COMPONENT_REGEXP = '^\\s*<{component}\\s*?/>\\s*$';
|
||||||
export const COMPONENT_REGEXP = '(?:' + LEGACY_REGEXP + '|' + MDX_COMPONENT_REGEXP + ')';
|
export const COMPONENT_REGEXP = '(?:' + LEGACY_REGEXP + '|' + MDX_COMPONENT_REGEXP + ')';
|
||||||
|
|
||||||
|
@ -35,6 +35,14 @@ export function buildComponentComment(name: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MarkdownRenderer {
|
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[] = [];
|
headings: MarkdownHeading[] = [];
|
||||||
currentTopHeading: MarkdownHeading;
|
currentTopHeading: MarkdownHeading;
|
||||||
|
|
||||||
|
@ -142,13 +150,17 @@ export class MarkdownRenderer {
|
||||||
// Use marked ecosystem
|
// Use marked ecosystem
|
||||||
renderMdWithComponents(
|
renderMdWithComponents(
|
||||||
rawText: string,
|
rawText: string,
|
||||||
components: Dict<MDXComponentMeta>,
|
components?: Dict<MDXComponentMeta>,
|
||||||
): Array<string | MDXComponentMeta> {
|
): Array<string | MDXComponentMeta> {
|
||||||
|
if (!components || Object.keys(components).length === 0) {
|
||||||
|
return [this.renderMd(rawText)];
|
||||||
|
}
|
||||||
|
|
||||||
const componentDefs: string[] = [];
|
const componentDefs: string[] = [];
|
||||||
const names = '(?:' + Object.keys(components).join('|') + ')';
|
const names = '(?:' + Object.keys(components).join('|') + ')';
|
||||||
|
|
||||||
const anyCompRegexp = new RegExp(
|
const anyCompRegexp = new RegExp(
|
||||||
COMPONENT_REGEXP.replace(/{component}/g, '(<?' + names + '.*?)'),
|
COMPONENT_REGEXP.replace(/{component}/g, '(' + names + ')'),
|
||||||
'gmi',
|
'gmi',
|
||||||
);
|
);
|
||||||
let match = anyCompRegexp.exec(rawText);
|
let match = anyCompRegexp.exec(rawText);
|
||||||
|
@ -157,10 +169,7 @@ export class MarkdownRenderer {
|
||||||
match = anyCompRegexp.exec(rawText);
|
match = anyCompRegexp.exec(rawText);
|
||||||
}
|
}
|
||||||
|
|
||||||
const splitCompRegexp = new RegExp(
|
const splitCompRegexp = new RegExp(COMPONENT_REGEXP.replace(/{component}/g, names), 'mi');
|
||||||
COMPONENT_REGEXP.replace(/{component}/g, names + '.*?'),
|
|
||||||
'mi',
|
|
||||||
);
|
|
||||||
const htmlParts = rawText.split(splitCompRegexp);
|
const htmlParts = rawText.split(splitCompRegexp);
|
||||||
const res: any[] = [];
|
const res: any[] = [];
|
||||||
for (let i = 0; i < htmlParts.length; i++) {
|
for (let i = 0; i < htmlParts.length; i++) {
|
||||||
|
@ -189,10 +198,6 @@ function parseComponent(
|
||||||
componentName?: string;
|
componentName?: string;
|
||||||
attrs: any;
|
attrs: any;
|
||||||
} {
|
} {
|
||||||
if (htmlTag.startsWith('<')) {
|
|
||||||
return legacyParse(htmlTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
const match = /([\w_-]+)(\s+[\w_-]+\s*={[^}]*?})*/.exec(htmlTag);
|
const match = /([\w_-]+)(\s+[\w_-]+\s*={[^}]*?})*/.exec(htmlTag);
|
||||||
if (match === null || match.length <= 1) {
|
if (match === null || match.length <= 1) {
|
||||||
return { componentName: undefined, attrs: {} };
|
return { componentName: undefined, attrs: {} };
|
||||||
|
@ -216,20 +221,3 @@ function parseComponent(
|
||||||
attrs,
|
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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';
|
||||||
import { appendToMdHeading, IS_BROWSER } from '../utils/';
|
import { appendToMdHeading, IS_BROWSER } from '../utils/';
|
||||||
import { JsonPointer } from '../utils/JsonPointer';
|
import { JsonPointer } from '../utils/JsonPointer';
|
||||||
import { isNamedDefinition } from '../utils/openapi';
|
import { isNamedDefinition } from '../utils/openapi';
|
||||||
import { buildComponentComment, COMPONENT_REGEXP, MDX_COMPONENT_REGEXP } from './MarkdownRenderer';
|
import { buildComponentComment, MarkdownRenderer } from './MarkdownRenderer';
|
||||||
import { RedocNormalizedOptions } from './RedocNormalizedOptions';
|
import { RedocNormalizedOptions } from './RedocNormalizedOptions';
|
||||||
|
|
||||||
export type MergedOpenAPISchema = OpenAPISchema & { parentRefs?: string[] };
|
export type MergedOpenAPISchema = OpenAPISchema & { parentRefs?: string[] };
|
||||||
|
@ -74,15 +74,7 @@ export class OpenAPIParser {
|
||||||
) {
|
) {
|
||||||
// Automatically inject Authentication section with SecurityDefinitions component
|
// Automatically inject Authentication section with SecurityDefinitions component
|
||||||
const description = spec.info.description || '';
|
const description = spec.info.description || '';
|
||||||
const legacySecurityRegexp = new RegExp(
|
if (!MarkdownRenderer.containsComponent(description, 'security-definitions')) {
|
||||||
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)) {
|
|
||||||
const comment = buildComponentComment('security-definitions');
|
const comment = buildComponentComment('security-definitions');
|
||||||
spec.info.description = appendToMdHeading(description, 'Authentication', comment);
|
spec.info.description = appendToMdHeading(description, 'Authentication', comment);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user