diff --git a/src/book.js b/src/book.js index fb66e54b1..a99bb52b3 100644 --- a/src/book.js +++ b/src/book.js @@ -502,9 +502,9 @@ class Book { this.opening.resolve(this); }); }) - .catch((err) => { - console.error(err); - }); + .catch((err) => { + console.error(err); + }); } else { // Resolve book opened promise this.loaded.displayOptions.then(() => { @@ -547,7 +547,7 @@ class Book { return this.load(navPath, "xml") .then((xml) => { - this.navigation = new Navigation(xml); + this.navigation = new Navigation(xml, navPath); this.pageList = new PageList(xml); return this.navigation; }); diff --git a/src/navigation.js b/src/navigation.js index 801598bdc..cb43826a9 100644 --- a/src/navigation.js +++ b/src/navigation.js @@ -1,11 +1,12 @@ import {qs, qsa, querySelectorByType, filterChildren, getParentByTagName} from "./utils/core"; +import Path from "./utils/path"; /** * Navigation Parser * @param {document} xml navigation html / xhtml / ncx */ class Navigation { - constructor(xml) { + constructor(xml, navPath = "") { this.toc = []; this.tocByHref = {}; this.tocById = {}; @@ -14,6 +15,11 @@ class Navigation { this.landmarksByType = {}; this.length = 0; + + if (navPath) { + this.tocPath = new Path(navPath); + } + if (xml) { this.parse(xml); } @@ -198,7 +204,8 @@ class Navigation { return; } - let src = content.getAttribute("href") || ""; + const href = content.getAttribute("href") || ""; + let src = this.tocPath.join(href); if (!id) { id = src; @@ -317,7 +324,7 @@ class Navigation { parentNode = item.parentNode, parent; - if(parentNode && (parentNode.nodeName === "navPoint" || parentNode.nodeName.split(':').slice(-1)[0] === "navPoint")) { + if(parentNode && (parentNode.nodeName === "navPoint" || parentNode.nodeName.split(":").slice(-1)[0] === "navPoint")) { parent = parentNode.getAttribute("id"); } diff --git a/src/utils/path.js b/src/utils/path.js index 6a060cb14..1b71ab920 100644 --- a/src/utils/path.js +++ b/src/utils/path.js @@ -58,6 +58,17 @@ class Path { return (what.charAt(what.length-1) === "/"); } + /** + * Join a paths + * + * https://nodejs.org/api/path.html#pathjoinpaths + * @param {string} what + * @returns {string} joined path + */ + join (what) { + return path.join(this.directory, what); + } + /** * Resolve a path against the directory of the Path *