Skip to content

Commit

Permalink
Merge pull request #682 from futurepress/landmarks
Browse files Browse the repository at this point in the history
 Add landmarks to Navigation
  • Loading branch information
fchasen authored Nov 6, 2017
2 parents 68956ab + 3f16c74 commit 0ef714a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 83 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ node_js:
- "node"
notifications:
email: false
sudo: false
addons:
chrome: stable
cache:
directories:
- node_modules
script:
- npm test
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ module.exports = function(config) {

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
browsers: ['ChromeHeadless'],


// Continuous Integration mode
Expand Down
96 changes: 64 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test": "test"
},
"scripts": {
"test": "./node_modules/.bin/karma start --single-run --browsers PhantomJS",
"test": "./node_modules/.bin/karma start --single-run --browsers ChromeHeadless",
"documentation": "./node_modules/.bin/gulp docs",
"lint": "./node_modules/.bin/eslint -c .eslintrc.js src; exit 0",
"start": "./node_modules/.bin/webpack-dev-server --inline --d",
Expand Down Expand Up @@ -51,12 +51,13 @@
"jquery": "^3.2.1",
"jshint": "^2.9.5",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.4",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4",
"mocha": "^3.5.0",
"mocha": "^3.5.3",
"mocha-loader": "^1.1.1",
"morgan": "^1.8.2",
"optimist": "^0.6.1",
Expand Down
7 changes: 5 additions & 2 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Book {
/**
* @property {PageList} pagelist
*/
this.pageList = new PageList();
this.pageList = undefined;

/**
* @property {Url} url
Expand Down Expand Up @@ -441,11 +441,14 @@ class Book {
let navPath = opf.navPath || opf.ncxPath;
let toc = opf.toc;

// From json manifest
if (toc) {
return new Promise((resolve, reject) => {
this.navigation = new Navigation(toc);

this.pageList = new PageList(); // TODO: handle page lists
if (opf.pageList) {
this.pageList = new PageList(opf.pageList); // TODO: handle page lists from Manifest
}

resolve(this.navigation);
});
Expand Down
76 changes: 75 additions & 1 deletion src/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Navigation {
this.toc = [];
this.tocByHref = {};
this.tocById = {};

this.landmarks = [];
this.landmarksByType = {};

this.length = 0;
if (xml) {
this.parse(xml);
Expand All @@ -33,6 +37,7 @@ class Navigation {
this.toc = this.load(xml);
} else if(html) {
this.toc = this.parseNav(xml);
this.landmarks = this.parseLandmarks(xml);
} else if(ncx){
this.toc = this.parseNcx(xml);
}
Expand Down Expand Up @@ -92,7 +97,25 @@ class Navigation {
}

/**
* Parse from a Epub > 3.0 Nav
* Get a landmark by type
* List of types: https://idpf.github.io/epub-vocabs/structure/
* @param {string} type
* @return {object} landmarkItems
*/
landmark(type) {
var index;

if(!type) {
return this.landmarks;
}

index = this.landmarksByType[type];

return this.landmarks[index];
}

/**
* Parse toc from a Epub > 3.0 Nav
* @private
* @param {document} navHtml
* @return {array} navigation list
Expand Down Expand Up @@ -164,6 +187,57 @@ class Navigation {
};
}

/**
* Parse landmarks from a Epub > 3.0 Nav
* @private
* @param {document} navHtml
* @return {array} landmarks list
*/
parseLandmarks(navHtml){
var navElement = querySelectorByType(navHtml, "nav", "landmarks");
var navItems = navElement ? qsa(navElement, "li") : [];
var length = navItems.length;
var i;
var list = [];
var item;

if(!navItems || length === 0) return list;

for (i = 0; i < length; ++i) {
item = this.landmarkItem(navItems[i]);
if (item) {
list.push(item);
this.landmarksByType[item.type] = i;
}
}

return list;
}

/**
* Create a landmarkItem
* @private
* @param {element} item
* @return {object} landmarkItem
*/
landmarkItem(item){
let content = filterChildren(item, "a", true);

if (!content) {
return;
}

let type = content.getAttributeNS("http://www.idpf.org/2007/ops", "type") || undefined;
let href = content.getAttribute("href") || "";
let text = content.textContent || "";

return {
"href": href,
"label": text,
"type" : type
};
}

/**
* Parse from a Epub > 3.0 NC
* @private
Expand Down
Loading

0 comments on commit 0ef714a

Please sign in to comment.