Skip to content

Generate better page titles #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions static/canjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ var can = require("can-namespace");
// exposes canjs stuff so widgets can use it.
window.can = can;

// helpers
var ucfirst = function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};

var getParentModule = function(docObject) {
if (docObject.type === "module") {
return docObject;
} else {
return getParentModule(docObject.parentPage);
}
};

// state
var $articleContainer,
$onThisPage,
Expand Down Expand Up @@ -292,13 +305,40 @@ function setPathPrefix(){
}
}

function setDocTitle() {
var title = window.docObject.title || window.docObject.name;
function setDocTitle(docObject) {
var title = docObject.title || docObject.name || "CanJS";
if (title.toLowerCase() === 'canjs') {
document.title = title;
return title;
}
if (docObject.type !== "page") {
var group, groupParentPage;
if (docObject.type === "module") {
group = docObject.parentPage;
} else {
var parentModule = getParentModule(docObject);
group = parentModule.parentPage;
title += " | " + (parentModule.title || parentModule.name);
}

groupParentPage = group.parentPage;
if (groupParentPage.type === "module") {
//handle sub-modules paths, eg. can-connect
title += " | " + (groupParentPage.title || groupParentPage.name);
if (groupParentPage.parentPage.type === "group") {
title += " | " + groupParentPage.parentPage.title + " | " + groupParentPage.parentPage.parentPage.title;
}
} else {
title += " | " + group.title + " | " + groupParentPage.title;
}
title += " | CanJS";
} else {
document.title = 'CanJS - ' + title;
var parentPage = docObject.parentPage;
while(parentPage) {
title += " | " + ucfirst(parentPage.title);
parentPage = parentPage.parentPage;
}
}
document.title = title;
}

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

init();
setDocTitle();
setDocTitle(sidebarViewModel.selectedPage);

if(searchControl.searchResultsCache){
searchControl.renderSearchResults(searchControl.searchResultsCache);
Expand Down
50 changes: 48 additions & 2 deletions templates/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ var path = require("path");
var escapeHTML = require("escape-html");
var unescapeHTML = require("unescape-html");

//helpers
var ucfirst = function(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};

module.exports = function(docMap, options, getCurrent, helpers, OtherHandlebars){
// create children lookup
var childrenMap = makeChildrenMap(docMap);
Expand Down Expand Up @@ -164,12 +169,53 @@ module.exports = function(docMap, options, getCurrent, helpers, OtherHandlebars)
return docObject.package.repository.github || docObject.package.repository.url || docObject.package.repository;
}
},
getDocumentTitle: function(docObject){
getDocumentTitle: function(docObject) {
var title = docMapInfo.getTitle(docObject) || 'CanJS';
if (docObject.name === 'canjs' || (title && title.toLowerCase() === 'canjs')) {
return title;
}
return 'CanJS - ' + title;

if (docObject.type !== "page") {
var group;
if (docObject.type === "module") {
group = docMap[this.parent];
} else {
var parentModule = docMap[docObject.parent];
while(parentModule && parentModule.type !== "module") {
parentModule = docMap[parentModule.parent];
}
if (parentModule) {
group = docMap[parentModule.parent];
title += " | " + (parentModule.title || parentModule.name);
}
}
if(group) {
var groupParent = docMap[group.parent];
if (groupParent) {
if (groupParent.type === "module") {
title += " | " + (groupParent.title || groupParent.name);
if (docMap[groupParent.parent].type === "group") {
var groupGrandParent = docMap[groupParent.parent];
var groupGrandParentParent = docMap[groupGrandParent.parent];

title += " | " + groupGrandParent.title + " | " + groupGrandParentParent.title;
}
} else {
title += " | " + group.title + " | " + groupParent.title;
}
} else {
title += " | " + group.title;
}
}
title += " | CanJS";
} else {
var parentPage = docMap[docObject.parent];
while(parentPage) {
title += " | " + ucfirst(parentPage.title);
parentPage = docMap[parentPage.parent];
}
}
return title;
},
isGroup: function(docObject){
return docMapInfo.isGroup(docObject);
Expand Down