Skip to content
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

fix: navigation not work in diff folders #3

Merged
merged 1 commit into from
Mar 22, 2024
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
8 changes: 4 additions & 4 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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;
});
Expand Down
13 changes: 10 additions & 3 deletions src/navigation.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand All @@ -14,6 +15,11 @@ class Navigation {
this.landmarksByType = {};

this.length = 0;

if (navPath) {
this.tocPath = new Path(navPath);
}

if (xml) {
this.parse(xml);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand Down
11 changes: 11 additions & 0 deletions src/utils/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down