This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
166 lines (143 loc) · 4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Expose `gingkoImport()`.
*/
module.exports = gingkoImport;
var _ = require('underscore');
/**
* Creates reasonable chunks from the md tokens containing the md text and depth information
*
* @param tokens
* @returns {Array} of {content, paragraphs, [depth]} objects
*/
function as_blocks(tokens) {
var blocks = [];
/**
* Get or create the last block and append text as a new paragraph (will be processed at a later step)
* @param text
*/
function add_paragraph_to_block(text) {
if (!blocks.length) {
blocks.push({depth: 1, header: "", paragraphs: []})
}
_.last(blocks).paragraphs.push(text);
}
_.each(tokens, function(t) {
switch (t.type) {
case 'heading':
blocks.push({depth: t.depth, header: t.src, paragraphs: []});
break;
case 'paragraph':
case 'code':
case 'list_end':
if (!t.src) {
console.warn("Empty src for token: " + t.type);
}
add_paragraph_to_block(t.src);
break;
case 'list_item_start':
case 'loose_item_start':
case 'text':
case 'list_item_end':
case 'list_start':
if (t.src) {
console.warn("Non-empty src for ignored token: " + t.type);
}
break;
default:
console.warn("Unknown marked token: " + t.type);
if (t.text) {
add_paragraph_to_block(t.text)
}
}
});
return blocks;
}
/**
* Creates a ginkgo-tree-like nested structure from the flat block array
* @param {Array} blocks
* @returns {Array}
*/
function nested_blocks(blocks) {
var acc = [];
var depth = 1;
_nested_blocks(blocks, acc, depth);
return acc;
}
function _nested_blocks(blocks, acc, depth) {
while (blocks.length) {
var block = _.head(blocks);
if (block.depth == depth) {
acc.push(block);
blocks = _.tail(blocks);
} else if (block.depth > depth) {
// go in
if (!acc.length) {
acc.push({depth: depth, header: '', paragraphs: []});
}
var last = _.last(acc);
last.children = [];
blocks = _nested_blocks(blocks, last.children, depth + 1);
} else {
// go out
return blocks;
}
}
return blocks; //empty by now
}
/**
* Creates the final gingko json structure from the nested_blocks
*
* Decides on which paragraphs to include inline (eg one paragraph after header) and what to render as sub-nodes.
*
* @param nested_blocks
* @returns {Array} - gingko JSON
*/
function nested_blocks_to_gingko(nested_blocks) {
return _.map(nested_blocks, function(block) {
var content = block.header;
var inline_paragraphs = block.children && block.children.length || (block.paragraphs && block.paragraphs.length <= 1);
if (inline_paragraphs) {
content += block.paragraphs.join("");
} else {
block.children = paragraphs_as_blocks(block.paragraphs);
}
var o = {content: content.trimRight()};
if (block.children && block.children.length) {
o.children = nested_blocks_to_gingko(block.children);
}
return o;
})
}
function paragraphs_as_blocks(paragraphs) {
return _.map(paragraphs, function(p) {
return {header: p.trimRight()}
})
}
gingko_from_marked_tokens = _.compose(nested_blocks_to_gingko, nested_blocks, as_blocks);
/**
* Convert markdown `text` to gingko tree json represenation.
*
* @param {String} text - markdown text
* @return {Array} - json represenation of gingko tree
* @api public
*/
function gingkoImport(text) {
var marked = require('./lib/marked');
var marked_options = {
gfm: true
};
var tokens = marked.lexer(text, marked_options);
// Token list example = [
// { type: 'heading', depth: 1, text: 'Alien' },
// { type: 'paragraph', text: 'The small crew of a deep space ...' },
// { type: 'heading', depth: 3, text: 'Final Image' }
//]
var gingko_json = gingko_from_marked_tokens(tokens);
if (!gingko_json.length) {
// special case - an empty gingko tree should have an empty block
return [
{ content: '' }
];
}
return gingko_json;
}