mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
Add landmark parsing
This commit is contained in:
parent
c977fc10d4
commit
7596f180d8
1 changed files with 53 additions and 1 deletions
|
@ -7,6 +7,7 @@ import {qs, qsa, querySelectorByType, filterChildren, getParentByTagName} from "
|
|||
class Navigation {
|
||||
constructor(xml) {
|
||||
this.toc = [];
|
||||
this.landmarks = [];
|
||||
this.tocByHref = {};
|
||||
this.tocById = {};
|
||||
this.length = 0;
|
||||
|
@ -33,6 +34,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);
|
||||
}
|
||||
|
@ -92,7 +94,7 @@ class Navigation {
|
|||
}
|
||||
|
||||
/**
|
||||
* Parse from a Epub > 3.0 Nav
|
||||
* Parse toc from a Epub > 3.0 Nav
|
||||
* @private
|
||||
* @param {document} navHtml
|
||||
* @return {array} navigation list
|
||||
|
@ -164,6 +166,56 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue