Skip to content

Commit 3f41f52

Browse files
committed
adds basic sidebar to default html output
1 parent 226be5d commit 3f41f52

File tree

8 files changed

+414
-13
lines changed

8 files changed

+414
-13
lines changed

build/build_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe("documentjs/lib/generators/html/build",function(){
122122
}, done);
123123
});
124124

125-
it.only("makes linked content",function(done){
125+
it("makes linked content",function(done){
126126
var options = {
127127
html: { templates: path.join(__dirname,"test","escaped") },
128128
dest: "XXXXYYYZZZ",

build/make_default_helpers.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ var _ = require("lodash"),
33
stmd_to_html = require("../stmd_to_html"),
44
deepExtendWithoutBody = require("./deep_extend_without_body"),
55
escape = require('escape-html'),
6-
striptags = require('striptags');
7-
6+
striptags = require('striptags'),
7+
DocMapInfo = require("../doc-map-info"),
8+
unescapeHTML = require("unescape-html");
89
// Helper helpers
910

1011

@@ -51,6 +52,8 @@ var linksRegExp = /[\[](.*?)\]/g,
5152
*/
5253
module.exports = function(docMap, config, getCurrent, Handlebars){
5354

55+
var docMapInfo = new DocMapInfo(docMap, getCurrent);
56+
5457
var urlTo = function(name){
5558
var currentDir = path.dirname( path.join(config.dest, docsFilename( getCurrent(), config)) );
5659
var toPath = path.join(config.dest,docsFilename( docMap[name] || name, config));
@@ -78,6 +81,73 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
7881
};
7982

8083
var helpers = {
84+
// DOC MAP HELPERS
85+
getCurrentTree: function(){
86+
return docMapInfo.getCurrentTree();
87+
},
88+
isGroup: function(docObject){
89+
return docMapInfo.isGroup(docObject);
90+
},
91+
isCurrent: function(docObject, options){
92+
return docMapInfo.isCurrent(docObject);
93+
},
94+
hasCurrent: function(docObject) {
95+
return docMapInfo.hasCurrent(docObject);
96+
},
97+
hasOrIsCurrent: function(docObject){
98+
return docMapInfo.hasOrIsCurrent(docObject);
99+
},
100+
getTitle: function(docObject){
101+
return docMapInfo.getTitle(this);
102+
},
103+
getLinkTitle: function(docObject) {
104+
var description = docObject.description || docObject.name;
105+
description = helpers.stripMarkdown(description);
106+
return unescapeHTML(description);
107+
},
108+
getShortTitle: function(docObject){
109+
return docMapInfo.getShortTitle(docObject);
110+
},
111+
sortChildren: function(children) {
112+
var ordered = [],
113+
sorted = [];
114+
115+
children.forEach(function(el) {
116+
var doc = el.docObject;
117+
if (doc && typeof doc.order === 'number') {
118+
ordered.push(el);
119+
} else {
120+
sorted.push(el);
121+
}
122+
});
123+
124+
// Sort alphabetically, "/" comes before "-"
125+
sorted.sort(function(x,y) {
126+
var a = x.docObject.name.replace(/\//g, '!'),
127+
b = y.docObject.name.replace(/\//g, '!');
128+
129+
if (a < b) {
130+
return -1;
131+
}
132+
if (a > b) {
133+
return 1;
134+
}
135+
return 0;
136+
});
137+
138+
// Sort by docObject "ordered" property
139+
ordered.sort(function(x,y) {
140+
return x.docObject.order > y.docObject.order;
141+
});
142+
143+
// Insert ordered items to their index in the alphabetical array
144+
ordered.forEach(function(el) {
145+
sorted.splice(el.docObject.order, 0, el);
146+
});
147+
148+
return sorted;
149+
},
150+
81151
// GENERIC HELPERS
82152
/**
83153
* @function documentjs.generators.html.defaultHelpers.ifEqual

doc-map-info.js

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
var DocMapInfo = function(docMap, getCurrent) {
2+
this.docMap = docMap;
3+
this.childrenMap = makeChildrenMap(docMap);
4+
this.getCurrent = getCurrent;
5+
};
6+
DocMapInfo.prototype.isCurrent = function(docObject){
7+
return docObject.name === this.getCurrent().name;
8+
};
9+
DocMapInfo.prototype.hasCurrent = function(docObject){
10+
var parents = this.getParents(this.getCurrent());
11+
parents.push(this.getCurrent());
12+
var itemMap = {};
13+
parents.forEach(function(docObject){
14+
itemMap[docObject.name] = true;
15+
});
16+
17+
return itemMap[docObject.name];
18+
};
19+
DocMapInfo.prototype.hasOrIsCurrent = function(docObject){
20+
return this.isCurrent(docObject) || this.hasCurrent(docObject);
21+
};
22+
DocMapInfo.prototype.getParents = function(docObject, cb){
23+
var names = {};
24+
25+
// walk up parents until you don't have a parent
26+
var parent = this.docMap[docObject.parent],
27+
parents = [];
28+
if(!parent) {
29+
return [];
30+
}
31+
// don't allow things that are their own parent
32+
if(parent.parent === docObject.name){
33+
return parents;
34+
}
35+
36+
while(parent){
37+
if(cb) {
38+
cb(parent);
39+
}
40+
parents.unshift(parent);
41+
if(names[parent.name]){
42+
return parents;
43+
}
44+
names[parent.name] = true;
45+
parent = this.docMap[parent.parent];
46+
}
47+
return parents;
48+
};
49+
DocMapInfo.prototype.getTitle = function(docObject) {
50+
return docObject.title || docObject.name;
51+
};
52+
53+
function getShortTitle(name, parent){
54+
if(parent && (parent.type === "module" || parent.type === "group")) {
55+
var modeletName = path.dirname(parent.name),
56+
moduleName = path.basename(parent.name);
57+
58+
if(name.indexOf( parent.name+"/" ) === 0 ) {
59+
name = name.replace(parent.name+"/", "./");
60+
}
61+
// can-util/dom/events/attributes/attributes's parent is can-util/dom/events/events
62+
else if( moduleName && modeletName.endsWith(moduleName) && name.indexOf( modeletName+"/" ) === 0 ) {
63+
name = name.replace(modeletName+"/", "./");
64+
}
65+
else {
66+
return;
67+
}
68+
69+
var basename = path.basename(name);
70+
if(name.endsWith("/"+basename+"/"+basename)) {
71+
return path.dirname(name)+"/";
72+
} else {
73+
return name;
74+
}
75+
}
76+
}
77+
DocMapInfo.prototype.getShortTitle = function(docObject) {
78+
79+
if(docObject.type === "module") {
80+
var parents = this.getParents(docObject).reverse();
81+
82+
var parentModule = parents.find(function(docObject){
83+
return docObject.type === "module";
84+
});
85+
var parentGroup = parents[0] && parents[0].type === "group" && parents[0];
86+
87+
var name = docObject.name,
88+
shortTitle;
89+
90+
if(parentGroup) {
91+
shortTitle = getShortTitle(name, parentGroup);
92+
if(shortTitle) {
93+
return shortTitle;
94+
}
95+
}
96+
97+
if(parentModule) {
98+
shortTitle = getShortTitle(name, parentModule);
99+
if(shortTitle) {
100+
return shortTitle;
101+
}
102+
}
103+
return name;
104+
} else {
105+
return this.getTitle(docObject);
106+
}
107+
108+
};
109+
DocMapInfo.prototype.isGroup = function(docObject) {
110+
return ["group","static","prototype"].indexOf(docObject.type) !== -1;
111+
};
112+
DocMapInfo.prototype.getCurrentTree = function(){
113+
// [{docObject, children<>},{docObject}]
114+
//
115+
var getChildren = this.getChildren.bind(this),
116+
getNestedDocObject = this.getNestedDocObject.bind(this);
117+
118+
var cur = this.getCurrent();
119+
120+
var curChildren = this.getNestedChildren(cur);
121+
122+
this.getParents(cur, function(docObject){
123+
curChildren = getChildren(docObject).map(function(docObject){
124+
if(docObject.name === cur.name) {
125+
return {docObject: docObject, children: curChildren};
126+
} else {
127+
return getNestedDocObject(docObject);
128+
}
129+
});
130+
cur = docObject;
131+
});
132+
133+
if(!curChildren) {
134+
return {children: []};
135+
} else {
136+
return {children: curChildren};
137+
}
138+
};
139+
DocMapInfo.prototype.getChildren = function(docObject){
140+
var children = this.childrenMap[docObject.name];
141+
return (children || []).sort(compareDocObjects);
142+
};
143+
DocMapInfo.prototype.getNestedDocObject = function(docObject){
144+
if(this.isGroup(docObject)) {
145+
return {
146+
docObject: docObject,
147+
children: this.getNestedChildren(docObject)
148+
};
149+
} else {
150+
return {docObject: docObject};
151+
}
152+
};
153+
DocMapInfo.prototype.getNestedChildren = function(docObject){
154+
return this.getChildren(docObject).map(this.getNestedDocObject.bind(this));
155+
};
156+
157+
var levelMap = ["collection","modules"];
158+
159+
function makeChildrenMap(docMap){
160+
var childrenMap = {};
161+
for(var name in docMap) {
162+
var docObject = docMap[name];
163+
var parent = docObject.parent;
164+
if(parent) {
165+
if(!childrenMap[parent]) {
166+
childrenMap[parent] = [];
167+
}
168+
childrenMap[parent].push(docObject);
169+
}
170+
}
171+
return childrenMap;
172+
}
173+
174+
175+
var compareDocObjects = function(child1, child2){
176+
177+
// put groups at the end
178+
if(/group|prototype|static/i.test(child1.type)){
179+
if(!/group|prototype|static/i.test(child2.type)){
180+
return 1;
181+
} else {
182+
if(child1.type === "prototype"){
183+
return -1;
184+
}
185+
if(child2.type === "prototype"){
186+
return 1;
187+
}
188+
if(child1.type === "static"){
189+
return -1;
190+
}
191+
if(child2.type === "static"){
192+
return 1;
193+
}
194+
195+
}
196+
}
197+
if(/prototype|static/i.test(child2.type)){
198+
return -1;
199+
}
200+
201+
if(typeof child1.order == "number"){
202+
if(typeof child2.order == "number"){
203+
// same order given?
204+
if(child1.order == child2.order){
205+
// sort by name
206+
if(child1.name < child2.name){
207+
return -1;
208+
}
209+
return 1;
210+
} else {
211+
return child1.order - child2.order;
212+
}
213+
214+
} else {
215+
return -1;
216+
}
217+
} else {
218+
if(typeof child2.order == "number"){
219+
return 1;
220+
} else {
221+
// alphabetical
222+
if(child1.name < child2.name){
223+
return -1;
224+
}
225+
return 1;
226+
}
227+
}
228+
};
229+
230+
module.exports = DocMapInfo;

0 commit comments

Comments
 (0)