redoc/src/services/MarkdownRenderer.ts

177 lines
5.0 KiB
TypeScript
Raw Normal View History

import * as marked from 'marked';
2017-11-23 00:38:38 +03:00
2018-02-08 00:01:56 +03:00
import slugify from 'slugify';
2017-11-23 00:38:38 +03:00
import { MDComponent } from '../components/Markdown/Markdown';
2018-01-22 21:30:53 +03:00
import { highlight, html2Str } from '../utils';
import { SECTION_ATTR } from './MenuStore';
2017-10-12 00:01:37 +03:00
const renderer = new marked.Renderer();
marked.setOptions({
renderer,
2017-10-12 00:01:37 +03:00
highlight: (str, lang) => {
return highlight(str, lang);
},
});
2017-11-21 17:33:22 +03:00
export const COMPONENT_REGEXP = '^\\s*<!-- ReDoc-Inject:\\s+?{component}\\s+?-->\\s*$';
export function buildComponentComment(name: string) {
return `<!-- ReDoc-Inject: <${name}> -->`;
}
2017-10-12 00:01:37 +03:00
2018-01-22 21:30:53 +03:00
interface MarkdownHeading {
id: string;
2017-10-12 00:01:37 +03:00
name: string;
items?: MarkdownHeading[];
description?: string;
2018-01-22 21:30:53 +03:00
}
2017-10-12 00:01:37 +03:00
export class MarkdownRenderer {
headings: MarkdownHeading[] = [];
currentTopHeading: MarkdownHeading;
2017-10-12 00:01:37 +03:00
private headingEnhanceRenderer: marked.Renderer;
private originalHeadingRule: typeof marked.Renderer.prototype.heading;
2017-10-12 00:01:37 +03:00
constructor() {
this.headingEnhanceRenderer = new marked.Renderer();
this.originalHeadingRule = this.headingEnhanceRenderer.heading.bind(
this.headingEnhanceRenderer,
);
this.headingEnhanceRenderer.heading = this.headingRule;
2017-10-12 00:01:37 +03:00
}
saveHeading(name: string, container: MarkdownHeading[] = this.headings): MarkdownHeading {
const item = {
id: 'section' + '/' + slugify(name),
2017-10-12 00:01:37 +03:00
name,
2018-02-08 00:01:56 +03:00
items: [],
};
2017-10-12 00:01:37 +03:00
container.push(item);
return item;
}
flattenHeadings(container?: MarkdownHeading[]): MarkdownHeading[] {
2018-01-22 21:30:53 +03:00
if (container === undefined) {
return [];
}
const res: MarkdownHeading[] = [];
for (const heading of container) {
2017-10-12 00:01:37 +03:00
res.push(heading);
res.push(...this.flattenHeadings(heading.items));
2017-10-12 00:01:37 +03:00
}
return res;
}
attachHeadingsDescriptions(rawText: string) {
const buildRegexp = heading =>
new RegExp(`<h\\d ${SECTION_ATTR}="${heading.id}" id="${heading.id}">`);
2017-10-12 00:01:37 +03:00
2018-01-22 21:30:53 +03:00
const flatHeadings = this.flattenHeadings(this.headings);
if (flatHeadings.length < 1) {
return;
}
2017-10-12 00:01:37 +03:00
let prevHeading = flatHeadings[0];
let prevPos = rawText.search(buildRegexp(prevHeading));
for (let i = 1; i < flatHeadings.length; i++) {
2018-01-22 21:30:53 +03:00
const heading = flatHeadings[i];
const currentPos = rawText.substr(prevPos + 1).search(buildRegexp(heading)) + prevPos + 1;
prevHeading.description = html2Str(rawText.substring(prevPos, currentPos));
2017-10-12 00:01:37 +03:00
prevHeading = heading;
prevPos = currentPos;
}
prevHeading.description = html2Str(rawText.substring(prevPos));
2017-10-12 00:01:37 +03:00
}
headingRule = (text: string, level: number, raw: string) => {
if (level === 1) {
this.currentTopHeading = this.saveHeading(text);
const id = this.currentTopHeading.id;
return (
`<a name="${id}"></a>` +
`<h${level} ${SECTION_ATTR}="${id}" id="${id}">` +
`<a class="share-link" href="#${id}"></a>${text}</h${level}>`
);
} else if (level === 2) {
const { id } = this.saveHeading(text, this.currentTopHeading && this.currentTopHeading.items);
return (
`<a name="${id}"></a>` +
`<h${level} ${SECTION_ATTR}="${id}" id="${id}">` +
`<a class="share-link" href="#${id}"></a>${text}</h${level}>`
);
2017-10-12 00:01:37 +03:00
} else {
return this.originalHeadingRule(text, level, raw);
2017-10-12 00:01:37 +03:00
}
};
renderMd(rawText: string, raw: boolean = true): string {
const opts = raw ? undefined : { renderer: this.headingEnhanceRenderer };
2017-10-12 00:01:37 +03:00
const res = marked(rawText.toString(), opts);
2017-10-12 00:01:37 +03:00
return res;
}
extractHeadings(rawText: string): MarkdownHeading[] {
2018-02-08 00:01:56 +03:00
const text = this.renderMd(rawText, false);
this.attachHeadingsDescriptions(text);
2017-10-12 00:01:37 +03:00
const res = this.headings;
this.headings = [];
return res;
}
renderMdWithComponents(
rawText: string,
2017-11-23 00:38:38 +03:00
components: Dict<MDComponent>,
2017-10-12 00:01:37 +03:00
raw: boolean = true,
2018-01-22 21:30:53 +03:00
): Array<string | MDComponent> {
const componentDefs: string[] = [];
const anyCompRegexp = new RegExp(COMPONENT_REGEXP.replace('{component}', '(.*?)'), 'gmi');
let match = anyCompRegexp.exec(rawText);
while (match) {
2017-10-12 00:01:37 +03:00
componentDefs.push(match[1]);
2018-01-22 21:30:53 +03:00
match = anyCompRegexp.exec(rawText);
2017-10-12 00:01:37 +03:00
}
2018-01-22 21:30:53 +03:00
const splitCompRegexp = new RegExp(COMPONENT_REGEXP.replace('{component}', '.*?'), 'mi');
const htmlParts = rawText.split(splitCompRegexp);
const res: any[] = [];
2017-10-12 00:01:37 +03:00
for (let i = 0; i < htmlParts.length; i++) {
const htmlPart = htmlParts[i];
if (htmlPart) {
res.push(this.renderMd(htmlPart, raw));
}
if (componentDefs[i]) {
const { componentName, attrs } = parseComponent(componentDefs[i]);
2018-01-22 21:30:53 +03:00
if (!componentName) {
continue;
}
2017-10-12 00:01:37 +03:00
res.push({
2017-11-23 00:38:38 +03:00
...components[componentName],
2018-01-22 21:30:53 +03:00
attrs,
2017-10-12 00:01:37 +03:00
});
}
}
return res;
}
}
function parseComponent(
htmlTag: string,
): {
componentName?: string;
attrs: any;
} {
const match = /<([\w_-]+).*?>/.exec(htmlTag);
2018-01-22 21:30:53 +03:00
if (match === null || match.length <= 1) {
return { componentName: undefined, attrs: {} };
}
2017-10-12 00:01:37 +03:00
const componentName = match[1];
return {
componentName,
attrs: {}, // TODO
};
}