diff --git a/src/services/MarkdownRenderer.ts b/src/services/MarkdownRenderer.ts index 9e833eb819..63dceabf4d 100644 --- a/src/services/MarkdownRenderer.ts +++ b/src/services/MarkdownRenderer.ts @@ -1,8 +1,8 @@ import * as marked from 'marked'; - import slugify from 'slugify'; -import { MDComponent } from '../components/Markdown/Markdown'; + import { highlight, html2Str } from '../utils'; +import { AppStore } from './AppStore'; import { SECTION_ATTR } from './MenuStore'; const renderer = new marked.Renderer(); @@ -14,9 +14,14 @@ marked.setOptions({ }, }); -export const COMPONENT_REGEXP = '^\\s*\\s*$'; -export function buildComponentComment(name: string) { - return ``; +export const LEGACY_REGEXP = '^\\s*\\s*$'; +export const MDX_COMPONENT_REGEXP = '^\\s*<{component}\\s*?/>\\s*$'; +export const COMPONENT_REGEXP = '(?:' + LEGACY_REGEXP + '|' + MDX_COMPONENT_REGEXP + ')'; + +export interface MDXComponentMeta { + component: React.ComponentType; + propsSelector: (store?: AppStore) => any; + attrs?: object; } export interface MarkdownHeading { @@ -26,6 +31,10 @@ export interface MarkdownHeading { description?: string; } +export function buildComponentComment(name: string) { + return ``; +} + export class MarkdownRenderer { headings: MarkdownHeading[] = []; currentTopHeading: MarkdownHeading; @@ -130,20 +139,30 @@ export class MarkdownRenderer { return res; } + // TODO: rewrite this completelly! Regexp-based 👎 + // Use marked ecosystem renderMdWithComponents( rawText: string, - components: Dict, + components: Dict, raw: boolean = true, - ): Array { + ): Array { const componentDefs: string[] = []; - const anyCompRegexp = new RegExp(COMPONENT_REGEXP.replace('{component}', '(.*?)'), 'gmi'); + const names = '(?:' + Object.keys(components).join('|') + ')'; + + const anyCompRegexp = new RegExp( + COMPONENT_REGEXP.replace(/{component}/g, '( p.trim()); + + // tslint:disable-next-line + attrs[name] = value.startsWith('{') ? eval(value.substr(1, value.length - 2)) : eval(value); + } + return { + componentName, + attrs, + }; +} + +function legacyParse( + htmlTag: string, +): { + componentName?: string; + attrs: any; } { const match = /<([\w_-]+).*?>/.exec(htmlTag); if (match === null || match.length <= 1) {