parse-epub
Advanced tools
Comparing version 4.1.0 to 4.1.1
@@ -9,3 +9,4 @@ import fs from 'fs'; | ||
export const fixedLayoutOpf = parser.xml2js(fs.readFileSync(`${__dirname}/fixed-layout.opf`, 'utf-8')); | ||
export const nestedTocHtml = parser.xml2js(fs.readFileSync(`${__dirname}/nested-toc.xhtml`, 'utf-8')); | ||
export const testSmil = parser.xml2js(fs.readFileSync(`${__dirname}/test.smil`, 'utf-8')) | ||
export const tocHtml = parser.xml2js(fs.readFileSync(`${__dirname}/toc.xhtml`, 'utf-8')); |
@@ -1,2 +0,2 @@ | ||
import { contentOpf, tocHtml } from './data.js'; | ||
import { contentOpf, nestedTocHtml, tocHtml } from './data.js'; | ||
import extractManifest from '../extract/manifest.js'; | ||
@@ -11,1 +11,7 @@ import extractSpine from '../extract/spine.js'; | ||
}); | ||
test('#extractNestedToc', () => { | ||
const manifest = extractManifest(contentOpf) | ||
const spine = extractSpine(contentOpf) | ||
expect(extractToc(nestedTocHtml, manifest, spine)).toMatchSnapshot(); | ||
}); |
@@ -6,6 +6,6 @@ import { join, getDirname } from '../path-helpers.js'; | ||
// Spec says we need to select the TOC using the epub:type=toc property | ||
const TAG = 'nav[epub\\\:type~=toc]'; | ||
const NAV_ATTRIBUTE = 'epub:type'; | ||
const NAV_VALUE = 'toc'; | ||
export const ROOT = '__root__'; | ||
// TODO do the same as manifest | ||
export default function toc(tocHtml, manifest, spine) { | ||
@@ -15,6 +15,9 @@ const byId = {}; | ||
const items = []; | ||
const tocRoot = findTocRoot(tocHtml.html.body); | ||
if (!tocRoot) { | ||
throw 'The root node of your navigation document (table of contents) could not be found. Please ensure the file contains a nav element with an attribute of epub:type="toc"'; | ||
} | ||
const tocItem = getTocItem(manifest); | ||
const tocItemPath = getDirname(tocItem.href); | ||
parse(tocHtml.html.body.nav, ROOT); | ||
parse(tocRoot, ROOT); | ||
@@ -71,4 +74,26 @@ function parse(rootNode, id, href, label, parentId, level=0) { | ||
function findTocRoot(currentNode) { | ||
let found = false; | ||
Object.keys(currentNode).find(key => { | ||
if (isRootNode(currentNode[key])) { | ||
found = currentNode[key]; | ||
return true; | ||
} | ||
if (typeof currentNode[key] === 'object') { | ||
return found = findTocRoot(currentNode[key]); | ||
} | ||
}); | ||
return found; | ||
} | ||
function isRootNode(node) { | ||
if (typeof node !== 'undefined') { | ||
return (node.hasOwnProperty(NAV_ATTRIBUTE) && node[NAV_ATTRIBUTE] === NAV_VALUE && node.hasOwnProperty('ol')); | ||
} else { | ||
return false; | ||
} | ||
} | ||
// TODO | ||
// - page-progression-direction | ||
// https://github.com/dariocravero/readium-js/blob/master/src/epub/package-document-parser.js#L68 |
33
lib.js
@@ -362,5 +362,7 @@ 'use strict'; | ||
// Spec says we need to select the TOC using the epub:type=toc property | ||
var NAV_ATTRIBUTE = 'epub:type'; | ||
var NAV_VALUE = 'toc'; | ||
var ROOT = '__root__'; | ||
// TODO do the same as manifest | ||
function toc(tocHtml, manifest, spine) { | ||
@@ -370,6 +372,9 @@ var byId = {}; | ||
var items = []; | ||
var tocRoot = findTocRoot(tocHtml.html.body); | ||
if (!tocRoot) { | ||
throw 'The root node of your navigation document (table of contents) could not be found. Please ensure the file contains a nav element with an attribute of epub:type="toc"'; | ||
} | ||
var tocItem = getTocItem(manifest); | ||
var tocItemPath = getDirname(tocItem.href); | ||
parse(tocHtml.html.body.nav, ROOT); | ||
parse(tocRoot, ROOT); | ||
@@ -428,2 +433,24 @@ function parse(rootNode, id, href, label, parentId, level) { | ||
function findTocRoot(currentNode) { | ||
var found = false; | ||
Object.keys(currentNode).find(function (key) { | ||
if (isRootNode(currentNode[key])) { | ||
found = currentNode[key]; | ||
return true; | ||
} | ||
if (typeof currentNode[key] === 'object') { | ||
return found = findTocRoot(currentNode[key]); | ||
} | ||
}); | ||
return found; | ||
} | ||
function isRootNode(node) { | ||
if (typeof node !== 'undefined') { | ||
return (node.hasOwnProperty(NAV_ATTRIBUTE) && node[NAV_ATTRIBUTE] === NAV_VALUE && node.hasOwnProperty('ol')); | ||
} else { | ||
return false; | ||
} | ||
} | ||
// TODO | ||
@@ -430,0 +457,0 @@ // - page-progression-direction |
@@ -38,3 +38,3 @@ { | ||
}, | ||
"version": "4.1.0" | ||
"version": "4.1.1" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
504894
46
1187