1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

Add canonical option, fix base tag for Safari

This commit is contained in:
Fred Chasen 2017-09-21 22:10:58 -07:00
parent ec0325f122
commit 9f0623b798
6 changed files with 42 additions and 11 deletions

View file

@ -1,6 +1,6 @@
{
"name": "epubjs",
"version": "0.3.52",
"version": "0.3.53",
"description": "Parse and Render Epubs",
"main": "lib/index.js",
"module": "src/index.js",

View file

@ -46,7 +46,8 @@ class Book {
requestCredentials: undefined,
requestHeaders: undefined,
encoding: undefined,
replacements: undefined
replacements: undefined,
canonical: undefined
});
extend(this.settings, options);
@ -320,6 +321,27 @@ class Book {
return resolved;
}
/**
* Get a canonical link to a path
* @param {string} path
* @return {string} the canonical path string
*/
canonical(path) {
var url = path;
if (!path) {
return "";
}
if (this.settings.canonical) {
url = this.settings.canonical(path);
} else {
url = this.resolve(path, true);
}
return url;
}
/**
* Determine the type of they input passed to open
* @private
@ -369,13 +391,13 @@ class Book {
unpack(opf) {
this.package = opf;
this.spine.unpack(this.package, this.resolve.bind(this));
this.spine.unpack(this.package, this.resolve.bind(this), this.canonical.bind(this));
this.resources = new Resources(this.package.manifest, {
archive: this.archive,
resolver: this.resolve.bind(this),
request: this.request.bind(this),
replacements: this.settings.replacements || "base64"
replacements: this.settings.replacements || (this.archived ? "blobUrl" : "base64")
});
this.loadNavigation(this.package).then(() => {

View file

@ -352,11 +352,11 @@ class IframeView {
var link = this.document.querySelector("link[rel='canonical']");
if (link) {
link.setAttribute("href", this.section.url);
link.setAttribute("href", this.section.canonical);
} else {
link = this.document.createElement("link");
link.setAttribute("rel", "canonical");
link.setAttribute("href", this.section.url);
link.setAttribute("href", this.section.canonical);
this.document.querySelector("head").appendChild(link);
}

View file

@ -18,6 +18,7 @@ class Section {
this.index = item.index;
this.href = item.href;
this.url = item.url;
this.canonical = item.canonical;
this.next = item.next;
this.prev = item.prev;

View file

@ -37,7 +37,7 @@ class Spine {
* @param {Package} _package
* @param {method} resolver URL resolver
*/
unpack(_package, resolver) {
unpack(_package, resolver, canonical) {
this.items = _package.spine;
this.manifest = _package.manifest;
@ -54,11 +54,14 @@ class Spine {
if (item.href) {
item.url = resolver(item.href, true);
item.canonical = canonical(item.href);
}
if(manifestItem) {
item.href = manifestItem.href;
item.url = resolver(item.href, true);
item.canonical = canonical(item.href);
if(manifestItem.properties.length){
item.properties.push.apply(item.properties, manifestItem.properties);
}

View file

@ -5,13 +5,13 @@ import Path from "./path";
export function replaceBase(doc, section){
var base;
var head;
var url = section.url;
var absolute = (url.indexOf("://") > -1);
if(!doc){
return;
}
// head = doc.querySelector("head");
// base = head.querySelector("base");
head = qs(doc, "head");
base = qs(head, "base");
@ -20,13 +20,18 @@ export function replaceBase(doc, section){
head.insertBefore(base, head.firstChild);
}
base.setAttribute("href", section.url);
// Fix for Safari crashing if the url doesn't have an origin
if (!absolute && window && window.location) {
url = window.location.origin + url;
}
base.setAttribute("href", url);
}
export function replaceCanonical(doc, section){
var head;
var link;
var url = section.url; // window.location.origin + window.location.pathname + "?loc=" + encodeURIComponent(section.url);
var url = section.canonical;
if(!doc){
return;