2017-10-12 00:01:37 +03:00
|
|
|
import * as Remarkable from 'remarkable';
|
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';
|
2018-02-07 23:54:55 +03:00
|
|
|
import { SECTION_ATTR } from './MenuStore';
|
|
|
|
import slugify from 'slugify';
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
const md = new Remarkable('default', {
|
|
|
|
html: true,
|
|
|
|
linkify: true,
|
|
|
|
breaks: false,
|
|
|
|
typographer: false,
|
|
|
|
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 {
|
2018-02-07 23:54:55 +03:00
|
|
|
id: string;
|
2017-10-12 00:01:37 +03:00
|
|
|
name: string;
|
2018-02-07 23:54:55 +03:00
|
|
|
items?: MarkdownHeading[];
|
|
|
|
description?: string;
|
2018-01-22 21:30:53 +03:00
|
|
|
}
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
export class MarkdownRenderer {
|
2018-02-07 23:54:55 +03:00
|
|
|
headings: MarkdownHeading[] = [];
|
|
|
|
currentTopHeading: MarkdownHeading;
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
private _origRules: any = {};
|
|
|
|
|
|
|
|
saveOrigRules() {
|
|
|
|
this._origRules.open = md.renderer.rules.heading_open;
|
|
|
|
this._origRules.close = md.renderer.rules.heading_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreOrigRules() {
|
|
|
|
md.renderer.rules.heading_open = this._origRules.open;
|
|
|
|
md.renderer.rules.heading_close = this._origRules.close;
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:54:55 +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-07 23:54:55 +03:00
|
|
|
};
|
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);
|
2018-02-07 23:54:55 +03:00
|
|
|
res.push(...this.flattenHeadings(heading.items));
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:54:55 +03:00
|
|
|
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;
|
2018-02-07 23:54:55 +03:00
|
|
|
prevHeading.description = html2Str(rawText.substring(prevPos, currentPos));
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
prevHeading = heading;
|
|
|
|
prevPos = currentPos;
|
|
|
|
}
|
2018-02-07 23:54:55 +03:00
|
|
|
prevHeading.description = html2Str(rawText.substring(prevPos));
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
headingOpenRule = (tokens, idx) => {
|
|
|
|
if (tokens[idx].hLevel > 2) {
|
|
|
|
return this._origRules.open(tokens, idx);
|
|
|
|
} else {
|
2018-01-22 21:30:53 +03:00
|
|
|
const content = tokens[idx + 1].content;
|
2017-10-12 00:01:37 +03:00
|
|
|
if (tokens[idx].hLevel === 1) {
|
|
|
|
this.currentTopHeading = this.saveHeading(content);
|
2018-01-22 21:30:53 +03:00
|
|
|
const id = this.currentTopHeading.id;
|
2017-10-12 00:01:37 +03:00
|
|
|
return (
|
|
|
|
`<a name="${id}"></a>` +
|
2018-01-10 19:23:14 +03:00
|
|
|
`<h${tokens[idx].hLevel} ${SECTION_ATTR}="${id}" id="${id}">` +
|
2017-10-12 00:01:37 +03:00
|
|
|
`<a class="share-link" href="#${id}"></a>`
|
|
|
|
);
|
|
|
|
} else if (tokens[idx].hLevel === 2) {
|
2018-01-29 11:51:45 +03:00
|
|
|
const { id } = this.saveHeading(
|
|
|
|
content,
|
|
|
|
this.currentTopHeading && this.currentTopHeading.items,
|
|
|
|
);
|
2017-10-12 00:01:37 +03:00
|
|
|
return (
|
|
|
|
`<a name="${id}"></a>` +
|
2018-01-10 19:23:14 +03:00
|
|
|
`<h${tokens[idx].hLevel} ${SECTION_ATTR}="${id}" id="${id}">` +
|
2017-10-12 00:01:37 +03:00
|
|
|
`<a class="share-link" href="#${id}"></a>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
headingCloseRule = (tokens, idx) => {
|
|
|
|
if (tokens[idx].hLevel > 2) {
|
|
|
|
return this._origRules.close(tokens, idx);
|
|
|
|
} else {
|
|
|
|
return `</h${tokens[idx].hLevel}>\n`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderMd(rawText: string, raw: boolean = true): string {
|
|
|
|
if (!raw) {
|
|
|
|
this.saveOrigRules();
|
|
|
|
md.renderer.rules.heading_open = this.headingOpenRule;
|
|
|
|
md.renderer.rules.heading_close = this.headingCloseRule;
|
|
|
|
}
|
|
|
|
|
2018-01-22 21:30:53 +03:00
|
|
|
const text = rawText;
|
2017-10-12 00:01:37 +03:00
|
|
|
|
2018-01-22 21:30:53 +03:00
|
|
|
const res = md.render(text);
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
if (!raw) {
|
|
|
|
this.restoreOrigRules();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:54:55 +03:00
|
|
|
extractHeadings(rawText: string): MarkdownHeading[] {
|
|
|
|
const md = this.renderMd(rawText, false);
|
|
|
|
this.attachHeadingsDescriptions(md);
|
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
|
|
|
|
};
|
|
|
|
}
|