-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtreeToHtml.js
66 lines (54 loc) · 1.93 KB
/
treeToHtml.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
var youtubeAutoEmbed = require('./youtubeAutoEmbed')
module.exports = {
plain: treeToPlainHtml,
stitched: treeToStitchedHtml,
}
function treeToPlainHtml ( tree ) {
var html = html || ''
if (tree.parent == null) html = tree.content
function recurssiveStitch(tree) {
tree.children.forEach( function(childTree) {
html = plainSubstitute( childTree.url, childTree.content, html )
recurssiveStitch( childTree )
})
}
recurssiveStitch(tree)
html = youtubeAutoEmbed(html)
return html
}
// TODO ask Mikey why this didn't work
//function treeToStitchedHtml ( tree ) {
//var html = html || ''
//if (tree.parent == null) html = tree.content
//tree.children.forEach( function(childTree) {
//html = stitchSubstitute( childTree.url, childTree.content, html )
//treeToStitchedHtml( childTree )
//})
//return html
//}
function treeToStitchedHtml ( tree ) {
var html = html || ''
if (tree.parent == null) html = tree.content
function recurssiveStitch(tree) {
tree.children.forEach( function(childTree) {
html = stitchSubstitute( childTree, html )
recurssiveStitch( childTree )
})
}
recurssiveStitch(tree)
html = youtubeAutoEmbed(html)
return html
}
function plainSubstitute (url, importedText, wholeText) {
var regex = new RegExp('\\+\<a href\=(\'|\")' + url + '.*\<\/a\>', 'g')
return wholeText.replace(regex, importedText)
}
function stitchSubstitute (treeNode, wholeText) {
var regex = new RegExp('\\+\<a href\=(\'|\")' + treeNode.url + '.*\<\/a\>', 'g')
var importedText = "<div class='stitch-mark visible' data-url='" + treeNode.url + "'>" +
"<button class='collapser has-plus'></button>" +
"<div class='content expanded'>" + treeNode.content + "</div>" +
"<div class='content collapsed hidden'>" + treeNode.label + "</div>" +
"</div>"
return wholeText.replace(regex, importedText)
}