chore: refactor MarkdownRenderer.extractHeadings

This commit is contained in:
Roman Hotsiy 2018-02-07 22:54:55 +02:00
parent 84d1c7b2f2
commit 32cd670a89
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
2 changed files with 32 additions and 20 deletions

View File

@ -2,8 +2,8 @@ import * as Remarkable from 'remarkable';
import { MDComponent } from '../components/Markdown/Markdown'; import { MDComponent } from '../components/Markdown/Markdown';
import { highlight, html2Str } from '../utils'; import { highlight, html2Str } from '../utils';
import { IMenuItem, SECTION_ATTR } from './MenuStore'; import { SECTION_ATTR } from './MenuStore';
import { GroupModel } from './models'; import slugify from 'slugify';
const md = new Remarkable('default', { const md = new Remarkable('default', {
html: true, html: true,
@ -21,14 +21,15 @@ export function buildComponentComment(name: string) {
} }
interface MarkdownHeading { interface MarkdownHeading {
id: string;
name: string; name: string;
children?: MarkdownHeading[]; items?: MarkdownHeading[];
content?: string; description?: string;
} }
export class MarkdownRenderer { export class MarkdownRenderer {
headings: GroupModel[] = []; headings: MarkdownHeading[] = [];
currentTopHeading: GroupModel; currentTopHeading: MarkdownHeading;
private _origRules: any = {}; private _origRules: any = {};
@ -42,11 +43,11 @@ export class MarkdownRenderer {
md.renderer.rules.heading_close = this._origRules.close; md.renderer.rules.heading_close = this._origRules.close;
} }
saveHeading(name: string, container: IMenuItem[] = this.headings): GroupModel { saveHeading(name: string, container: MarkdownHeading[] = this.headings): MarkdownHeading {
const item = new GroupModel('section', { const item = {
id: 'section' + '/' + slugify(name),
name, name,
}); };
item.depth = 1;
container.push(item); container.push(item);
return item; return item;
} }
@ -58,13 +59,14 @@ export class MarkdownRenderer {
const res: MarkdownHeading[] = []; const res: MarkdownHeading[] = [];
for (const heading of container) { for (const heading of container) {
res.push(heading); res.push(heading);
res.push(...this.flattenHeadings(heading.children)); res.push(...this.flattenHeadings(heading.items));
} }
return res; return res;
} }
attachHeadingsContent(rawText: string) { attachHeadingsDescriptions(rawText: string) {
const buildRegexp = heading => new RegExp(`<h\\d ${SECTION_ATTR}="section/${heading.id}">`); const buildRegexp = heading =>
new RegExp(`<h\\d ${SECTION_ATTR}="${heading.id}" id="${heading.id}">`);
const flatHeadings = this.flattenHeadings(this.headings); const flatHeadings = this.flattenHeadings(this.headings);
if (flatHeadings.length < 1) { if (flatHeadings.length < 1) {
@ -76,12 +78,12 @@ export class MarkdownRenderer {
for (let i = 1; i < flatHeadings.length; i++) { for (let i = 1; i < flatHeadings.length; i++) {
const heading = flatHeadings[i]; const heading = flatHeadings[i];
const currentPos = rawText.substr(prevPos + 1).search(buildRegexp(heading)) + prevPos + 1; const currentPos = rawText.substr(prevPos + 1).search(buildRegexp(heading)) + prevPos + 1;
prevHeading.content = html2Str(rawText.substring(prevPos, currentPos)); prevHeading.description = html2Str(rawText.substring(prevPos, currentPos));
prevHeading = heading; prevHeading = heading;
prevPos = currentPos; prevPos = currentPos;
} }
prevHeading.content = html2Str(rawText.substring(prevPos)); prevHeading.description = html2Str(rawText.substring(prevPos));
} }
headingOpenRule = (tokens, idx) => { headingOpenRule = (tokens, idx) => {
@ -130,16 +132,15 @@ export class MarkdownRenderer {
const res = md.render(text); const res = md.render(text);
this.attachHeadingsContent(res);
if (!raw) { if (!raw) {
this.restoreOrigRules(); this.restoreOrigRules();
} }
return res; return res;
} }
extractHeadings(rawText: string): GroupModel[] { extractHeadings(rawText: string): MarkdownHeading[] {
this.renderMd(rawText, false); const md = this.renderMd(rawText, false);
this.attachHeadingsDescriptions(md);
const res = this.headings; const res = this.headings;
this.headings = []; this.headings = [];
return res; return res;

View File

@ -56,7 +56,18 @@ export class MenuBuilder {
static addMarkdownItems(description: string): ContentItemModel[] { static addMarkdownItems(description: string): ContentItemModel[] {
const renderer = new MarkdownRenderer(); const renderer = new MarkdownRenderer();
const headings = renderer.extractHeadings(description || ''); const headings = renderer.extractHeadings(description || '');
return headings;
const mapHeadingsDeep = (parent, items, depth = 1) =>
items.map(heading => {
const group = new GroupModel('section', heading, parent);
group.depth = depth;
if (heading.items) {
group.items = mapHeadingsDeep(group, group.items, depth + 1);
}
return group;
});
return mapHeadingsDeep(undefined, headings);
} }
/** /**