forked from mapbox/mapbox-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
94 lines (90 loc) · 2.84 KB
/
Copy pathcontent.js
File metadata and controls
94 lines (90 loc) · 2.84 KB
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
import React from 'react';
import Section from './section';
import PureRenderMixin from 'react-pure-render/mixin';
import GithubSlugger from 'github-slugger';
import { transformURL } from '../custom';
let slugger = new GithubSlugger();
let slug = title => { slugger.reset(); return slugger.slug(title); };
var roundedToggleOptionType = React.PropTypes.shape({
title: React.PropTypes.string,
value: React.PropTypes.string
});
function chunkifyAST(ast, language) {
var preview = false;
return ast.children.reduce((chunks, node) => {
if (node.type === 'heading' && node.depth === 1) {
return chunks;
} else if (node.type === 'heading' && node.depth < 4) {
chunks.push([node]);
} else {
chunks[chunks.length - 1].push(node);
}
return chunks;
}, [[]]).filter(chunk => chunk.length)
.map(chunk => {
var left = [], right = [], title;
if (language === 'cli') {
language = 'bash';
}
if (chunk[0].depth < 3) {
preview = false;
}
chunk.forEach(node => {
if (node.type === 'code') {
if (node.lang === 'json' || node.lang === 'http' || node.lang === 'html') {
right.push(node);
} else if (node.lang === language) {
if (language === 'curl') {
right.push({ ...node, lang: 'bash' });
} else {
right.push(node);
}
} else if (node.lang === 'endpoint') {
right.push(transformURL(node.value));
} else if (node.lang === null) {
left.push(node);
}
} else if (node.type === 'heading' && node.depth >= 4) {
right.push(node);
} else if (node.type === 'blockquote') {
left.push(node);
} else if (node.type === 'heading' && node.depth < 4 && !title) {
title = node.children[0].value;
left.push(node);
} else if (node.type === 'html') {
if (node.value.indexOf('<!--') === 0) {
var content = node.value
.replace(/^<!--/, '')
.replace(/-->$/, '')
.trim();
if (content === 'preview') {
preview = true;
}
}
} else {
left.push(node);
}
});
return { left, right, title, preview, slug: slug(title) };
});
}
var Content = React.createClass({
mixins: [PureRenderMixin],
propTypes: {
ast: React.PropTypes.object.isRequired,
language: roundedToggleOptionType,
leftClassname: React.PropTypes.string.isRequired,
rightClassname: React.PropTypes.string.isRequired
},
render() {
let { ast, language, leftClassname, rightClassname } = this.props;
return (<div className='clearfix'>
{chunkifyAST(ast, language.value).map((chunk, i) => <Section
leftClassname={leftClassname}
rightClassname={rightClassname}
chunk={chunk}
key={i} />)}
</div>);
}
});
module.exports = Content;