Skip to content

Commit 18d3a3a

Browse files
cherifGsoulchasenlehara
authored andcommitted
Generate better page titles (#538)
Fixes bit-docs/bit-docs-generate-html#44
1 parent 6b467b9 commit 18d3a3a

File tree

2 files changed

+93
-7
lines changed

2 files changed

+93
-7
lines changed

static/canjs.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ var can = require("can-namespace");
1111
// exposes canjs stuff so widgets can use it.
1212
window.can = can;
1313

14+
// helpers
15+
var ucfirst = function (str) {
16+
return str.charAt(0).toUpperCase() + str.slice(1);
17+
};
18+
19+
var getParentModule = function(docObject) {
20+
if (docObject.type === "module") {
21+
return docObject;
22+
} else {
23+
return getParentModule(docObject.parentPage);
24+
}
25+
};
26+
1427
// state
1528
var $articleContainer,
1629
$onThisPage,
@@ -292,13 +305,40 @@ function setPathPrefix(){
292305
}
293306
}
294307

295-
function setDocTitle() {
296-
var title = window.docObject.title || window.docObject.name;
308+
function setDocTitle(docObject) {
309+
var title = docObject.title || docObject.name || "CanJS";
297310
if (title.toLowerCase() === 'canjs') {
298-
document.title = title;
311+
return title;
312+
}
313+
if (docObject.type !== "page") {
314+
var group, groupParentPage;
315+
if (docObject.type === "module") {
316+
group = docObject.parentPage;
317+
} else {
318+
var parentModule = getParentModule(docObject);
319+
group = parentModule.parentPage;
320+
title += " | " + (parentModule.title || parentModule.name);
321+
}
322+
323+
groupParentPage = group.parentPage;
324+
if (groupParentPage.type === "module") {
325+
//handle sub-modules paths, eg. can-connect
326+
title += " | " + (groupParentPage.title || groupParentPage.name);
327+
if (groupParentPage.parentPage.type === "group") {
328+
title += " | " + groupParentPage.parentPage.title + " | " + groupParentPage.parentPage.parentPage.title;
329+
}
330+
} else {
331+
title += " | " + group.title + " | " + groupParentPage.title;
332+
}
333+
title += " | CanJS";
299334
} else {
300-
document.title = 'CanJS - ' + title;
335+
var parentPage = docObject.parentPage;
336+
while(parentPage) {
337+
title += " | " + ucfirst(parentPage.title);
338+
parentPage = parentPage.parentPage;
339+
}
301340
}
341+
document.title = title;
302342
}
303343

304344
// Set the scroll position until user scrolls or navigates away.
@@ -436,7 +476,7 @@ function navigate(href, updateLocation) {
436476
}
437477

438478
init();
439-
setDocTitle();
479+
setDocTitle(sidebarViewModel.selectedPage);
440480

441481
if(searchControl.searchResultsCache){
442482
searchControl.renderSearchResults(searchControl.searchResultsCache);

templates/helpers.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ var path = require("path");
33
var escapeHTML = require("escape-html");
44
var unescapeHTML = require("unescape-html");
55

6+
//helpers
7+
var ucfirst = function(str) {
8+
return str.charAt(0).toUpperCase() + str.slice(1);
9+
};
10+
611
module.exports = function(docMap, options, getCurrent, helpers, OtherHandlebars){
712
// create children lookup
813
var childrenMap = makeChildrenMap(docMap);
@@ -164,12 +169,53 @@ module.exports = function(docMap, options, getCurrent, helpers, OtherHandlebars)
164169
return docObject.package.repository.github || docObject.package.repository.url || docObject.package.repository;
165170
}
166171
},
167-
getDocumentTitle: function(docObject){
172+
getDocumentTitle: function(docObject) {
168173
var title = docMapInfo.getTitle(docObject) || 'CanJS';
169174
if (docObject.name === 'canjs' || (title && title.toLowerCase() === 'canjs')) {
170175
return title;
171176
}
172-
return 'CanJS - ' + title;
177+
178+
if (docObject.type !== "page") {
179+
var group;
180+
if (docObject.type === "module") {
181+
group = docMap[this.parent];
182+
} else {
183+
var parentModule = docMap[docObject.parent];
184+
while(parentModule && parentModule.type !== "module") {
185+
parentModule = docMap[parentModule.parent];
186+
}
187+
if (parentModule) {
188+
group = docMap[parentModule.parent];
189+
title += " | " + (parentModule.title || parentModule.name);
190+
}
191+
}
192+
if(group) {
193+
var groupParent = docMap[group.parent];
194+
if (groupParent) {
195+
if (groupParent.type === "module") {
196+
title += " | " + (groupParent.title || groupParent.name);
197+
if (docMap[groupParent.parent].type === "group") {
198+
var groupGrandParent = docMap[groupParent.parent];
199+
var groupGrandParentParent = docMap[groupGrandParent.parent];
200+
201+
title += " | " + groupGrandParent.title + " | " + groupGrandParentParent.title;
202+
}
203+
} else {
204+
title += " | " + group.title + " | " + groupParent.title;
205+
}
206+
} else {
207+
title += " | " + group.title;
208+
}
209+
}
210+
title += " | CanJS";
211+
} else {
212+
var parentPage = docMap[docObject.parent];
213+
while(parentPage) {
214+
title += " | " + ucfirst(parentPage.title);
215+
parentPage = docMap[parentPage.parent];
216+
}
217+
}
218+
return title;
173219
},
174220
isGroup: function(docObject){
175221
return docMapInfo.isGroup(docObject);

0 commit comments

Comments
 (0)