Skip to content

Commit

Permalink
feat(render): #12 add basic list parser
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 3, 2020
1 parent 8418bdf commit 4af00e0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class LedgeRadarComponent implements OnInit, AfterViewInit {
}

private buildOption(data) {
console.log(data);
let indicator: any[] = data.children;

let legend: any[] = [data.name];
Expand Down
61 changes: 59 additions & 2 deletions src/app/shared/ledge-render/ledge-render.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class LedgeRenderComponent implements OnInit, AfterViewInit, OnChanges {
@Input()
content: string;
markdownData: any[] = [];
token: null;
token = null;
tokens: TokensList | any = [];

constructor() {}
Expand Down Expand Up @@ -55,6 +55,16 @@ export class LedgeRenderComponent implements OnInit, AfterViewInit, OnChanges {
return this.tokens[this.tokens.length - 1] || 0;
}

parseText() {
let body = this.token.text;

while (this.peek().type === 'text') {
body += '\n' + (this.next() as any).text;
}

return body;
}

private tok() {
const token: Token = this.token;
switch (token.type) {
Expand All @@ -65,7 +75,7 @@ export class LedgeRenderComponent implements OnInit, AfterViewInit, OnChanges {
this.handleCode(token);
break;
case 'space':
break;
return '';
case 'blockquote_start':
let body = '';
while (this.next().type !== 'blockquote_end') {
Expand All @@ -85,6 +95,53 @@ export class LedgeRenderComponent implements OnInit, AfterViewInit, OnChanges {
text: inline,
});
break;
case 'list_start': {
const listBody = [];
const ordered = this.token.ordered;
const start = this.token.start;

while (this.next().type !== 'list_end') {
listBody.push(this.tok());
}

return { body: listBody, ordered, start };
}
case 'list_item_start': {
const itemBody = {
name: '',
children: [],
};
const loose = this.token.loose;
const checked = this.token.checked;
const task = this.token.task;
//
// if (this.token.task) {
// if (loose) {
// if (this.peek().type === 'text') {
// const nextToken = this.peek();
// nextToken.text = this.renderer.checkbox(checked) + ' ' + nextToken.text;
// } else {
// this.tokens.push({
// type: 'text',
// text: this.renderer.checkbox(checked)
// });
// }
// } else {
// body += this.renderer.checkbox(checked);
// }
// }

while (this.next().type !== 'list_item_end') {
if (!loose && this.token.type === 'text') {
itemBody.name += this.parseText();
} else {
itemBody.children = this.tok();
}
}

// return this.renderer.listitem(body, task, checked);
return { body: itemBody, name: itemBody.name, task, checked };
}
case 'hr':
this.markdownData.push(token);
break;
Expand Down

0 comments on commit 4af00e0

Please sign in to comment.