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", "name": "epubjs",
"version": "0.3.52", "version": "0.3.53",
"description": "Parse and Render Epubs", "description": "Parse and Render Epubs",
"main": "lib/index.js", "main": "lib/index.js",
"module": "src/index.js", "module": "src/index.js",

View file

@ -46,7 +46,8 @@ class Book {
requestCredentials: undefined, requestCredentials: undefined,
requestHeaders: undefined, requestHeaders: undefined,
encoding: undefined, encoding: undefined,
replacements: undefined replacements: undefined,
canonical: undefined
}); });
extend(this.settings, options); extend(this.settings, options);
@ -320,6 +321,27 @@ class Book {
return resolved; 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 * Determine the type of they input passed to open
* @private * @private
@ -369,13 +391,13 @@ class Book {
unpack(opf) { unpack(opf) {
this.package = 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, { this.resources = new Resources(this.package.manifest, {
archive: this.archive, archive: this.archive,
resolver: this.resolve.bind(this), resolver: this.resolve.bind(this),
request: this.request.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(() => { this.loadNavigation(this.package).then(() => {

View file

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

View file

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

View file

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

View file

@ -5,13 +5,13 @@ import Path from "./path";
export function replaceBase(doc, section){ export function replaceBase(doc, section){
var base; var base;
var head; var head;
var url = section.url;
var absolute = (url.indexOf("://") > -1);
if(!doc){ if(!doc){
return; return;
} }
// head = doc.querySelector("head");
// base = head.querySelector("base");
head = qs(doc, "head"); head = qs(doc, "head");
base = qs(head, "base"); base = qs(head, "base");
@ -20,13 +20,18 @@ export function replaceBase(doc, section){
head.insertBefore(base, head.firstChild); 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){ export function replaceCanonical(doc, section){
var head; var head;
var link; var link;
var url = section.url; // window.location.origin + window.location.pathname + "?loc=" + encodeURIComponent(section.url); var url = section.canonical;
if(!doc){ if(!doc){
return; return;